From 63585a9a36e1e560969ea1c06d5cbecf8388fba2 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 8 Sep 2023 12:22:34 +0800 Subject: [PATCH] Update link to docs for searches, run prettier --- .../web/pages/settings/saved-searches.tsx | 360 +++++++++++------- 1 file changed, 227 insertions(+), 133 deletions(-) diff --git a/packages/web/pages/settings/saved-searches.tsx b/packages/web/pages/settings/saved-searches.tsx index 06e3e4fd3..685d72e73 100644 --- a/packages/web/pages/settings/saved-searches.tsx +++ b/packages/web/pages/settings/saved-searches.tsx @@ -1,4 +1,11 @@ -import { Dispatch, MouseEventHandler, SetStateAction, useCallback, useEffect, useState } from "react" +import { + Dispatch, + MouseEventHandler, + SetStateAction, + useCallback, + useEffect, + useState, +} from 'react' import { SettingsLayout } from '../../components/templates/SettingsLayout' import { Button } from '../../components/elements/Button' import { styled, theme } from '../../components/tokens/stitches.config' @@ -25,13 +32,13 @@ import { } from '../../components/elements/DropdownElements' import { ConfirmationModal } from '../../components/patterns/ConfirmationModal' import { InfoLink } from '../../components/elements/InfoLink' -import { useGetSavedSearchQuery } from "../../lib/networking/queries/useGetSavedSearchQuery" -import { SavedSearch } from "../../lib/networking/fragments/savedSearchFragment" -import CheckboxComponent from "../../components/elements/Checkbox" -import { updateFilterMutation } from "../../lib/networking/mutations/updateFilterMutation" -import { saveFilterMutation } from "../../lib/networking/mutations/saveFilterMutation" -import { inRange } from 'lodash'; -import { deleteFilterMutation } from "../../lib/networking/mutations/deleteFilterMutation" +import { useGetSavedSearchQuery } from '../../lib/networking/queries/useGetSavedSearchQuery' +import { SavedSearch } from '../../lib/networking/fragments/savedSearchFragment' +import CheckboxComponent from '../../components/elements/Checkbox' +import { updateFilterMutation } from '../../lib/networking/mutations/updateFilterMutation' +import { saveFilterMutation } from '../../lib/networking/mutations/saveFilterMutation' +import { inRange } from 'lodash' +import { deleteFilterMutation } from '../../lib/networking/mutations/deleteFilterMutation' const HeaderWrapper = styled(Box, { width: '100%', @@ -146,22 +153,24 @@ export default function SavedSearchesPage(): JSX.Element { const { savedSearches, isLoading } = useGetSavedSearchQuery() const [nameInputText, setNameInputText] = useState('') const [queryInputText, setQueryInputText] = useState('') - const [editingId, setEditingId] = useState(null); + const [editingId, setEditingId] = useState(null) const [isCreateMode, setIsCreateMode] = useState(false) const [windowWidth, setWindowWidth] = useState(0) - const [confirmRemoveSavedSearchId, setConfirmRemoveSavedSearchId] = useState< - string | null - >(null) - const [draggedElementId, setDraggedElementId] = useState(null); - const [draggedElementPosition, setDraggedElementPosition] = useState<{ x: number, y: number } | null>(null); - const [sortedSavedSearch, setSortedSavedSearch] = useState([]); + const [confirmRemoveSavedSearchId, setConfirmRemoveSavedSearchId] = + useState(null) + const [draggedElementId, setDraggedElementId] = useState(null) + const [draggedElementPosition, setDraggedElementPosition] = + useState<{ x: number; y: number } | null>(null) + const [sortedSavedSearch, setSortedSavedSearch] = useState([]) // Some theming stuff here. const breakpoint = 768 applyStoredTheme(false) useEffect(() => { - setSortedSavedSearch([...(savedSearches ?? [])].sort((l, r) => l.position - r.position)) + setSortedSavedSearch( + [...(savedSearches ?? [])].sort((l, r) => l.position - r.position) + ) }, [isLoading]) useEffect(() => { @@ -184,7 +193,12 @@ export default function SavedSearchesPage(): JSX.Element { async function createSavedSearch(): Promise { try { - const savedFilter = await saveFilterMutation({ name: nameInputText, filter: queryInputText, category: 'Search', position: sortedSavedSearch?.length ?? 0 }); + const savedFilter = await saveFilterMutation({ + name: nameInputText, + filter: queryInputText, + category: 'Search', + position: sortedSavedSearch?.length ?? 0, + }) showSuccessToast(`Added Filter: ${nameInputText}`) if (savedFilter) { @@ -199,12 +213,16 @@ export default function SavedSearchesPage(): JSX.Element { async function updateSavedSearch(id: string): Promise { resetSavedSearchState() - const changedSortedSearch = sortedSavedSearch?.find(it => it.id == id) + const changedSortedSearch = sortedSavedSearch?.find((it) => it.id == id) if (changedSortedSearch != undefined) { changedSortedSearch.name = nameInputText changedSortedSearch.filter = queryInputText setSortedSavedSearch(sortedSavedSearch) - await updateFilterMutation( { id, name: nameInputText, filter: queryInputText }); + await updateFilterMutation({ + id, + name: nameInputText, + filter: queryInputText, + }) } } @@ -219,15 +237,21 @@ export default function SavedSearchesPage(): JSX.Element { } async function onDeleteSavedSearch(id: string): Promise { - const currentElement = sortedSavedSearch?.find((it) => it.id == id); + const currentElement = sortedSavedSearch?.find((it) => it.id == id) if (currentElement) { await deleteFilterMutation(id) setSortedSavedSearch( sortedSavedSearch - .filter(it => it.id !== id) - .map(it => { - return { ...it, position: currentElement.position > it.position ? it.position : it.position - 1 } + .filter((it) => it.id !== id) + .map((it) => { + return { + ...it, + position: + currentElement.position > it.position + ? it.position + : it.position - 1, + } }) ) } @@ -239,18 +263,34 @@ export default function SavedSearchesPage(): JSX.Element { setConfirmRemoveSavedSearchId(id) } - async function updatePositionOnMouseUp(y: number): Promise { - const currentElement = sortedSavedSearch?.find(({ id }) => id == draggedElementId); + async function updatePositionOnMouseUp( + y: number + ): Promise { + const currentElement = sortedSavedSearch?.find( + ({ id }) => id == draggedElementId + ) if (currentElement) { - const idx = Math.floor(((y + window.scrollY - 25) - TOP_SETTINGS_PANEL) / HEIGHT_SETTING_CARD) - const correctedIdx = Math.min(Math.max(idx, 0), sortedSavedSearch?.length - 1) + const idx = Math.floor( + (y + window.scrollY - 25 - TOP_SETTINGS_PANEL) / HEIGHT_SETTING_CARD + ) + const correctedIdx = Math.min( + Math.max(idx, 0), + sortedSavedSearch?.length - 1 + ) const moveUp = correctedIdx < currentElement.position if (correctedIdx != currentElement.position) { const newlyOrdered = sortedSavedSearch ?.map((search) => { - let pos = search.position; - if (inRange(pos, Math.min(correctedIdx, currentElement.position), Math.max(correctedIdx, currentElement.position)) || search.position == correctedIdx) { + let pos = search.position + if ( + inRange( + pos, + Math.min(correctedIdx, currentElement.position), + Math.max(correctedIdx, currentElement.position) + ) || + search.position == correctedIdx + ) { pos = search.position + (moveUp ? +1 : -1) } if (draggedElementId == search?.id) { @@ -258,12 +298,15 @@ export default function SavedSearchesPage(): JSX.Element { } return { ...search, - position: pos + position: pos, } }) - ?.sort((l, r) => l.position - r.position); + ?.sort((l, r) => l.position - r.position) setSortedSavedSearch(newlyOrdered) - return updateFilterMutation({ ...currentElement, position: correctedIdx }) + return updateFilterMutation({ + ...currentElement, + position: correctedIdx, + }) } } @@ -288,9 +331,7 @@ export default function SavedSearchesPage(): JSX.Element { > {confirmRemoveSavedSearchId ? ( { await onDeleteSavedSearch(confirmRemoveSavedSearchId) setConfirmRemoveSavedSearchId(null) @@ -308,7 +349,7 @@ export default function SavedSearchesPage(): JSX.Element { Saved Searches - + {sortedSavedSearch ? sortedSavedSearch.map((savedSearch, i) => { - const isLastChild = i === sortedSavedSearch.length - 1 - const isFirstChild = i === 0 - const positionY = TOP_SETTINGS_PANEL + (HEIGHT_SETTING_CARD * (i + 1)); - const cardProps = { - savedSearch, - editingId, - isCreateMode: isCreateMode, - isLastChild: isLastChild, - isFirstChild: isFirstChild, - setEditingId, - nameInputText: nameInputText, - queryInputText: queryInputText, - setNameInputText: setNameInputText, - setQueryInputText: setQueryInputText, - setIsCreateMode: setIsCreateMode, - resetState: resetSavedSearchState, - updateSavedSearch, - deleteSavedSearch, - createSavedSearch, - draggedElementId, - setDraggedElementId, - onEditPress, - setDraggedElementPosition, - isSwappedCard: (draggedElementPosition && draggedElementId != savedSearch.id && (draggedElementPosition.y + window.scrollY + HEIGHT_SETTING_CARD)> positionY && draggedElementPosition?.y + window.scrollY + HEIGHT_SETTING_CARD < positionY + HEIGHT_SETTING_CARD) || undefined, - updatePositionOnMouseUp - } - if (editingId == savedSearch.id) { - if (windowWidth >= breakpoint) { - return - } else { - return + const isLastChild = i === sortedSavedSearch.length - 1 + const isFirstChild = i === 0 + const positionY = + TOP_SETTINGS_PANEL + HEIGHT_SETTING_CARD * (i + 1) + const cardProps = { + savedSearch, + editingId, + isCreateMode: isCreateMode, + isLastChild: isLastChild, + isFirstChild: isFirstChild, + setEditingId, + nameInputText: nameInputText, + queryInputText: queryInputText, + setNameInputText: setNameInputText, + setQueryInputText: setQueryInputText, + setIsCreateMode: setIsCreateMode, + resetState: resetSavedSearchState, + updateSavedSearch, + deleteSavedSearch, + createSavedSearch, + draggedElementId, + setDraggedElementId, + onEditPress, + setDraggedElementPosition, + isSwappedCard: + (draggedElementPosition && + draggedElementId != savedSearch.id && + draggedElementPosition.y + + window.scrollY + + HEIGHT_SETTING_CARD > + positionY && + draggedElementPosition?.y + + window.scrollY + + HEIGHT_SETTING_CARD < + positionY + HEIGHT_SETTING_CARD) || + undefined, + updatePositionOnMouseUp, + } + if (editingId == savedSearch.id) { + if (windowWidth >= breakpoint) { + return + } else { + return + } } - } - return ( - - ) - }) + return ( + + ) + }) : null} @@ -462,7 +515,7 @@ type EditCardProps = { setQueryInputText: Dispatch> isCreateMode: boolean setIsCreateMode: Dispatch> - setEditingId: Dispatch> + setEditingId: Dispatch> setNameInputText: Dispatch> createSavedSearch: () => Promise updateSavedSearch: (id: string) => Promise @@ -473,8 +526,10 @@ type EditCardProps = { isLastChild?: boolean | undefined draggedElementId: string | null setDraggedElementId: Dispatch> - setDraggedElementPosition: Dispatch> - isSwappedCard?: boolean, + setDraggedElementPosition: Dispatch< + SetStateAction<{ x: number; y: number } | null> + > + isSwappedCard?: boolean updatePositionOnMouseUp?: (y: number) => Promise } @@ -504,13 +559,22 @@ function GenericTableCard( setDraggedElementId, setDraggedElementPosition, isSwappedCard, - updatePositionOnMouseUp + updatePositionOnMouseUp, } = props const [isVisible, setIsVisible] = useState(!!savedSearch?.visible) - const showInput = editingId === savedSearch?.id || (isCreateMode && !savedSearch) + const showInput = + editingId === savedSearch?.id || (isCreateMode && !savedSearch) const iconColor = isDarkTheme() ? '#D8D7D5' : '#5F5E58' - const DEFAULT_STYLE = { position: null }; - const [style, setStyle] = useState>(DEFAULT_STYLE) + const DEFAULT_STYLE = { position: null } + const [style, setStyle] = + useState< + Partial<{ + position: string | null + top: string + left: string + maxWidth: string + }> + >(DEFAULT_STYLE) const handleEdit = () => { editingId && updateSavedSearch(editingId) setEditingId(null) @@ -535,7 +599,7 @@ function GenericTableCard( onClick={() => onEditPress(savedSearch)} disabled={isCreateMode} > - + (savedSearch ? deleteSavedSearch(savedSearch.id) : null)} + onClick={() => + savedSearch ? deleteSavedSearch(savedSearch.id) : null + } disabled={isCreateMode} > @@ -575,29 +641,41 @@ function GenericTableCard( ) } - const onMouseDown= (e: MouseEvent) => { + const onMouseDown = (e: MouseEvent) => { if (savedSearch) { setDraggedElementId(savedSearch.id) - setDraggedElementPosition({ y: e.clientY - 25, x: e.clientX - 25}) + setDraggedElementPosition({ y: e.clientY - 25, x: e.clientX - 25 }) } } const onMouseUp = async (e: MouseEvent) => { - if (draggedElementId != null && draggedElementId == savedSearch?.id && updatePositionOnMouseUp) { + if ( + draggedElementId != null && + draggedElementId == savedSearch?.id && + updatePositionOnMouseUp + ) { const updatePosition = updatePositionOnMouseUp(e.clientY) - setDraggedElementId(null); - setStyle(DEFAULT_STYLE); - setDraggedElementPosition(null); - await updatePosition; + setDraggedElementId(null) + setStyle(DEFAULT_STYLE) + setDraggedElementPosition(null) + await updatePosition } } - const onMouseMove = useCallback((e: MouseEvent) => { - if (draggedElementId != null && draggedElementId == savedSearch?.id) { - setStyle({ position: "absolute", top: `${e.clientY - 25 + window.scrollY}px`, left: `${e.clientX - 25 + window.scrollX}px`, maxWidth: '865px' }); - setDraggedElementPosition({ y: e.clientY - 25, x: e.clientX - 25}); - } - }, [draggedElementId, savedSearch, setDraggedElementPosition]) + const onMouseMove = useCallback( + (e: MouseEvent) => { + if (draggedElementId != null && draggedElementId == savedSearch?.id) { + setStyle({ + position: 'absolute', + top: `${e.clientY - 25 + window.scrollY}px`, + left: `${e.clientX - 25 + window.scrollX}px`, + maxWidth: '865px', + }) + setDraggedElementPosition({ y: e.clientY - 25, x: e.clientX - 25 }) + } + }, + [draggedElementId, savedSearch, setDraggedElementPosition] + ) useEffect(() => { window.addEventListener('mousemove', onMouseMove) @@ -607,26 +685,29 @@ function GenericTableCard( }, [draggedElementId, onMouseMove]) const setVisibility = async () => { - await updateFilterMutation({ ...savedSearch, visible: !isVisible} ) - setIsVisible(!isVisible); + await updateFilterMutation({ ...savedSearch, visible: !isVisible }) + setIsVisible(!isVisible) } return ( - - - + distribution="start" + alignment="center" + css={{ + padding: '0 5px', + }} + > + + @@ -678,14 +763,13 @@ function GenericTableCard( }} > {showInput && !savedSearch ? ( - - {editingId === savedSearch?.id @@ -793,7 +877,7 @@ function GenericTableCard( padding: '0px 8px', }} > - {(editingId === savedSearch?.id || !savedSearch) ? ( + {editingId === savedSearch?.id || !savedSearch ? ( <> @@ -827,7 +913,11 @@ function GenericTableCard( > onEditPress(savedSearch)} disabled={isCreateMode} > @@ -835,7 +925,11 @@ function GenericTableCard( deleteSavedSearch(savedSearch.id)} disabled={isCreateMode || savedSearch?.defaultFilter} >