diff --git a/src/components/chuni/top-rating-sidebar.tsx b/src/components/chuni/top-rating-sidebar.tsx index ed35db0..9ce6726 100644 --- a/src/components/chuni/top-rating-sidebar.tsx +++ b/src/components/chuni/top-rating-sidebar.tsx @@ -4,9 +4,14 @@ import { ChuniTopRating, ChuniTopRatingProps } from '@/components/chuni/top-rati import { getUserRating } from '@/actions/chuni/profile'; import { useState } from 'react'; import { Button, ButtonGroup } from '@nextui-org/react'; +import { useBreakpoint } from '@/helpers/use-breakpoint'; export const ChuniTopRatingSidebar = ({ rating }: { rating: Awaited> }) => { const [shownRating, setShownRating] = useState<'top' | 'recent' | null>('recent'); + const breakpoint = useBreakpoint(); + + if (![undefined, 'sm'].includes(breakpoint) && shownRating === null) + setShownRating('recent'); return (
diff --git a/src/helpers/use-breakpoint.ts b/src/helpers/use-breakpoint.ts new file mode 100644 index 0000000..3d50e9c --- /dev/null +++ b/src/helpers/use-breakpoint.ts @@ -0,0 +1,18 @@ +import resolveConfig from 'tailwindcss/resolveConfig'; +import tailwindConfig from '@/../tailwind.base'; +import { useMediaQuery } from 'usehooks-ts'; + +const config = resolveConfig(tailwindConfig); +const breakpoints = Object.entries(config.theme.screens) + .sort(([, a], [, b]) => parseInt(a) - parseInt(b)); + +export const useBreakpoint = () => { + let activeName: string | undefined; + for (const [name, width] of breakpoints) { + // eslint-disable-next-line react-hooks/rules-of-hooks + if (useMediaQuery(`(min-width: ${width})`)) + activeName = name; + } + + return activeName; +};