From bf2655c690d0c7237f548af4fde703e7979b1a13 Mon Sep 17 00:00:00 2001 From: zuies Date: Thu, 21 Dec 2023 12:52:53 +0300 Subject: [PATCH 1/2] update channel context --- src/context/ChannelContext.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/context/ChannelContext.tsx b/src/context/ChannelContext.tsx index e5206606..4d90f2b0 100644 --- a/src/context/ChannelContext.tsx +++ b/src/context/ChannelContext.tsx @@ -28,7 +28,7 @@ interface ChannelContextProps { }; refreshData: ( platformId: string, - property?: 'channel' | 'role', + property?: 'channel', selectedChannels?: string[] ) => Promise; handleSubChannelChange: (channelId: string, subChannelId: string) => void; @@ -53,7 +53,7 @@ const initialChannelContextData: ChannelContextProps = { selectedSubChannels: initialSelectedSubChannels, refreshData: async ( platformId: string, - property?: 'channel' | 'role', + property?: 'channel', selectedChannels?: string[] ) => {}, handleSubChannelChange: (channelId: string, subChannelId: string) => {}, @@ -78,7 +78,7 @@ export const ChannelProvider = ({ children }: ChannelProviderProps) => { const refreshData = useCallback( async ( platformId: string, - property: 'channel' | 'role' = 'channel', + property: 'channel' = 'channel', selectedChannels?: string[] ) => { setLoading(true); From a9644baaa30c0fa84cad2b7975ed7d695d5a6c96 Mon Sep 17 00:00:00 2001 From: zuies Date: Thu, 21 Dec 2023 21:02:29 +0300 Subject: [PATCH 2/2] add functionality to show analyzed channels on heatmap filter by channels --- .../pages/pageIndex/HeatmapChart.tsx | 2 +- src/context/ChannelContext.tsx | 88 +++++++++++++------ 2 files changed, 63 insertions(+), 27 deletions(-) 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 4d90f2b0..a789f5e8 100644 --- a/src/context/ChannelContext.tsx +++ b/src/context/ChannelContext.tsx @@ -29,13 +29,15 @@ interface ChannelContextProps { refreshData: ( platformId: string, property?: 'channel', - selectedChannels?: string[] + 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; } @@ -54,13 +56,15 @@ const initialChannelContextData: ChannelContextProps = { refreshData: async ( platformId: string, property?: 'channel', - selectedChannels?: string[] + selectedChannels?: string[], + hideDeactiveSubchannels?: boolean ) => {}, handleSubChannelChange: (channelId: string, subChannelId: string) => {}, handleSelectAll: (channelId: string, subChannels: SubChannel[]) => {}, updateSelectedSubChannels: ( allChannels: Channel[], - newSelectedSubChannels: string[] + newSelectedSubChannels: string[], + hideDeactiveSubchannels?: boolean ) => {}, }; @@ -79,14 +83,19 @@ export const ChannelProvider = ({ children }: ChannelProviderProps) => { async ( platformId: string, property: 'channel' = 'channel', - selectedChannels?: string[] + 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 = {