Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search panel for subscribe page #249

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components/calendar-tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Props {
tabListItems: CalendarDataKey[];
tabListProps?: TabListProps;
boxProps?: BoxProps;
type?: 'subscribe';
}

export default function CalendarTab({
Expand All @@ -16,6 +17,7 @@ export default function CalendarTab({
tabListItems,
tabListProps,
boxProps,
type,
...props
}: Props & TabsProps) {
const engToZh: Record<CalendarDataKey, string> = {
Expand All @@ -40,6 +42,11 @@ export default function CalendarTab({
{engToZh[week]}
</Tab>
))}
{type === 'subscribe' ? (
<Tab whiteSpace="nowrap" mb="-2px" key="search">
搜索
</Tab>
) : null}
</TabList>
<Box top="46.5px" borderBottom="2px solid" mt="-2.5px" borderBottomColor="inherit" {...boxProps} />

Expand Down
38 changes: 34 additions & 4 deletions src/pages/subscribe/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useMemo, useReducer } from 'react';
import { useMemo, useReducer, useState } from 'react';

import { CiFilter } from 'react-icons/ci';
import { Button, Flex, Menu, MenuButton, MenuItem, MenuList, Spinner } from '@chakra-ui/react';
import { Box, Button, Flex, Input, Menu, MenuButton, MenuItem, MenuList, Spinner, TabPanel } from '@chakra-ui/react';

import { useCalendar } from '~/hooks/use-calendar';
import { useColorMode } from '~/hooks/use-color-mode';

import Auth from '~/components/auth';
import CalendarTab from '~/components/calendar-tab';
import SubscribePanel from '~/components/subscribe-panel';
import Auth from '~/components/auth';

import type { Calendar, CalendarDataEntries, CalendarDataKey } from '~/types/calendar';
import type { Calendar, CalendarDataEntries, CalendarDataKey, WeekCalendar } from '~/types/calendar';

interface FilterOptionsState {
subscribed: boolean;
Expand Down Expand Up @@ -122,6 +122,8 @@ export default function Subscribe() {
const tabListItems = useMemo(() => Object.keys(calendarData ?? []) as CalendarDataKey[], [calendarData]);
const tabPanelsItems = useMemo(() => Object.entries(calendarData ?? []) as CalendarDataEntries, [calendarData]);

const searchData = useMemo(() => tabPanelsItems.flatMap(([_, bangumis]) => bangumis ?? []), [tabPanelsItems]);

if (!calendarData || tabListItems.length === 0 || tabPanelsItems.length === 0) {
return (
<Flex justifyContent="center" alignContent="center" mt="44">
Expand All @@ -137,11 +139,39 @@ export default function Subscribe() {
tabListItems={tabListItems}
tabListProps={{ mr: '16' }}
boxProps={{ mr: '16' }}
type="subscribe"
>
{tabPanelsItems.map(([week, bangumis]) => (
<SubscribePanel key={week} bangumis={bangumis} />
))}
<TabPanel mt="6">
<SearchPanel data={searchData} />
</TabPanel>
</CalendarTab>
</Auth>
);
}

function SearchPanel({ data }: { data: WeekCalendar[] }) {
const [key, setKey] = useState('');

const filterData = useMemo(() => {
return data.sort(b => (!b.status ? 1 : -1)).filter(_d => _d.name.includes(key));
}, [data, key]);

return (
<>
<Input
placeholder="搜索动画片"
maxW="96"
value={key}
display="block"
mx="auto"
onChange={e => setKey(e.target.value)}
/>
<Box mt="8">
<SubscribePanel bangumis={filterData} />
</Box>
</>
);
}
Loading