"use client"; import { useRouter } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { SnowflakeIcon, SparklesIcon } from "@/components/icons"; import { useI18n } from "@/lib/i18n"; import { cn } from "@/lib/utils"; export type StudyMode = "deck" | "homework"; /** Canonical URL per mode, so a deep link always lands on the right screen. */ export const MODE_HREF: Record = { deck: "/create?mode=deck", homework: "/homework?mode=homework", }; /** How long the ice pill takes to slide before the route follows. */ const SLIDE_MS = 180; /** * Compact two-segment mode switch (Study Deck / Homework Solver). The ice pill * slides to the chosen segment and the router follows a moment later, so the * toggle reads as one smooth transition instead of a hard jump. The current * mode stays pressed; the other segment navigates. */ export function ModeSwitch({ current }: { current: StudyMode }) { const { t } = useI18n(); const router = useRouter(); const [pending, setPending] = useState(null); const timer = useRef(null); useEffect( () => () => { if (timer.current !== null) window.clearTimeout(timer.current); }, [], ); const choose = (mode: StudyMode) => { if (mode === current || pending !== null) return; setPending(mode); const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches; timer.current = window.setTimeout(() => router.push(MODE_HREF[mode]), reduce ? 0 : SLIDE_MS + 10); }; const shown = pending ?? current; const segment = (mode: StudyMode, icon: React.ReactNode, label: string) => ( ); return (
{segment("deck", , t("actionStudyDeck"))} {segment("homework", , t("actionHomework"))}
); }