omnivore/packages/web/components/templates/article/SetLabelsModalPresenter.tsx

81 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import { useCallback, useEffect } from 'react'
2023-06-20 05:36:36 +00:00
import { useSetPageLabels } from '../../../lib/hooks/useSetPageLabels'
import { LabelsProvider } from './SetLabelsControl'
import { SetLabelsModal } from './SetLabelsModal'
import { useSetHighlightLabels } from '../../../lib/hooks/useSetHighlightLabels'
import { Highlight } from '../../../lib/networking/fragments/highlightFragment'
import { LibraryItemNode } from '../../../lib/networking/library_items/useLibraryItems'
2023-06-20 05:36:36 +00:00
type SetPageLabelsModalPresenterProps = {
libraryItem: LibraryItemNode
2023-06-20 05:36:36 +00:00
onOpenChange: (open: boolean) => void
}
export function SetPageLabelsModalPresenter(
props: SetPageLabelsModalPresenterProps
): JSX.Element {
const [labels, dispatchLabels] = useSetPageLabels(
props.libraryItem.id,
props.libraryItem.slug
)
2023-06-20 05:36:36 +00:00
const onOpenChange = useCallback(() => {
if (props.libraryItem) {
props.libraryItem.labels = labels.labels
}
props.onOpenChange(true)
}, [props, labels])
useEffect(() => {
dispatchLabels({
type: 'RESET',
labels: props.libraryItem.labels ?? [],
})
}, [props.libraryItem, dispatchLabels])
2023-06-20 05:36:36 +00:00
return (
<SetLabelsModal
provider={props.libraryItem}
2023-06-20 05:36:36 +00:00
selectedLabels={labels.labels}
dispatchLabels={dispatchLabels}
onOpenChange={onOpenChange}
2023-06-20 05:36:36 +00:00
/>
)
}
type SetHighlightLabelsModalPresenterProps = {
highlightId: string
highlight: Highlight
onUpdate: (updatedHighlight: Highlight) => void
2023-06-20 05:36:36 +00:00
onOpenChange: (open: boolean) => void
}
export function SetHighlightLabelsModalPresenter(
props: SetHighlightLabelsModalPresenterProps
): JSX.Element {
const [labels, dispatchLabels] = useSetHighlightLabels(props.highlightId)
useEffect(() => {
dispatchLabels({
type: 'RESET',
labels: props.highlight.labels ?? [],
})
}, [props.highlight, dispatchLabels])
const onOpenChange = useCallback(() => {
props.highlight.labels = labels.labels
props.onUpdate(props.highlight)
props.onOpenChange(true)
}, [props])
2023-06-20 05:36:36 +00:00
return (
<SetLabelsModal
provider={props.highlight}
selectedLabels={labels.labels}
dispatchLabels={dispatchLabels}
onOpenChange={onOpenChange}
2023-06-20 05:36:36 +00:00
/>
)
}