88 useCommanderComponent ,
99 useAnyComponentCommands ,
1010 useCommanderState ,
11- useCommanderChildrenComponents
11+ useChildCommandHook ,
12+ useChildCommandRunner
1213} from "@zenflux/react-commander/use-commands" ;
1314
1415import ChannelItemAccordion from "@zenflux/app-budget-allocation/src/components//channel/channel-item-accordion.tsx" ;
@@ -20,32 +21,23 @@ import type { ChannelItemAccordionComponent } from "@zenflux/app-budget-allocati
2021const scheduler = new EventEmitter ( ) ;
2122
2223// On channel list, request edit title name
23- function onEditRequest (
24+ function _onEditRequest (
2425 channel : ChannelItemAccordionComponent ,
2526 setSelected : ( selected : { [ key : string ] : boolean } ) => void ,
26- channelsCommands : ReturnType < typeof useCommanderComponent > ,
27- accordionItemCommands : ReturnType < typeof useCommanderChildrenComponents > ,
27+ runAccordionItem : ReturnType < typeof useChildCommandRunner > ,
2828) {
2929 // Select the channel (trigger accordion item selection)
3030 setSelected ( { [ channel . props . meta . id ] : true } ) ;
3131
32- const correspondingCommand = accordionItemCommands . find ( ( command ) => {
33- return command . getInternalContext ( ) . props . itemKey === channel . props . meta . id ;
34- } ) ;
35-
36- const tryToEnableEdit = ( correspondingCommand : any ) => {
37- // Try tell accordion to enter edit mode
38- correspondingCommand ?. run ( "UI/AccordionItem/EditableTitle" , { state : true } ) ;
39-
40- return correspondingCommand ;
41- } ;
42-
43- if ( tryToEnableEdit ( correspondingCommand ) ) return ;
32+ const enabled = runAccordionItem ( channel . props . meta . id , "UI/AccordionItem/EditableTitle" , { state : true } ) ;
33+ if ( enabled ) return ;
4434
45- scheduler . once ( `enable-editable-title-${ channel . props . meta . id } ` , tryToEnableEdit ) ;
35+ scheduler . once ( `enable-editable-title-${ channel . props . meta . id } ` , ( ) =>
36+ runAccordionItem ( channel . props . meta . id , "UI/AccordionItem/EditableTitle" , { state : true } )
37+ ) ;
4638}
4739
48- function onRemoveRequest (
40+ function _onRemoveRequest (
4941 channel : ChannelItemAccordionComponent ,
5042 getChannelsListState : ReturnType < typeof useCommanderState < ChannelListState > > [ 0 ] ,
5143 setChannelsListState : ReturnType < typeof useCommanderState < ChannelListState > > [ 1 ]
@@ -98,42 +90,33 @@ export function channelsListAccordionInteractions() {
9890
9991 const channelsCommands = useCommanderComponent ( "App/ChannelsList" ) ;
10092
101- useCommanderChildrenComponents ( "UI/AccordionItem" , ( accordionItemCommands ) => {
102- if ( ! accordionItemCommands . length ) return ;
103-
104- // Hook on title changed, run command within the channel list, to inform about the change
105- accordionItemCommands . forEach ( ( command ) => {
106- if ( ! command . isAlive ( ) ) return ;
107-
108- command . hook ( "UI/AccordionItem/OnTitleChanged" , ( result , args ) => {
109- channelsCommands . run ( "App/ChannelsList/SetName" , {
110- id : args ! . itemKey ,
111- name : args ! . title ,
112- } ) ;
93+ useChildCommandHook (
94+ "UI/AccordionItem" ,
95+ "UI/AccordionItem/OnTitleChanged" ,
96+ ( _result , args : any ) => {
97+ channelsCommands . run ( "App/ChannelsList/SetName" , {
98+ id : args . itemKey ,
99+ name : args . title ,
113100 } ) ;
101+ }
102+ ) ;
114103
115- // This will ensure that the accordion item will enter edit mode, if the channel list requested it
116- const key = `enable-editable-title-${ command . getInternalContext ( ) . props . itemKey } ` ;
117- if ( scheduler . eventNames ( ) . includes ( key ) ) {
118- scheduler . emit ( key , command ) ;
119- scheduler . removeAllListeners ( key ) ;
120- }
121- } ) ;
104+ const runAccordionItem = useChildCommandRunner (
105+ "UI/AccordionItem" ,
106+ ( ctx ) => ctx . props . itemKey
107+ ) ;
122108
109+ React . useEffect ( ( ) => {
123110 channelsCommands . hook ( "App/ChannelsList/EditRequest" , ( r , args : any ) =>
124- onEditRequest ( args . channel , setSelected , channelsCommands , accordionItemCommands ) ) ;
111+ _onEditRequest ( args . channel , setSelected , runAccordionItem ) ) ;
125112
126113 channelsCommands . hook ( "App/ChannelsList/RemoveRequest" , ( r , args : any ) =>
127- onRemoveRequest ( args . channel , getChannelsListState , setChannelsListState ) ) ;
114+ _onRemoveRequest ( args . channel , getChannelsListState , setChannelsListState ) ) ;
128115
129116 return ( ) => {
130- accordionItemCommands . forEach ( ( command ) => {
131- command . unhook ( "UI/AccordionItem/OnTitleChanged" ) ;
132- } ) ;
133-
134117 commandsManager . unhookWithinComponent ( channelsCommands . getId ( ) ) ;
135118 } ;
136- } ) ;
119+ } , [ isMounted ( ) ] ) ;
137120
138121 React . useEffect ( ( ) => {
139122 const addChannelCommands = useAnyComponentCommands ( "App/AddChannel" ) ;
0 commit comments