/* eslint-disable react/no-children-prop */ import { ChangeEvent, useCallback, useEffect, useMemo, useRef, useState, } from 'react' import { formattedShortTime } from '../../lib/dateFormatting' import { Box, HStack, SpanBox, VStack } from '../elements/LayoutPrimitives' import MarkdownIt from 'markdown-it' import MdEditor, { Plugins } from 'react-markdown-editor-lite' import 'react-markdown-editor-lite/lib/index.css' import ReactMarkdown from 'react-markdown' import throttle from 'lodash/throttle' import { updateHighlightMutation } from '../../lib/networking/mutations/updateHighlightMutation' import { Highlight } from '../../lib/networking/fragments/highlightFragment' import { Button } from '../elements/Button' import { ModalContent, ModalOverlay, ModalRoot, } from '../elements/ModalPrimitives' import { CloseButton } from '../elements/CloseButton' import { StyledText } from '../elements/StyledText' import remarkGfm from 'remark-gfm' import { RcEditorStyles } from './RcEditorStyles' import { isDarkTheme } from '../../lib/themeUpdater' import { showErrorToast, showSuccessToast } from '../../lib/toastHelpers' const mdParser = new MarkdownIt() MdEditor.use(Plugins.TabInsert, { tabMapValue: 1, // note that 1 means a '\t' instead of ' '. }) type NoteSectionProps = { targetId: string placeHolder: string mode: 'edit' | 'preview' setEditMode: (set: 'edit' | 'preview') => void text: string | undefined saveText: (text: string, completed: (success: boolean) => void) => void } export function HighlightNoteBox(props: NoteSectionProps): JSX.Element { const [lastSaved, setLastSaved] = useState(undefined) const saveText = useCallback( (text, updateTime) => { props.saveText(text, (success) => { if (success) { setLastSaved(updateTime) } }) }, [props] ) return ( ) } type HighlightViewNoteProps = { targetId: string placeHolder: string mode: 'edit' | 'preview' highlight: Highlight setEditMode: (set: 'edit' | 'preview') => void text: string | undefined updateHighlight: (highlight: Highlight) => void } export function HighlightViewNote(props: HighlightViewNoteProps): JSX.Element { const [lastSaved, setLastSaved] = useState(undefined) const saveText = useCallback( (text, updateTime) => { ;(async () => { const success = await updateHighlightMutation({ annotation: text, highlightId: props.highlight?.id, }) if (success) { setLastSaved(updateTime) props.highlight.annotation = text props.updateHighlight(props.highlight) showSuccessToast('Note saved.', { position: 'bottom-right', }) } else { showErrorToast('Error saving note.', { position: 'bottom-right', }) } })() }, [props] ) return ( ) } type MarkdownNote = { targetId: string placeHolder: string mode: 'edit' | 'preview' setEditMode: (set: 'edit' | 'preview') => void text: string | undefined fillBackground: boolean | undefined lastSaved: Date | undefined saveText: (text: string, updateTime: Date) => void } export function MarkdownNote(props: MarkdownNote): JSX.Element { const editorRef = useRef(null) const [lastChanged, setLastChanged] = useState(undefined) const [errorSaving, setErrorSaving] = useState(undefined) const isDark = isDarkTheme() return ( <> {props.mode == 'edit' ? ( ) => { if (event.code.toLowerCase() === 'escape') { props.setEditMode('preview') event.preventDefault() event.stopPropagation() } }} > mdParser.render(text)} /> {errorSaving && ( {errorSaving} )} ) : ( <> *': { m: '0px', }, }} onClick={() => props.setEditMode('edit')} > )} ) }