Skip to content

Commit

Permalink
add functionality to show analyzed channels on heatmap filter by chan…
Browse files Browse the repository at this point in the history
…nels
  • Loading branch information
zuies committed Dec 21, 2023
1 parent bf2655c commit a9644ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/components/pages/pageIndex/HeatmapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
88 changes: 62 additions & 26 deletions src/context/ChannelContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ interface ChannelContextProps {
refreshData: (
platformId: string,
property?: 'channel',
selectedChannels?: string[]
selectedChannels?: string[],
hideDeactiveSubchannels?: boolean
) => Promise<Channel[] | void>;
handleSubChannelChange: (channelId: string, subChannelId: string) => void;
handleSelectAll: (channelId: string, subChannels: SubChannel[]) => void;
updateSelectedSubChannels: (
allChannels: Channel[],
newSelectedSubChannels: string[]
newSelectedSubChannels: string[],
hideDeactiveSubchannels?: boolean
) => void;
}

Expand All @@ -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
) => {},
};

Expand All @@ -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) => {
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit a9644ba

Please sign in to comment.