|
| 1 | +import { Checkbox } from "@/components/ui/checkbox"; |
| 2 | +import { Label } from "@/components/ui/label"; |
| 3 | +import { QuestionDifficulty } from "@/gql/graphql"; |
| 4 | +import { FilterIcon } from "lucide-react"; |
| 5 | +import { type Difficulty, difficultyTranslation, type SolvedStatus, solvedStatusTranslation } from "../model"; |
| 6 | + |
| 7 | +export interface TagState { |
| 8 | + solvedStatus: SolvedStatus[]; |
| 9 | + difficulty: Difficulty[]; |
| 10 | +} |
| 11 | + |
| 12 | +export interface TagFilterSectionProps { |
| 13 | + value: TagState; |
| 14 | + onChange: (tags: TagState) => void; |
| 15 | +} |
| 16 | + |
| 17 | +export default function TagFilterSection({ |
| 18 | + value, |
| 19 | + onChange, |
| 20 | +}: TagFilterSectionProps) { |
| 21 | + const getSolvedStatus = (solvedStatus: SolvedStatus) => { |
| 22 | + return value.solvedStatus.includes(solvedStatus); |
| 23 | + }; |
| 24 | + |
| 25 | + const getDifficulty = (difficulty: Difficulty) => { |
| 26 | + return value.difficulty.includes(difficulty); |
| 27 | + }; |
| 28 | + |
| 29 | + const handleDifficultyChange = (difficulty: Difficulty) => { |
| 30 | + return (checked: boolean) => { |
| 31 | + onChange({ |
| 32 | + ...value, |
| 33 | + difficulty: checked |
| 34 | + ? [...value.difficulty, difficulty] |
| 35 | + : value.difficulty.filter((d) => d !== difficulty), |
| 36 | + }); |
| 37 | + }; |
| 38 | + }; |
| 39 | + |
| 40 | + const handleSolvedStatusChange = (status: SolvedStatus) => { |
| 41 | + return (checked: boolean) => { |
| 42 | + onChange({ |
| 43 | + ...value, |
| 44 | + solvedStatus: checked |
| 45 | + ? [...value.solvedStatus, status] |
| 46 | + : value.solvedStatus.filter((s) => s !== status), |
| 47 | + }); |
| 48 | + }; |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="space-y-3"> |
| 53 | + <label className="flex items-center gap-2 font-bold"> |
| 54 | + <FilterIcon className="size-4" /> |
| 55 | + 標籤 |
| 56 | + </label> |
| 57 | + |
| 58 | + <div className="mb-4 space-y-2 text-sm text-muted-foreground"> |
| 59 | + 解題狀態 |
| 60 | + <div className="mt-2 space-y-2"> |
| 61 | + <TagCheckbox |
| 62 | + tag="solved" |
| 63 | + checked={getSolvedStatus("solved")} |
| 64 | + onChange={handleSolvedStatusChange("solved")} |
| 65 | + translation={solvedStatusTranslation} |
| 66 | + /> |
| 67 | + <TagCheckbox |
| 68 | + tag="unsolved" |
| 69 | + checked={getSolvedStatus("unsolved")} |
| 70 | + onChange={handleSolvedStatusChange("unsolved")} |
| 71 | + translation={solvedStatusTranslation} |
| 72 | + /> |
| 73 | + <TagCheckbox |
| 74 | + tag="not-tried" |
| 75 | + checked={getSolvedStatus("not-tried")} |
| 76 | + onChange={handleSolvedStatusChange("not-tried")} |
| 77 | + translation={solvedStatusTranslation} |
| 78 | + /> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div className="space-y-2 text-sm text-muted-foreground"> |
| 83 | + 難度 |
| 84 | + <div className="mt-2 space-y-2"> |
| 85 | + <TagCheckbox |
| 86 | + tag={QuestionDifficulty.Easy} |
| 87 | + checked={getDifficulty(QuestionDifficulty.Easy)} |
| 88 | + onChange={handleDifficultyChange(QuestionDifficulty.Easy)} |
| 89 | + translation={difficultyTranslation} |
| 90 | + /> |
| 91 | + <TagCheckbox |
| 92 | + tag={QuestionDifficulty.Medium} |
| 93 | + checked={getDifficulty(QuestionDifficulty.Medium)} |
| 94 | + onChange={handleDifficultyChange(QuestionDifficulty.Medium)} |
| 95 | + translation={difficultyTranslation} |
| 96 | + /> |
| 97 | + <TagCheckbox |
| 98 | + tag={QuestionDifficulty.Hard} |
| 99 | + checked={getDifficulty(QuestionDifficulty.Hard)} |
| 100 | + onChange={handleDifficultyChange(QuestionDifficulty.Hard)} |
| 101 | + translation={difficultyTranslation} |
| 102 | + /> |
| 103 | + <TagCheckbox |
| 104 | + tag={QuestionDifficulty.Unspecified} |
| 105 | + checked={getDifficulty(QuestionDifficulty.Unspecified)} |
| 106 | + onChange={handleDifficultyChange(QuestionDifficulty.Unspecified)} |
| 107 | + translation={difficultyTranslation} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +function TagCheckbox<T extends string>({ |
| 116 | + tag, |
| 117 | + checked, |
| 118 | + onChange, |
| 119 | + translation, |
| 120 | +}: { |
| 121 | + tag: T; |
| 122 | + checked: boolean; |
| 123 | + onChange: (checked: boolean) => void; |
| 124 | + translation: Record<T, string>; |
| 125 | +}) { |
| 126 | + return ( |
| 127 | + <div className="flex items-center gap-2"> |
| 128 | + <Checkbox id={tag} checked={checked} onCheckedChange={onChange} /> |
| 129 | + <Label htmlFor={tag}>{translation[tag]}</Label> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +} |
0 commit comments