Skip to content

Commit

Permalink
Merge pull request #228 from TogetherCrew/fix/filter-channels
Browse files Browse the repository at this point in the history
Fix/filter channels
  • Loading branch information
mehdi-torabiv authored Dec 21, 2023
2 parents 06b7630 + a9644ba commit bb5a9fc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 30 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
94 changes: 65 additions & 29 deletions src/context/ChannelContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ interface ChannelContextProps {
};
refreshData: (
platformId: string,
property?: 'channel' | 'role',
selectedChannels?: string[]
property?: 'channel',
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 @@ -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
) => {},
};

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

Please sign in to comment.