11import React from "react" ;
22import getSubTree from "roamjs-components/util/getSubTree" ;
33import createBlock from "roamjs-components/writes/createBlock" ;
4- import { Checkbox } from "@blueprintjs/core" ;
54import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid" ;
65import deleteBlock from "roamjs-components/writes/deleteBlock" ;
76import refreshConfigTree from "~/utils/refreshConfigTree" ;
87import getDiscourseNodes from "~/utils/getDiscourseNodes" ;
98import getDiscourseNodeFormatExpression from "~/utils/getDiscourseNodeFormatExpression" ;
109import QueryEditor from "~/components/QueryEditor" ;
1110import internalError from "~/utils/internalError" ;
11+ import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors" ;
12+ import { DiscourseNodeFlagPanel } from "~/components/settings/components/BlockPropSettingPanels" ;
1213
1314const NodeSpecification = ( {
1415 parentUid,
@@ -20,11 +21,14 @@ const NodeSpecification = ({
2021 parentSetEnabled ?: ( enabled : boolean ) => void ;
2122} ) => {
2223 const [ migrated , setMigrated ] = React . useState ( false ) ;
23- const [ enabled , setEnabled ] = React . useState < string > (
24+ const enabledBlockUid = React . useMemo (
2425 ( ) =>
2526 getSubTree ( { tree : getBasicTreeByParentUid ( parentUid ) , key : "enabled" } )
2627 ?. uid ,
28+ [ parentUid ] ,
2729 ) ;
30+ const [ enabled , setEnabled ] = React . useState ( ! ! enabledBlockUid ) ;
31+
2832 React . useEffect ( ( ) => {
2933 if ( enabled ) {
3034 const scratchNode = getSubTree ( { parentUid, key : "scratch" } ) ;
@@ -69,19 +73,41 @@ const NodeSpecification = ({
6973 } ,
7074 } ) ,
7175 )
72- . then ( ( ) => setMigrated ( true ) )
76+ . then ( ( ) => {
77+ setDiscourseNodeSetting ( node . type , [ "specification" , "query" ] , {
78+ conditions : [
79+ {
80+ type : "clause" as const ,
81+ source : node . text ,
82+ relation : "has title" ,
83+ target : `/${ getDiscourseNodeFormatExpression ( node . format ) . source } /` ,
84+ } ,
85+ ] ,
86+ selections : [ ] ,
87+ custom : "" ,
88+ returnNode : node . text ,
89+ } ) ;
90+ setMigrated ( true ) ;
91+ } )
7392 . catch ( ( error ) => {
7493 internalError ( { error } ) ;
7594 } ) ;
7695 }
7796 } else {
7897 const tree = getBasicTreeByParentUid ( parentUid ) ;
7998 const scratchNode = getSubTree ( { tree, key : "scratch" } ) ;
80- Promise . all ( scratchNode . children . map ( ( c ) => deleteBlock ( c . uid ) ) ) . catch (
81- ( error ) => {
99+ Promise . all ( scratchNode . children . map ( ( c ) => deleteBlock ( c . uid ) ) )
100+ . then ( ( ) => {
101+ setDiscourseNodeSetting ( node . type , [ "specification" , "query" ] , {
102+ conditions : [ ] ,
103+ selections : [ ] ,
104+ custom : "" ,
105+ returnNode : "" ,
106+ } ) ;
107+ } )
108+ . catch ( ( error ) => {
82109 internalError ( { error } ) ;
83- } ,
84- ) ;
110+ } ) ;
85111 }
86112 return ( ) => {
87113 refreshConfigTree ( ) ;
@@ -90,47 +116,33 @@ const NodeSpecification = ({
90116 return (
91117 < div className = { "roamjs-node-specification" } >
92118 < style >
93- { `.roamjs-node-specification .bp3-button.bp3-intent-primary { display: none; }` }
119+ { `.roamjs-node-specification .bp3-button.bp3-intent-primary { display: none; }
120+ .roamjs-node-specification .bp3-checkbox { visibility: hidden; }
121+ .roamjs-node-specification .bp3-checkbox .bp3-control-indicator { visibility: visible; }` }
94122 </ style >
95- < p >
96- < Checkbox
97- checked = { ! ! enabled }
98- className = { "ml-8 inline-block" }
99- onChange = { ( e ) => {
100- const flag = ( e . target as HTMLInputElement ) . checked ;
101- if ( flag ) {
102- createBlock ( {
103- parentUid,
104- order : 2 ,
105- node : { text : "enabled" } ,
106- } )
107- . then ( ( uid : string ) => {
108- setEnabled ( uid ) ;
109- if ( parentSetEnabled ) parentSetEnabled ( true ) ;
110- } )
111- . catch ( ( error ) => {
112- internalError ( { error } ) ;
113- } ) ;
114- } else {
115- deleteBlock ( enabled )
116- . then ( ( ) => {
117- setEnabled ( "" ) ;
118- if ( parentSetEnabled ) parentSetEnabled ( false ) ;
119- } )
120- . catch ( ( error ) => {
121- internalError ( { error } ) ;
122- } ) ;
123- }
124- } }
125- />
126- </ p >
123+ < DiscourseNodeFlagPanel
124+ nodeType = { node . type }
125+ title = "enabled"
126+ description = ""
127+ settingKeys = { [ "specification" , "enabled" ] }
128+ defaultValue = { enabled }
129+ parentUid = { parentUid }
130+ uid = { enabledBlockUid }
131+ order = { 2 }
132+ onChange = { ( checked ) => {
133+ setEnabled ( checked ) ;
134+ parentSetEnabled ?.( checked ) ;
135+ } }
136+ />
127137 < div
128138 className = { `${ enabled ? "" : "bg-gray-200 opacity-75" } overflow-auto` }
129139 >
130140 < QueryEditor
131141 parentUid = { parentUid }
132142 key = { Number ( migrated ) }
133143 hideCustomSwitch
144+ discourseNodeType = { node . type }
145+ settingKey = "specification"
134146 />
135147 </ div >
136148 </ div >
0 commit comments