diff --git a/package-lock.json b/package-lock.json index 2e97ceda..3179f5e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "query-builder", - "version": "1.10.16", + "version": "1.10.17", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "query-builder", - "version": "1.10.16", + "version": "1.10.17", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 36a49ebe..7fc4f9af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "query-builder", - "version": "1.10.16", + "version": "1.10.17", "description": "Introduces new user interfaces for building queries in Roam", "main": "./build/main.js", "author": { diff --git a/src/components/TldrawCanvas.tsx b/src/components/TldrawCanvas.tsx index fed11452..a6d37a95 100644 --- a/src/components/TldrawCanvas.tsx +++ b/src/components/TldrawCanvas.tsx @@ -249,6 +249,88 @@ const createDiscourseNode = async ({ } }; +const LabelDialogAutocomplete = ({ + setLabel, + setUid, + nodeType, + initialUid, + initialValue, + onSubmit, +}: { + setLabel: (text: string) => void; + setUid: (uid: string) => void; + nodeType: string; + initialUid: string; + initialValue: { text: string; uid: string }; + onSubmit: () => void; +}) => { + const [isLoading, setIsLoading] = useState(false); + const [options, setOptions] = useState([]); + useEffect(() => { + setIsLoading(true); + const conditionUid = window.roamAlphaAPI.util.generateUID(); + setTimeout(() => { + fireQuery({ + returnNode: "node", + selections: [], + conditions: [ + { + source: "node", + relation: "is a", + target: nodeType, + uid: conditionUid, + type: "clause", + }, + ], + }).then((results) => { + setOptions(results); + setIsLoading(false); + }); + }, 100); + }, [nodeType, setOptions]); + + const setValue = React.useCallback( + (r: Result) => { + setLabel(r.text); + setUid(r.uid); + }, + [setLabel, setUid] + ); + const onNewItem = React.useCallback( + (text: string) => ({ text, uid: initialUid }), + [initialUid] + ); + const itemToQuery = React.useCallback( + (result?: Result) => result?.text || "", + [] + ); + const filterOptions = React.useCallback( + (o: Result[], q: string) => + fuzzy + .filter(q, o, { extract: itemToQuery }) + .map((f) => f.original) + .filter((f): f is Result => !!f), + [itemToQuery] + ); + + return ( + + ); +}; + type NodeDialogProps = { label: string; onSuccess: (a: Result) => Promise; @@ -268,7 +350,6 @@ const LabelDialog = ({ }: RoamOverlayProps) => { const containerRef = useRef(null); const [error, setError] = useState(""); - const [options, setOptions] = useState([]); const initialLabel = useMemo(() => { if (_label) return _label; const { specification, text } = discourseContext.nodes[nodeType]; @@ -305,22 +386,8 @@ const LabelDialog = ({ onCancel(); onClose(); }; - useEffect(() => { - const conditionUid = window.roamAlphaAPI.util.generateUID(); - fireQuery({ - returnNode: "node", - selections: [], - conditions: [ - { - source: "node", - relation: "is a", - target: nodeType, - uid: conditionUid, - type: "clause", - }, - ], - }).then((results) => setOptions(results)); - }, [nodeType, setOptions]); + + // Listens for touch outside container to trigger close const touchRef = useRef(); useEffect(() => { const { current } = containerRef; @@ -346,29 +413,7 @@ const LabelDialog = ({ document.body.removeEventListener("touchend", touchEndListener); }; }, [containerRef, onCancelClick, touchRef]); - const setValue = React.useCallback( - (r: Result) => { - setLabel(r.text); - setUid(r.uid); - }, - [setLabel, setUid] - ); - const onNewItem = React.useCallback( - (text: string) => ({ text, uid: initialUid }), - [initialUid] - ); - const itemToQuery = React.useCallback( - (result?: Result) => result?.text || "", - [] - ); - const filterOptions = React.useCallback( - (o: Result[], q: string) => - fuzzy - .filter(q, o, { extract: itemToQuery }) - .map((f) => f.original) - .filter((f): f is Result => !!f), - [itemToQuery] - ); + return ( <>
-