import clsx from 'clsx'; import { ChangeEvent, useEffect, useState } from 'react'; import { useUserSession } from '@vertesia/ui/session'; import { ContentObjectItem } from '@vertesia/common'; import { ChevronsUpDown, X } from 'lucide-react'; import { Button, Styles, useFlag } from '@vertesia/ui/core'; import { Node } from '@vertesia/ui/widgets'; import { SelectDocumentModal } from './SelectDocumentModal'; const STORE_REGEX = /store:([a-f0-9]+)/; interface DocumentInputProps { object: Node; type: string; // the editor/input type } export function DocumentInput({ object }: DocumentInputProps) { const { client } = useUserSession(); const { off, on, isOn } = useFlag(); const [actualValue, setValue] = useState(object.value != null ? String(object.value) : ''); const [doc, setDoc] = useState(undefined) const _onChange = (event: ChangeEvent) => { const value = event.target.value; setValue(value); object.value = value; }; const clearValue = () => { setValue(''); object.value = ''; setDoc(undefined); }; const onSelect = (value?: ContentObjectItem) => { if (value) { const uri = "store:" + value.id; setValue(uri); setDoc(value || undefined); object.value = uri; } off(); }; useEffect(() => { if (!actualValue || doc) { return; } const match = actualValue.match(STORE_REGEX); if (!match) { return; } client.objects.get(match[1]).then((doc) => { setDoc(doc); }).catch(() => { clearValue(); }); }, [actualValue]); return (
{doc &&
}
{doc &&
{doc.properties?.title || doc.name}
}
) }