feishin/src/renderer/features/songs/components/song-list-header.tsx

103 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-12-27 13:52:50 -08:00
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
2022-12-28 01:23:54 -08:00
import { Flex, Group, Stack } from '@mantine/core';
2022-12-27 13:52:50 -08:00
import debounce from 'lodash/debounce';
2023-07-19 20:04:56 -07:00
import { ChangeEvent, MutableRefObject } from 'react';
import { useTranslation } from 'react-i18next';
2023-07-19 20:04:56 -07:00
import { useListStoreByKey } from '../../../store/list.store';
import { LibraryItem } from '/@/renderer/api/types';
2023-03-31 05:22:51 -07:00
import { PageHeader, SearchInput } from '/@/renderer/components';
2023-07-19 20:04:56 -07:00
import { useListContext } from '/@/renderer/context/list-context';
2023-03-31 05:22:51 -07:00
import { FilterBar, LibraryHeaderBar } from '/@/renderer/features/shared';
2023-02-05 22:41:47 -08:00
import { SongListHeaderFilters } from '/@/renderer/features/songs/components/song-list-header-filters';
2022-12-27 13:52:50 -08:00
import { useContainerQuery } from '/@/renderer/hooks';
2023-07-19 20:04:56 -07:00
import { useListFilterRefresh } from '/@/renderer/hooks/use-list-filter-refresh';
import { SongListFilter, useCurrentServer, useListStoreActions } from '/@/renderer/store';
2023-02-05 22:41:47 -08:00
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
2023-07-19 20:04:56 -07:00
import { ListDisplayType } from '/@/renderer/types';
2023-09-23 15:36:57 -07:00
import { VirtualInfiniteGridRef } from '/@/renderer/components/virtual-grid';
2022-12-19 15:59:14 -08:00
2022-12-27 13:52:50 -08:00
interface SongListHeaderProps {
2023-09-23 15:36:57 -07:00
gridRef: MutableRefObject<VirtualInfiniteGridRef | null>;
2023-07-01 19:10:05 -07:00
itemCount?: number;
tableRef: MutableRefObject<AgGridReactType | null>;
title?: string;
2022-12-27 13:52:50 -08:00
}
2023-09-23 15:36:57 -07:00
export const SongListHeader = ({ gridRef, title, itemCount, tableRef }: SongListHeaderProps) => {
const { t } = useTranslation();
2023-07-01 19:10:05 -07:00
const server = useCurrentServer();
2023-07-19 20:04:56 -07:00
const { pageKey, handlePlay, customFilters } = useListContext();
2023-07-01 19:10:05 -07:00
const { setFilter, setTablePagination } = useListStoreActions();
2023-07-19 20:04:56 -07:00
const { display, filter } = useListStoreByKey({ key: pageKey });
2023-07-01 19:10:05 -07:00
const cq = useContainerQuery();
2022-12-27 13:52:50 -08:00
2023-09-25 15:57:48 -07:00
const { handleRefreshTable, handleRefreshGrid } = useListFilterRefresh({
2023-07-19 20:04:56 -07:00
itemType: LibraryItem.SONG,
server,
});
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
const handleSearch = debounce((e: ChangeEvent<HTMLInputElement>) => {
const searchTerm = e.target.value === '' ? undefined : e.target.value;
const updatedFilters = setFilter({
data: { searchTerm },
itemType: LibraryItem.SONG,
key: pageKey,
}) as SongListFilter;
2023-07-19 20:04:56 -07:00
const filterWithCustom = {
...updatedFilters,
...customFilters,
};
if (display === ListDisplayType.TABLE || display === ListDisplayType.TABLE_PAGINATED) {
handleRefreshTable(tableRef, filterWithCustom);
setTablePagination({ data: { currentPage: 0 }, key: pageKey });
} else {
2023-09-25 15:57:48 -07:00
handleRefreshGrid(gridRef, filterWithCustom);
2023-07-19 20:04:56 -07:00
}
2023-07-01 19:10:05 -07:00
}, 500);
2022-12-27 13:52:50 -08:00
2023-07-01 19:10:05 -07:00
const playButtonBehavior = usePlayButtonBehavior();
2023-07-01 19:10:05 -07:00
return (
<Stack
ref={cq.ref}
spacing={0}
2022-12-19 15:59:14 -08:00
>
2023-07-01 19:10:05 -07:00
<PageHeader backgroundColor="var(--titlebar-bg)">
<Flex
justify="space-between"
w="100%"
>
<LibraryHeaderBar>
<LibraryHeaderBar.PlayButton
onClick={() => handlePlay?.({ playType: playButtonBehavior })}
/>
<LibraryHeaderBar.Title>
{title || t('page.trackList.title', { postProcess: 'titleCase' })}
</LibraryHeaderBar.Title>
2023-07-01 19:10:05 -07:00
<LibraryHeaderBar.Badge
isLoading={itemCount === null || itemCount === undefined}
>
{itemCount}
</LibraryHeaderBar.Badge>
</LibraryHeaderBar>
<Group>
<SearchInput
defaultValue={filter.searchTerm}
openedWidth={cq.isMd ? 250 : cq.isSm ? 200 : 150}
onChange={handleSearch}
/>
</Group>
</Flex>
</PageHeader>
<FilterBar>
2023-09-23 15:36:57 -07:00
<SongListHeaderFilters
gridRef={gridRef}
tableRef={tableRef}
/>
2023-07-01 19:10:05 -07:00
</FilterBar>
</Stack>
);
2022-12-19 15:59:14 -08:00
};