|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState } from "react" |
| 4 | +import CodeMirror from "@uiw/react-codemirror" |
| 5 | +import { vscodeDark } from "@uiw/codemirror-theme-vscode" |
| 6 | +import { cn } from "@/lib/utils" |
| 7 | +import { getLanguageExtension, type Language } from "@/lib/languages" |
| 8 | +import { Button } from "@/components/ui/button" |
| 9 | +import type { Snippet } from "@/types/snippet" |
| 10 | + |
| 11 | +interface ReviewCardProps { |
| 12 | + snippet: Snippet |
| 13 | + index: number |
| 14 | + total: number |
| 15 | + onRate: (rating: 1 | 3 | 5) => void |
| 16 | + isSubmitting: boolean |
| 17 | +} |
| 18 | + |
| 19 | +export function ReviewCard({ snippet, index, total, onRate, isSubmitting }: ReviewCardProps) { |
| 20 | + const [revealed, setReveal] = useState(false) |
| 21 | + |
| 22 | + return ( |
| 23 | + <div className="flex flex-col gap-4"> |
| 24 | + |
| 25 | + {/* ── Progress ─────────────────────────────────────── */} |
| 26 | + <div className="flex items-center gap-3"> |
| 27 | + <div className="h-1.5 flex-1 rounded-full bg-border overflow-hidden"> |
| 28 | + <div |
| 29 | + className="h-full rounded-full bg-primary transition-[width] duration-500" |
| 30 | + style={{ width: `${(index / total) * 100}%` }} |
| 31 | + /> |
| 32 | + </div> |
| 33 | + <span className="shrink-0 font-mono text-xs tabular-nums text-muted-foreground"> |
| 34 | + {index + 1} / {total} |
| 35 | + </span> |
| 36 | + </div> |
| 37 | + |
| 38 | + {/* ── Card ─────────────────────────────────────────── */} |
| 39 | + <div className="rounded-2xl border border-border bg-card overflow-hidden"> |
| 40 | + |
| 41 | + {/* Header */} |
| 42 | + <div className="px-6 pt-6 pb-4 border-b border-border"> |
| 43 | + <div className="flex flex-wrap items-center gap-1.5 mb-3"> |
| 44 | + <span className="rounded-md bg-muted/60 px-2 py-0.5 font-mono text-[11px] text-muted-foreground"> |
| 45 | + {snippet.language} |
| 46 | + </span> |
| 47 | + {snippet.tags.map((tag) => ( |
| 48 | + <span |
| 49 | + key={tag} |
| 50 | + className="rounded-md bg-primary/10 px-2 py-0.5 text-[11px] font-medium text-primary" |
| 51 | + > |
| 52 | + {tag} |
| 53 | + </span> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + |
| 57 | + <h2 className="font-heading text-xl font-semibold tracking-tight text-foreground text-wrap-balance"> |
| 58 | + {snippet.title} |
| 59 | + </h2> |
| 60 | + |
| 61 | + {snippet.description && ( |
| 62 | + <p className="mt-2 text-sm text-muted-foreground leading-relaxed"> |
| 63 | + {snippet.description} |
| 64 | + </p> |
| 65 | + )} |
| 66 | + </div> |
| 67 | + |
| 68 | + {/* Code area */} |
| 69 | + {snippet.code && ( |
| 70 | + <div className="relative"> |
| 71 | + {/* Blurred overlay when not revealed */} |
| 72 | + {!revealed && ( |
| 73 | + <div className="absolute inset-0 z-10 flex flex-col items-center justify-center gap-3 bg-card/70 backdrop-blur-sm"> |
| 74 | + <Button |
| 75 | + variant="outline" |
| 76 | + onClick={() => setReveal(true)} |
| 77 | + className="active:scale-[0.96] transition-[transform,opacity] duration-100 shadow-sm" |
| 78 | + > |
| 79 | + Show answer |
| 80 | + </Button> |
| 81 | + <p className="text-xs text-muted-foreground"> |
| 82 | + Try to recall the code first |
| 83 | + </p> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + |
| 87 | + <div className={cn("min-h-48 transition-[filter] duration-300", !revealed && "blur-sm select-none pointer-events-none")}> |
| 88 | + <CodeMirror |
| 89 | + value={snippet.code} |
| 90 | + theme={vscodeDark} |
| 91 | + extensions={[ |
| 92 | + ...(getLanguageExtension(snippet.language as Language) |
| 93 | + ? [getLanguageExtension(snippet.language as Language)!] |
| 94 | + : []), |
| 95 | + ]} |
| 96 | + editable={false} |
| 97 | + basicSetup={{ |
| 98 | + lineNumbers: true, |
| 99 | + foldGutter: false, |
| 100 | + highlightActiveLine: false, |
| 101 | + autocompletion: false, |
| 102 | + }} |
| 103 | + className="text-sm" |
| 104 | + style={{ fontFamily: "var(--font-mono, ui-monospace, monospace)" }} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + |
| 111 | + {/* ── Rating buttons ───────────────────────────────── */} |
| 112 | + {revealed && ( |
| 113 | + <div className="flex flex-col gap-2"> |
| 114 | + <p className="text-center text-xs text-muted-foreground"> |
| 115 | + How well did you remember this? |
| 116 | + </p> |
| 117 | + <div className="grid grid-cols-3 gap-2"> |
| 118 | + <RatingButton |
| 119 | + label="Forgot" |
| 120 | + sublabel="Start over" |
| 121 | + onClick={() => onRate(1)} |
| 122 | + disabled={isSubmitting} |
| 123 | + variant="forgot" |
| 124 | + /> |
| 125 | + <RatingButton |
| 126 | + label="Hard" |
| 127 | + sublabel="Got it, barely" |
| 128 | + onClick={() => onRate(3)} |
| 129 | + disabled={isSubmitting} |
| 130 | + variant="hard" |
| 131 | + /> |
| 132 | + <RatingButton |
| 133 | + label="Easy" |
| 134 | + sublabel="Got it!" |
| 135 | + onClick={() => onRate(5)} |
| 136 | + disabled={isSubmitting} |
| 137 | + variant="easy" |
| 138 | + /> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +function RatingButton({ |
| 147 | + label, |
| 148 | + sublabel, |
| 149 | + onClick, |
| 150 | + disabled, |
| 151 | + variant, |
| 152 | +}: { |
| 153 | + label: string |
| 154 | + sublabel: string |
| 155 | + onClick: () => void |
| 156 | + disabled: boolean |
| 157 | + variant: "forgot" | "hard" | "easy" |
| 158 | +}) { |
| 159 | + const styles = { |
| 160 | + forgot: "border-destructive/30 bg-destructive/8 text-destructive hover:bg-destructive/15", |
| 161 | + hard: "border-pulse/30 bg-pulse/8 text-pulse hover:bg-pulse/15", |
| 162 | + easy: "border-primary/30 bg-primary/8 text-primary hover:bg-primary/15", |
| 163 | + } |
| 164 | + |
| 165 | + return ( |
| 166 | + <button |
| 167 | + type="button" |
| 168 | + onClick={onClick} |
| 169 | + disabled={disabled} |
| 170 | + className={cn( |
| 171 | + "flex flex-col items-center gap-0.5 rounded-xl border px-3 py-3.5", |
| 172 | + "active:scale-[0.96] transition-[transform,background-color,opacity] duration-100", |
| 173 | + "disabled:pointer-events-none disabled:opacity-50", |
| 174 | + styles[variant], |
| 175 | + )} |
| 176 | + > |
| 177 | + <span className="text-sm font-semibold">{label}</span> |
| 178 | + <span className="text-[11px] opacity-70">{sublabel}</span> |
| 179 | + </button> |
| 180 | + ) |
| 181 | +} |
0 commit comments