diff --git a/src/components/pages/pageIndex/HeatmapChart.tsx b/src/components/pages/pageIndex/HeatmapChart.tsx index 55a15eca..b81366eb 100644 --- a/src/components/pages/pageIndex/HeatmapChart.tsx +++ b/src/components/pages/pageIndex/HeatmapChart.tsx @@ -154,7 +154,7 @@ const HeatmapChart = () => { const { metadata } = data; if (metadata) { const { selectedChannels } = metadata; - await refreshData(platformId, 'channel', selectedChannels); + await refreshData(platformId, 'channel', selectedChannels, true); } else { await refreshData(platformId); } diff --git a/src/context/ChannelContext.tsx b/src/context/ChannelContext.tsx index e5206606..a789f5e8 100644 --- a/src/context/ChannelContext.tsx +++ b/src/context/ChannelContext.tsx @@ -28,14 +28,16 @@ interface ChannelContextProps { }; refreshData: ( platformId: string, - property?: 'channel' | 'role', - selectedChannels?: string[] + property?: 'channel', + selectedChannels?: string[], + hideDeactiveSubchannels?: boolean ) => Promise; handleSubChannelChange: (channelId: string, subChannelId: string) => void; handleSelectAll: (channelId: string, subChannels: SubChannel[]) => void; updateSelectedSubChannels: ( allChannels: Channel[], - newSelectedSubChannels: string[] + newSelectedSubChannels: string[], + hideDeactiveSubchannels?: boolean ) => void; } @@ -53,14 +55,16 @@ const initialChannelContextData: ChannelContextProps = { selectedSubChannels: initialSelectedSubChannels, refreshData: async ( platformId: string, - property?: 'channel' | 'role', - selectedChannels?: string[] + property?: 'channel', + selectedChannels?: string[], + hideDeactiveSubchannels?: boolean ) => {}, handleSubChannelChange: (channelId: string, subChannelId: string) => {}, handleSelectAll: (channelId: string, subChannels: SubChannel[]) => {}, updateSelectedSubChannels: ( allChannels: Channel[], - newSelectedSubChannels: string[] + newSelectedSubChannels: string[], + hideDeactiveSubchannels?: boolean ) => {}, }; @@ -78,15 +82,20 @@ export const ChannelProvider = ({ children }: ChannelProviderProps) => { const refreshData = useCallback( async ( platformId: string, - property: 'channel' | 'role' = 'channel', - selectedChannels?: string[] + property: 'channel' = 'channel', + selectedChannels?: string[], + hideDeactiveSubchannels: boolean = false ) => { setLoading(true); try { const data = await retrievePlatformProperties({ property, platformId }); setChannels(data); if (selectedChannels) { - updateSelectedSubChannels(data, selectedChannels); + updateSelectedSubChannels( + data, + selectedChannels, + hideDeactiveSubchannels + ); } else { const newSelectedSubChannels = data.reduce( (acc: any, channel: any) => { @@ -148,30 +157,57 @@ export const ChannelProvider = ({ children }: ChannelProviderProps) => { const updateSelectedSubChannels = ( allChannels: Channel[], - newSelectedSubChannels: string[] + newSelectedSubChannels: string[], + hideDeactiveSubchannels: boolean = false ) => { - setSelectedSubChannels((prevSelectedSubChannels: SelectedSubChannels) => { - const updatedSelectedSubChannels: SelectedSubChannels = { - ...prevSelectedSubChannels, - }; - - allChannels?.forEach((channel) => { - const channelUpdates: { [subChannelId: string]: boolean } = {}; - - channel?.subChannels?.forEach((subChannel) => { - if (subChannel.canReadMessageHistoryAndViewChannel) { - channelUpdates[subChannel.channelId] = - newSelectedSubChannels.includes(subChannel.channelId); - } else { - channelUpdates[subChannel.channelId] = false; - } + if (hideDeactiveSubchannels) { + const filteredChannels = allChannels + .map((channel) => ({ + ...channel, + subChannels: channel.subChannels.filter((subChannel) => + newSelectedSubChannels.includes(subChannel.channelId) + ), + })) + .filter((channel) => channel.subChannels.length > 0); + + setChannels(filteredChannels); + + setSelectedSubChannels( + filteredChannels.reduce((acc: any, channel: any) => { + acc[channel.channelId] = channel.subChannels.reduce( + (subAcc: any, subChannel: any) => { + subAcc[subChannel.channelId] = true; + return subAcc; + }, + {} + ); + return acc; + }, {}) + ); + } else { + setSelectedSubChannels((prevSelectedSubChannels: SelectedSubChannels) => { + const updatedSelectedSubChannels: SelectedSubChannels = { + ...prevSelectedSubChannels, + }; + + allChannels?.forEach((channel) => { + const channelUpdates: { [subChannelId: string]: boolean } = {}; + + channel?.subChannels?.forEach((subChannel) => { + if (subChannel.canReadMessageHistoryAndViewChannel) { + channelUpdates[subChannel.channelId] = + newSelectedSubChannels.includes(subChannel.channelId); + } else { + channelUpdates[subChannel.channelId] = false; + } + }); + + updatedSelectedSubChannels[channel.channelId] = channelUpdates; }); - updatedSelectedSubChannels[channel.channelId] = channelUpdates; + return updatedSelectedSubChannels; }); - - return updatedSelectedSubChannels; - }); + } }; const value = {