import dayjs, { Dayjs } from 'dayjs' import { useCallback, useState } from 'react' import { updatePageMutation } from '../../../lib/networking/mutations/updatePageMutation' import { ArticleAttributes } from '../../../lib/networking/library_items/useLibraryItems' import { LibraryItem } from '../../../lib/networking/library_items/useLibraryItems' import { showErrorToast, showSuccessToast } from '../../../lib/toastHelpers' import { CloseButton } from '../../elements/CloseButton' import { FormInput } from '../../elements/FormElements' import { Box, HStack, SpanBox, VStack } from '../../elements/LayoutPrimitives' import { ModalButtonBar, ModalContent, ModalOverlay, ModalRoot, } from '../../elements/ModalPrimitives' import { StyledText } from '../../elements/StyledText' import { StyledTextArea } from '../../elements/StyledTextArea' type EditLibraryItemModalProps = { onOpenChange: (open: boolean) => void item: LibraryItem updateItem: (item: LibraryItem) => Promise } export function EditLibraryItemModal( props: EditLibraryItemModalProps ): JSX.Element { const onSave = useCallback( ( title: string, author: string | undefined, description: string, savedAt: Dayjs, publishedAt: Dayjs | undefined ) => { ;(async () => { if (title !== '') { const res = await updatePageMutation({ pageId: props.item.node.id, title, description, byline: author, savedAt: savedAt.toISOString(), publishedAt: publishedAt ? publishedAt.toISOString() : undefined, }) if (res) { await props.updateItem({ cursor: props.item.cursor, node: { ...props.item.node, title: title, author: author, description: description, }, }) showSuccessToast('Link updated succesfully', { position: 'bottom-right', }) props.onOpenChange(false) } else { showErrorToast('There was an error updating your link', { position: 'bottom-right', }) } } else { showErrorToast('Title must be a non-empty value', { position: 'bottom-right', }) } })() }, [props] ) return ( ) } type EditArticleModalProps = { onOpenChange: (open: boolean) => void article: ArticleAttributes updateArticle: ( title: string, author: string | undefined, description: string, savedAt: string, publishedAt: string | undefined ) => void } export function EditArticleModal(props: EditArticleModalProps): JSX.Element { const onSave = useCallback( ( title: string, author: string | undefined, description: string, savedAt: Dayjs, publishedAt: Dayjs | undefined ) => { ;(async () => { if (title !== '') { const res = await updatePageMutation({ pageId: props.article.id, title, description, byline: author, savedAt: savedAt.toISOString(), publishedAt: publishedAt ? publishedAt.toISOString() : undefined, }) if (res) { props.updateArticle( title, author, description, savedAt.toISOString(), publishedAt ? publishedAt.toISOString() : undefined ) showSuccessToast('Link updated succesfully', { position: 'bottom-right', }) props.onOpenChange(false) } else { showErrorToast('There was an error updating your link', { position: 'bottom-right', }) } } else { showErrorToast('Title must be a non-empty value', { position: 'bottom-right', }) } })() }, [props] ) return ( ) } type EditItemModalProps = { title: string author: string | undefined description: string savedAt: Dayjs publishedAt: Dayjs | undefined onOpenChange: (open: boolean) => void onSave: ( title: string, author: string | undefined, description: string, savedAt: Dayjs, publishedAt: Dayjs | undefined ) => void } function EditItemModal(props: EditItemModalProps): JSX.Element { const [title, setTitle] = useState(props.title) const [author, setAuthor] = useState(props.author) const [savedAt, setSavedAt] = useState(props.savedAt) const [publishedAt, setPublishedAt] = useState(props.publishedAt) const [description, setDescription] = useState(props.description) const titleStyle = { mt: '22px', mb: '2px', fontFamily: '$display', fontWeight: '600', fontSize: '11px', color: '#898989', } const inputStyle = { mt: '1px', borderRadius: '5px', border: '1px solid $thBorderColor', fontFamily: 'Inter', fontWeight: '500', fontSize: '16px', height: '38px', p: '5px', color: '$thTextContrast2', '&:focus': { outline: 'none !important', border: '1px solid $omnivoreCtaYellow', }, } return ( { props.onOpenChange(false) }} css={{}} > { event.preventDefault() }} onEscapeKeyDown={(event) => { props.onOpenChange(false) event.preventDefault() event.stopPropagation() }} >
{ event.preventDefault() props.onSave(title, author, description, savedAt, publishedAt) }} > SAVED AT { const dateStr = event.target.value setSavedAt(dayjs(dateStr)) }} css={{ ...inputStyle, fontSize: '14px', textIndent: '0px', }} /> PUBLISHED AT { const dateStr = event.target.value setPublishedAt(dayjs(dateStr)) }} css={{ ...inputStyle, fontSize: '14px', textIndent: '0px', }} /> TITLE setTitle(event.target.value)} onFocus={(event) => { event.target.select() }} css={inputStyle} /> AUTHOR setAuthor(event.target.value)} onFocus={(event) => { event.target.select() }} css={inputStyle} /> DESCRIPTION setDescription(event.target.value)} onFocus={(event) => { event.target.select() }} maxLength={4000} />
) } type HeaderProps = { onOpenChange: (open: boolean) => void } function Header(props: HeaderProps): JSX.Element { return ( Edit Title & Description props.onOpenChange(false)} /> ) }