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

105 lines
3.7 KiB
TypeScript
Raw Normal View History

import { ChangeEvent, MutableRefObject, useEffect, useRef } from 'react';
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';
import { useTranslation } from 'react-i18next';
2023-07-19 20:04:56 -07:00
import { LibraryItem } from '/@/renderer/api/types';
2023-03-31 05:22:51 -07:00
import { PageHeader, SearchInput } from '/@/renderer/components';
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';
import { SongListFilter, useCurrentServer } from '/@/renderer/store';
2023-02-05 22:41:47 -08:00
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
2023-09-23 15:36:57 -07:00
import { VirtualInfiniteGridRef } from '/@/renderer/components/virtual-grid';
import { useDisplayRefresh } from '/@/renderer/hooks/use-display-refresh';
2022-12-19 15:59:14 -08:00
2022-12-27 13:52:50 -08:00
interface SongListHeaderProps {
genreId?: string;
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
}
export const SongListHeader = ({
genreId,
gridRef,
title,
itemCount,
tableRef,
}: SongListHeaderProps) => {
const { t } = useTranslation();
2023-07-01 19:10:05 -07:00
const server = useCurrentServer();
const cq = useContainerQuery();
const genreRef = useRef<string>();
2022-12-27 13:52:50 -08:00
const { customFilters, filter, handlePlay, refresh, search } = useDisplayRefresh({
gridRef,
2023-07-19 20:04:56 -07:00
itemType: LibraryItem.SONG,
server,
tableRef,
2023-07-19 20:04:56 -07:00
});
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
const handleSearch = debounce((e: ChangeEvent<HTMLInputElement>) => {
const updatedFilters = search(e) as SongListFilter;
2023-07-19 20:04:56 -07:00
const filterWithCustom = {
...updatedFilters,
...customFilters,
};
refresh(filterWithCustom);
2023-07-01 19:10:05 -07:00
}, 500);
2022-12-27 13:52:50 -08:00
useEffect(() => {
if (genreRef.current && genreRef.current !== genreId) {
refresh(customFilters);
}
genreRef.current = genreId;
}, [customFilters, genreId, refresh, tableRef]);
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
};