-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-1280: Port Discourse node: Query builder and editor component for block props #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: eng-1225-discourse-node-migrate-settings-prod
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,8 @@ import { | |
| import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid"; | ||
| import { ALL_SELECTION_SUGGESTIONS } from "~/utils/predefinedSelections"; | ||
| import { getAlias } from "~/utils/parseResultSettings"; | ||
| import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors"; | ||
| import { IndexSchema } from "~/components/settings/utils/zodSchema"; | ||
|
|
||
| const getSourceCandidates = (cs: Condition[]): string[] => | ||
| cs.flatMap((c) => | ||
|
|
@@ -434,6 +436,7 @@ type QueryEditorComponent = (props: { | |
| setHasResults?: () => void; | ||
| hideCustomSwitch?: boolean; | ||
| showAlias?: boolean; | ||
| discourseNodeType?: string; | ||
| }) => JSX.Element; | ||
|
|
||
| const QueryEditor: QueryEditorComponent = ({ | ||
|
|
@@ -442,6 +445,7 @@ const QueryEditor: QueryEditorComponent = ({ | |
| setHasResults, | ||
| hideCustomSwitch, | ||
| showAlias, | ||
| discourseNodeType, | ||
| }) => { | ||
| useEffect(() => { | ||
| const previewQuery = ((e: CustomEvent) => { | ||
|
|
@@ -476,6 +480,39 @@ const QueryEditor: QueryEditorComponent = ({ | |
| const [conditions, _setConditions] = useState(initialConditions); | ||
| const [selections, setSelections] = useState(initialSelections); | ||
| const [custom, setCustom] = useState(initialCustom); | ||
|
|
||
| const blockPropSyncTimeoutRef = useRef(0); | ||
| const lastSyncedIndexRef = useRef(""); | ||
| useEffect(() => { | ||
| return () => window.clearTimeout(blockPropSyncTimeoutRef.current); | ||
| }, []); | ||
| useEffect(() => { | ||
| if (!discourseNodeType) return; | ||
|
|
||
| const stripped: unknown = JSON.parse( | ||
| JSON.stringify({ conditions, selections, custom }, (key, value: unknown) => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. format with prettier |
||
| key === "uid" ? undefined : value, | ||
| ), | ||
| ); | ||
|
|
||
| const serialized = JSON.stringify(stripped); | ||
| if (serialized === lastSyncedIndexRef.current) return; | ||
|
|
||
| const result = IndexSchema.safeParse(stripped); | ||
| if (!result.success) { | ||
| console.error("Index blockprop sync failed validation:", result.error); | ||
| return; | ||
| } | ||
|
|
||
| window.clearTimeout(blockPropSyncTimeoutRef.current); | ||
| blockPropSyncTimeoutRef.current = window.setTimeout(() => { | ||
| setDiscourseNodeSetting(discourseNodeType, ["index"], result.data); | ||
| lastSyncedIndexRef.current = serialized; | ||
| }, 250); | ||
|
|
||
| return () => window.clearTimeout(blockPropSyncTimeoutRef.current); | ||
| }, [conditions, selections, custom, discourseNodeType]); | ||
|
|
||
| const customNodeOnChange = (value: string) => { | ||
| window.clearTimeout(debounceRef.current); | ||
| setCustom(value); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import type { DiscourseNode } from "~/utils/getDiscourseNodes"; | |
| import QueryBuilder from "~/components/QueryBuilder"; | ||
| import parseQuery, { DEFAULT_RETURN_NODE } from "~/utils/parseQuery"; | ||
| import createBlock from "roamjs-components/writes/createBlock"; | ||
| import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors"; | ||
|
|
||
| const NodeIndex = ({ | ||
| parentUid, | ||
|
|
@@ -25,7 +26,7 @@ const NodeIndex = ({ | |
| ); | ||
| useEffect(() => { | ||
| if (!showQuery) { | ||
| createBlock({ | ||
| void createBlock({ | ||
| parentUid: initialQueryArgs.conditionsNodesUid, | ||
| node: { | ||
| text: "clause", | ||
|
|
@@ -48,12 +49,30 @@ const NodeIndex = ({ | |
| }, | ||
| ], | ||
| }, | ||
| }).then(() => setShowQuery(true)); | ||
| }).then(() => { | ||
| setDiscourseNodeSetting(node.type, ["index"], { | ||
| conditions: [ | ||
| { | ||
| type: "clause", | ||
| source: DEFAULT_RETURN_NODE, | ||
| relation: "is a", | ||
| target: node.text, | ||
| }, | ||
| ], | ||
| selections: [], | ||
| }); | ||
|
|
||
| setShowQuery(true); | ||
| }); | ||
| } | ||
| }, [parentUid, initialQueryArgs, showQuery]); | ||
| }, [parentUid, initialQueryArgs, showQuery, node.text, node.type]); | ||
| return ( | ||
| <ExtensionApiContextProvider {...onloadArgs}> | ||
| {showQuery ? <QueryBuilder pageUid={parentUid} /> : <Spinner />} | ||
| {showQuery ? ( | ||
| <QueryBuilder pageUid={parentUid} discourseNodeType={node.type} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The pageUid/parentUid is the EG: in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah never mind, we wouldn't want to make an extra query/check for every query instance |
||
| ) : ( | ||
| <Spinner /> | ||
| )} | ||
| </ExtensionApiContextProvider> | ||
| ); | ||
| }; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.