fix: persist channel status filter across page navigation (#5863)
This commit is contained in:
@@ -18,7 +18,12 @@ For commercial licensing, please contact support@quantumnous.com
|
|||||||
*/
|
*/
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { getRouteApi } from '@tanstack/react-router'
|
import { getRouteApi } from '@tanstack/react-router'
|
||||||
import type { OnChangeFn, SortingState, Row } from '@tanstack/react-table'
|
import type {
|
||||||
|
ColumnFiltersState,
|
||||||
|
OnChangeFn,
|
||||||
|
SortingState,
|
||||||
|
Row,
|
||||||
|
} from '@tanstack/react-table'
|
||||||
import { Eye, EyeOff } from 'lucide-react'
|
import { Eye, EyeOff } from 'lucide-react'
|
||||||
import { useState, useMemo, useEffect } from 'react'
|
import { useState, useMemo, useEffect } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
@@ -63,6 +68,7 @@ import { DataTableBulkActions } from './data-table-bulk-actions'
|
|||||||
const route = getRouteApi('/_authenticated/channels/')
|
const route = getRouteApi('/_authenticated/channels/')
|
||||||
const CHANNELS_COLUMN_VISIBILITY_STORAGE_KEY = 'channels:column-visibility'
|
const CHANNELS_COLUMN_VISIBILITY_STORAGE_KEY = 'channels:column-visibility'
|
||||||
const CHANNELS_VIEW_MODE_STORAGE_KEY = 'channels:view-mode'
|
const CHANNELS_VIEW_MODE_STORAGE_KEY = 'channels:view-mode'
|
||||||
|
const CHANNELS_STATUS_FILTER_STORAGE_KEY = 'channel-status-filter'
|
||||||
|
|
||||||
const CHANNEL_SORTABLE_COLUMNS = new Set<ChannelSortBy>([
|
const CHANNEL_SORTABLE_COLUMNS = new Set<ChannelSortBy>([
|
||||||
'id',
|
'id',
|
||||||
@@ -111,13 +117,40 @@ export function ChannelsTable() {
|
|||||||
},
|
},
|
||||||
globalFilter: { enabled: true, key: 'filter' },
|
globalFilter: { enabled: true, key: 'filter' },
|
||||||
columnFilters: [
|
columnFilters: [
|
||||||
{ columnId: 'status', searchKey: 'status', type: 'array' },
|
{
|
||||||
|
columnId: 'status',
|
||||||
|
searchKey: 'status',
|
||||||
|
type: 'array',
|
||||||
|
deserialize: (value) => {
|
||||||
|
if (value !== undefined) return value
|
||||||
|
const stored = localStorage.getItem(
|
||||||
|
CHANNELS_STATUS_FILTER_STORAGE_KEY
|
||||||
|
)
|
||||||
|
return stored === 'enabled' || stored === 'disabled' ? [stored] : []
|
||||||
|
},
|
||||||
|
},
|
||||||
{ columnId: 'type', searchKey: 'type', type: 'array' },
|
{ columnId: 'type', searchKey: 'type', type: 'array' },
|
||||||
{ columnId: 'group', searchKey: 'group', type: 'array' },
|
{ columnId: 'group', searchKey: 'group', type: 'array' },
|
||||||
{ columnId: 'model', searchKey: 'model', type: 'string' },
|
{ columnId: 'model', searchKey: 'model', type: 'string' },
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleColumnFiltersChange: OnChangeFn<ColumnFiltersState> = (
|
||||||
|
updater
|
||||||
|
) => {
|
||||||
|
onColumnFiltersChange((previous) => {
|
||||||
|
const next = typeof updater === 'function' ? updater(previous) : updater
|
||||||
|
const status = next.find((f) => f.id === 'status')?.value as
|
||||||
|
| string[]
|
||||||
|
| undefined
|
||||||
|
localStorage.setItem(
|
||||||
|
CHANNELS_STATUS_FILTER_STORAGE_KEY,
|
||||||
|
status?.[0] ?? 'all'
|
||||||
|
)
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Extract filters from column filters
|
// Extract filters from column filters
|
||||||
const statusFilter =
|
const statusFilter =
|
||||||
(columnFilters.find((f) => f.id === 'status')?.value as string[]) || []
|
(columnFilters.find((f) => f.id === 'status')?.value as string[]) || []
|
||||||
@@ -290,7 +323,7 @@ export function ChannelsTable() {
|
|||||||
? (row: Row<Channel>) => !isTagAggregateRow(row.original)
|
? (row: Row<Channel>) => !isTagAggregateRow(row.original)
|
||||||
: false,
|
: false,
|
||||||
onSortingChange: handleSortingChange,
|
onSortingChange: handleSortingChange,
|
||||||
onColumnFiltersChange,
|
onColumnFiltersChange: handleColumnFiltersChange,
|
||||||
onPaginationChange,
|
onPaginationChange,
|
||||||
onGlobalFilterChange,
|
onGlobalFilterChange,
|
||||||
getSubRows: (row: Channel & { children?: Channel[] }) => row.children,
|
getSubRows: (row: Channel & { children?: Channel[] }) => row.children,
|
||||||
|
|||||||
Reference in New Issue
Block a user