|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { Cancel01Icon } from "@hugeicons/core-free-icons"; |
| 6 | +import { HugeiconsIcon } from "@hugeicons/react"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { getSortedReleases } from "../utils"; |
| 9 | +import type { Release } from "../utils"; |
| 10 | + |
| 11 | +const STORAGE_KEY = "last-seen-version"; |
| 12 | + |
| 13 | +export function ChangelogNotification() { |
| 14 | + const [release, setRelease] = useState<Release | null>(null); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const releases = getSortedReleases(); |
| 18 | + const latest = releases[0]; |
| 19 | + if (!latest) return; |
| 20 | + |
| 21 | + let storedVersion: string | null = null; |
| 22 | + try { |
| 23 | + storedVersion = localStorage.getItem(STORAGE_KEY); |
| 24 | + } catch { |
| 25 | + // localStorage unavailable |
| 26 | + } |
| 27 | + |
| 28 | + // First-time visitor: record the version silently, nothing to announce. |
| 29 | + if (storedVersion === null) { |
| 30 | + try { |
| 31 | + localStorage.setItem(STORAGE_KEY, latest.version); |
| 32 | + } catch { |
| 33 | + // ignore |
| 34 | + } |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Already seen this version. |
| 39 | + if (storedVersion === latest.version) return; |
| 40 | + |
| 41 | + // New version since last visit: record it and show the notification. |
| 42 | + try { |
| 43 | + localStorage.setItem(STORAGE_KEY, latest.version); |
| 44 | + } catch { |
| 45 | + // ignore |
| 46 | + } |
| 47 | + |
| 48 | + setRelease(latest); |
| 49 | + }, []); |
| 50 | + |
| 51 | + if (!release) return null; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="fixed bottom-5 left-5 z-50 flex w-72 flex-col gap-3 rounded-xl border bg-card p-4 shadow-lg"> |
| 55 | + <div className="flex items-start justify-between gap-2"> |
| 56 | + <div className="flex flex-col gap-1"> |
| 57 | + <span className="text-sm font-semibold leading-snug"> |
| 58 | + {release.title} |
| 59 | + </span> |
| 60 | + <span className="text-xs text-muted-foreground"> |
| 61 | + v{release.version} |
| 62 | + </span> |
| 63 | + </div> |
| 64 | + <Button |
| 65 | + variant="ghost" |
| 66 | + size="icon" |
| 67 | + className="-mr-1 -mt-1 shrink-0" |
| 68 | + onClick={() => setRelease(null)} |
| 69 | + aria-label="Dismiss" |
| 70 | + > |
| 71 | + <HugeiconsIcon icon={Cancel01Icon} className="size-4" /> |
| 72 | + </Button> |
| 73 | + </div> |
| 74 | + |
| 75 | + {release.summary && ( |
| 76 | + <p className="text-xs leading-relaxed text-muted-foreground"> |
| 77 | + {release.summary} |
| 78 | + </p> |
| 79 | + )} |
| 80 | + |
| 81 | + <div className="flex justify-end"> |
| 82 | + <Button asChild size="sm"> |
| 83 | + <Link href="/changelog" onClick={() => setRelease(null)}> |
| 84 | + See full changelog |
| 85 | + </Link> |
| 86 | + </Button> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
0 commit comments