import classNames from 'classnames'; import { Orchestrator, OrchestratorMeta, type OrchestratorStory, } from '../../utils/Orchestrator'; import D from '../../../i18n'; import { ComboboxOptionType } from '../../../components/shared/Combobox/ComboboxType'; import { Meta } from '@storybook/react'; import { type ComponentProps, type FormEventHandler, useCallback, useMemo, useState, } from 'react'; import { LunaticSource } from '../../../type.source'; import { useLocalStorage } from '../../../hooks/useLocalStorage'; type Props = { option: ComboboxOptionType; shouldDisplayOptionId?: boolean; selected?: boolean; }; const CustomComboboxOption = ({ option, shouldDisplayOptionId = true, selected, }: Props) => { const { id, value, label, score, melautoScore } = option; const scoreValues = ( {` [ Score: ${score} | MelautoScore: ${melautoScore} ] `} ); if (value === 'OTHER') { return (
{`${D.SUGGESTER_NO_RESULT} : ${D.SUGGESTER_ARBITRARY} "${label}"`}
); } if (label) { return (
{shouldDisplayOptionId && ( <> {id || value} - )} {label} {scoreValues}
); } return (
{id || value} {scoreValues}
); }; const meta: Meta = { title: 'Behaviour/SearchScore', ...OrchestratorMeta, }; export default meta; export const Default: OrchestratorStory = { args: { slots: { ComboboxOption: CustomComboboxOption, }, }, render(args) { return ; }, }; type Nomenclature = { name: string; uri: string }; type GetReferentiel = NonNullable< ComponentProps['getReferentiel'] >; function PasteOrchestrator( props: Readonly> ) { const [source, setSource, sourceAsString] = useLocalStorage('story-source-search'); const [nomenclatures, setNomenclatures] = useLocalStorage< Nomenclature[] | null >('story-nomenclatures-search'); const getReferentiel = useCallback( async (name: string) => { const nomenclature = (nomenclatures ?? []).find((n) => n.name === name); if (!nomenclature) { throw new Error(`Nomenclature "${name}" introuvable`); } const response = await fetch(nomenclature.uri); if (!response.ok) { throw new Error( `Impossible de charger "${name}" (${response.status} ${response.statusText})` ); } return response.json(); }, [nomenclatures] ); const extraTabs = useMemo(() => { return [ { label: 'Source & nomenclatures', children: ( { setNomenclatures(JSON.stringify(nomenclatures)); setSource(source); }} /> ), }, ]; }, [sourceAsString, nomenclatures, setSource, setNomenclatures]); if (!source) { return <>{extraTabs[0].children}; } const key = `${sourceAsString}-${JSON.stringify(nomenclatures)}`; return (
); } function FormForSourceAndNomenclatures( props: Readonly<{ source: string; nomenclatures: Nomenclature[]; onChange: (source: string, nomenclatures: Nomenclature[]) => void; }> ) { const [items, setItems] = useState(props.nomenclatures); const add = () => setItems((prev) => [...prev, { name: '', uri: '' }]); const remove = (index: number) => setItems((prev) => prev.filter((_, i) => i !== index)); const update = (index: number, field: keyof Nomenclature, value: string) => setItems((prev) => prev.map((item, i) => (i === index ? { ...item, [field]: value } : item)) ); const onSubmit: FormEventHandler = (e) => { e.preventDefault(); const data = new FormData(e.currentTarget); if (data.has('source')) { props.onChange( data.get('source') as string, items.filter((item) => item.name.trim() && item.uri.trim()) ); } }; return (
Paste the source.json