@@ -44,6 +44,8 @@ import {
4444import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid" ;
4545import { ALL_SELECTION_SUGGESTIONS } from "~/utils/predefinedSelections" ;
4646import { getAlias } from "~/utils/parseResultSettings" ;
47+ import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors" ;
48+ import { IndexSchema } from "~/components/settings/utils/zodSchema" ;
4749
4850const getSourceCandidates = ( cs : Condition [ ] ) : string [ ] =>
4951 cs . flatMap ( ( c ) =>
@@ -434,6 +436,7 @@ type QueryEditorComponent = (props: {
434436 setHasResults ?: ( ) => void ;
435437 hideCustomSwitch ?: boolean ;
436438 showAlias ?: boolean ;
439+ discourseNodeType ?: string ;
437440} ) => JSX . Element ;
438441
439442const QueryEditor : QueryEditorComponent = ( {
@@ -442,6 +445,7 @@ const QueryEditor: QueryEditorComponent = ({
442445 setHasResults,
443446 hideCustomSwitch,
444447 showAlias,
448+ discourseNodeType,
445449} ) => {
446450 useEffect ( ( ) => {
447451 const previewQuery = ( ( e : CustomEvent ) => {
@@ -476,6 +480,39 @@ const QueryEditor: QueryEditorComponent = ({
476480 const [ conditions , _setConditions ] = useState ( initialConditions ) ;
477481 const [ selections , setSelections ] = useState ( initialSelections ) ;
478482 const [ custom , setCustom ] = useState ( initialCustom ) ;
483+
484+ const blockPropSyncTimeoutRef = useRef ( 0 ) ;
485+ const lastSyncedIndexRef = useRef ( "" ) ;
486+ useEffect ( ( ) => {
487+ return ( ) => window . clearTimeout ( blockPropSyncTimeoutRef . current ) ;
488+ } , [ ] ) ;
489+ useEffect ( ( ) => {
490+ if ( ! discourseNodeType ) return ;
491+
492+ const stripped : unknown = JSON . parse (
493+ JSON . stringify ( { conditions, selections, custom } , ( key , value : unknown ) =>
494+ key === "uid" ? undefined : value ,
495+ ) ,
496+ ) ;
497+
498+ const serialized = JSON . stringify ( stripped ) ;
499+ if ( serialized === lastSyncedIndexRef . current ) return ;
500+
501+ const result = IndexSchema . safeParse ( stripped ) ;
502+ if ( ! result . success ) {
503+ console . error ( "Index blockprop sync failed validation:" , result . error ) ;
504+ return ;
505+ }
506+
507+ window . clearTimeout ( blockPropSyncTimeoutRef . current ) ;
508+ blockPropSyncTimeoutRef . current = window . setTimeout ( ( ) => {
509+ setDiscourseNodeSetting ( discourseNodeType , [ "index" ] , result . data ) ;
510+ lastSyncedIndexRef . current = serialized ;
511+ } , 500 ) ;
512+
513+ return ( ) => window . clearTimeout ( blockPropSyncTimeoutRef . current ) ;
514+ } , [ conditions , selections , custom , discourseNodeType ] ) ;
515+
479516 const customNodeOnChange = ( value : string ) => {
480517 window . clearTimeout ( debounceRef . current ) ;
481518 setCustom ( value ) ;
0 commit comments