import { Card, Group, Select, Stack, Text, TextInput } from '@mantine/core'; import { useDebouncedValue, useDisclosure } from '@mantine/hooks'; import { DeleteItemModal, ManagementPill } from 'components'; import { useCommonHooks, useMutationAPI, useQueryAPI } from 'hooks'; import { useEffect, useState } from 'react'; import { cardClasses, selectClasses, textClasses, textInputClasses } from 'styles'; import type { Bookmark, BookmarkGroup } from 'types'; function DebouncedBookmarkItem({ bookmarkItem, setIsOpen, }: { bookmarkItem: Bookmark; setIsOpen: (isOpen: boolean) => void; }) { const { handleDelete, isMobile, theme } = useCommonHooks(); const [modalOpened, { open: openModal, close: closeModal }] = useDisclosure(false); const { mutateAsync } = useMutationAPI(); const { data: groupData, isFetched: groupsIsFetched } = useQueryAPI({ endpoint: 'api/bookmarkCategories', }); const [bookmark, setBookmark] = useState(bookmarkItem); const [groups, setGroups] = useState([]); const [update, setUpdate] = useState({}); const [debounced] = useDebouncedValue(update, 1000); const handleClose = () => { setIsOpen(false); closeModal(); }; useEffect(() => { if (Object.keys(debounced).length) { mutateAsync({ method: 'PATCH', endpoint: 'api/bookmarks', body: debounced }); } }, [debounced]); useEffect(() => { if (update !== bookmark && Object.keys(update).length) { setBookmark(update); } }, [update]); useEffect(() => { if (groupData) { setGroups( groupData.sort( (a: BookmarkGroup, b: BookmarkGroup) => a.categoryName.toLocaleLowerCase() > b.categoryName.toLocaleLowerCase(), ), ); } }, [groupData, groupsIsFetched]); return ( handleDelete(bookmark, 'api/bookmarks')} body={ Are you sure you want to delete this bookmark? {`"${bookmark.name}"`} This action cannot be undone! } title='Delete Bookmark' /> ) => { setUpdate({ ...bookmark, enabled: event.target.checked ? 1 : 0, }); }} openModal={openModal} /> { setUpdate({ ...bookmark, name: event.target.value, }); }} styles={ theme.textShadowsEnabled ? { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } : undefined } />