From a93a0094143dd226564904fd65898675b48aefa4 Mon Sep 17 00:00:00 2001 From: Mateusz Bacherycz Date: Wed, 27 Nov 2024 15:52:21 +0100 Subject: [PATCH] draft: hiding filters showing no results (not working) --- packages/frontend/src/pages/Inbox/Inbox.tsx | 18 ++++++++--- packages/frontend/src/pages/Inbox/filters.ts | 32 +++++++++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/packages/frontend/src/pages/Inbox/Inbox.tsx b/packages/frontend/src/pages/Inbox/Inbox.tsx index e4c788c3..1f22c4b6 100644 --- a/packages/frontend/src/pages/Inbox/Inbox.tsx +++ b/packages/frontend/src/pages/Inbox/Inbox.tsx @@ -102,6 +102,19 @@ export const Inbox = ({ viewType }: InboxProps) => { return filterDialogs(dataSource, activeFilters, format); }, [dataSource, activeFilters]); + const filteredDialogs = useMemo(() => { + return filterDialogs(dataSource, activeFilters, format); + }, [dataSource, activeFilters]); + + // const filterBarSettings = useMemo(() => { + // return getFilterBarSettings(dataSource, filteredDialogs, format); + // }, [dataSource, filteredDialogs]); + + const filterBarSettings = useMemo( + () => getFilterBarSettings(dataSource, filteredDialogs, format).filter((setting) => setting.options.length > 1), + [dataSource, format], + ); + // biome-ignore lint/correctness/useExhaustiveDependencies: Full control of what triggers this code is needed useEffect(() => { setActiveFilters([]); @@ -198,11 +211,6 @@ export const Inbox = ({ viewType }: InboxProps) => { })); }; - const filterBarSettings = useMemo( - () => getFilterBarSettings(dataSource, format).filter((setting) => setting.options.length > 1), - [dataSource, format], - ); - const savedSearchDisabled = !activeFilters?.length && !searchString; const showFilterButton = filterBarSettings.length > 0; diff --git a/packages/frontend/src/pages/Inbox/filters.ts b/packages/frontend/src/pages/Inbox/filters.ts index 91a9064b..b0787f5e 100644 --- a/packages/frontend/src/pages/Inbox/filters.ts +++ b/packages/frontend/src/pages/Inbox/filters.ts @@ -74,7 +74,11 @@ export const filterDialogs = ( }); }; -export const getFilterBarSettings = (dialogs: InboxItemInput[], format: FormatFunction): FilterSetting[] => { +export const getFilterBarSettings = ( + dialogs: InboxItemInput[], + filteredDialogs: InboxItemInput[], + format: FormatFunction, +): FilterSetting[] => { return [ { id: 'sender', @@ -85,7 +89,11 @@ export const getFilterBarSettings = (dialogs: InboxItemInput[], format: FormatFu options: (() => { const senders = dialogs.map((p) => p.sender.name); const senderCounts = countOccurrences(senders); - return Array.from(new Set(senders)).map((sender) => ({ + + const validSenders = filteredDialogs.map((p) => p.sender.name); + const uniqueSenders = Array.from(new Set(validSenders)); + + return uniqueSenders.map((sender) => ({ displayLabel: `${t('filter_bar_fields.from')} ${sender}`, value: sender, count: senderCounts[sender], @@ -101,7 +109,11 @@ export const getFilterBarSettings = (dialogs: InboxItemInput[], format: FormatFu options: (() => { const receivers = dialogs.map((p) => p.receiver.name); const receiversCount = countOccurrences(receivers); - return Array.from(new Set(receivers)).map((receiver) => ({ + + const validReceivers = filteredDialogs.map((p) => p.receiver.name); + const uniqueReceivers = Array.from(new Set(validReceivers)); + + return uniqueReceivers.map((receiver) => ({ displayLabel: `${t('filter_bar_fields.to')} ${receiver}`, value: receiver, count: receiversCount[receiver], @@ -116,12 +128,16 @@ export const getFilterBarSettings = (dialogs: InboxItemInput[], format: FormatFu operation: 'includes', horizontalRule: true, options: (() => { - const status = dialogs.map((p) => p.status); - const statusCount = countOccurrences(status); - return Array.from(new Set(status)).map((statusLabel) => ({ + const statuses = dialogs.map((p) => p.status); + const statusCounts = countOccurrences(statuses); + + const validStatuses = filteredDialogs.map((p) => p.status); + const uniqueStatuses = Array.from(new Set(validStatuses)); + + return uniqueStatuses.map((statusLabel) => ({ displayLabel: t(`status.${statusLabel.toLowerCase()}`), value: statusLabel, - count: statusCount[statusLabel], + count: statusCounts[statusLabel], })); })(), }, @@ -132,7 +148,7 @@ export const getFilterBarSettings = (dialogs: InboxItemInput[], format: FormatFu unSelectedLabel: t('filter_bar.label.all_dates'), operation: 'equals', options: generateDateOptions( - dialogs.map((p) => new Date(p.updatedAt)), + filteredDialogs.map((p) => new Date(p.updatedAt)), format, ), },