|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { useSyncExternalStore, useState } from "react"; |
3 | 4 | import { useTranslations } from "next-intl"; |
| 5 | +import { CircleArrowUp, RefreshCw } from "lucide-react"; |
4 | 6 |
|
| 7 | +import packageMeta from "@/package.json"; |
| 8 | +import { Button } from "@/components/ui/button"; |
| 9 | +import { |
| 10 | + Dialog, |
| 11 | + DialogClose, |
| 12 | + DialogContent, |
| 13 | + DialogDescription, |
| 14 | + DialogFooter, |
| 15 | + DialogHeader, |
| 16 | + DialogTitle, |
| 17 | +} from "@/components/ui/dialog"; |
| 18 | +import { AdminUpdateTooltipContent } from "@/features/admin/components/admin-update-tooltip-content"; |
| 19 | +import { |
| 20 | + compareReleaseVersions, |
| 21 | + formatReleaseVersion, |
| 22 | + getCachedLatestReleaseSnapshot, |
| 23 | + getServerLatestReleaseSnapshot, |
| 24 | + LATEST_RELEASE_ENDPOINT, |
| 25 | + resolveAvailableRelease, |
| 26 | + subscribeLatestReleaseChange, |
| 27 | + type ReleaseInfo, |
| 28 | + writeCachedLatestRelease, |
| 29 | +} from "@/features/admin/model/update-check"; |
5 | 30 | import { AboutSettingsContent } from "@/shared/components/about-settings-content"; |
| 31 | +import { cn } from "@/lib/utils"; |
| 32 | + |
| 33 | +type GitHubRelease = { |
| 34 | + tag_name?: string; |
| 35 | + html_url?: string; |
| 36 | +}; |
| 37 | + |
| 38 | +type UpdateDialogState = |
| 39 | + | { type: "current" } |
| 40 | + | { type: "available"; release: ReleaseInfo } |
| 41 | + | { type: "failed" }; |
| 42 | + |
| 43 | +function AdminUpdateCheck() { |
| 44 | + const t = useTranslations("adminUsers.aboutPage"); |
| 45 | + const [checking, setChecking] = useState(false); |
| 46 | + const [dialogState, setDialogState] = useState<UpdateDialogState | null>(null); |
| 47 | + |
| 48 | + async function handleCheckUpdate() { |
| 49 | + if (checking) return; |
| 50 | + |
| 51 | + setChecking(true); |
| 52 | + try { |
| 53 | + const response = await fetch(LATEST_RELEASE_ENDPOINT, { |
| 54 | + cache: "no-store", |
| 55 | + headers: { Accept: "application/vnd.github+json" }, |
| 56 | + }); |
| 57 | + |
| 58 | + if (!response.ok) { |
| 59 | + throw new Error(`Release check failed with HTTP ${response.status}`); |
| 60 | + } |
| 61 | + |
| 62 | + const release = (await response.json()) as GitHubRelease; |
| 63 | + const latestVersion = release.tag_name?.trim(); |
| 64 | + const releaseURL = release.html_url?.trim(); |
| 65 | + |
| 66 | + if (!latestVersion || !releaseURL) { |
| 67 | + throw new Error("Latest release payload is incomplete"); |
| 68 | + } |
| 69 | + |
| 70 | + const currentVersion = packageMeta.version; |
| 71 | + const compareResult = compareReleaseVersions(currentVersion, latestVersion); |
| 72 | + |
| 73 | + if (compareResult === "available" || compareResult === "unknown") { |
| 74 | + const release = { version: latestVersion, url: releaseURL }; |
| 75 | + writeCachedLatestRelease(release); |
| 76 | + setDialogState({ type: "available", release }); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + writeCachedLatestRelease({ version: latestVersion, url: releaseURL }); |
| 81 | + setDialogState({ type: "current" }); |
| 82 | + } catch { |
| 83 | + setDialogState({ type: "failed" }); |
| 84 | + } finally { |
| 85 | + setChecking(false); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <> |
| 91 | + <button |
| 92 | + type="button" |
| 93 | + className="inline-flex items-center gap-1 text-xs text-muted-foreground/80 transition-colors hover:text-foreground disabled:cursor-wait disabled:opacity-70" |
| 94 | + onClick={() => void handleCheckUpdate()} |
| 95 | + disabled={checking} |
| 96 | + > |
| 97 | + <RefreshCw className={cn("size-3", checking && "animate-spin")} /> |
| 98 | + <span>{checking ? t("checkingUpdate") : t("checkUpdate")}</span> |
| 99 | + </button> |
| 100 | + <UpdateResultDialog |
| 101 | + state={dialogState} |
| 102 | + onOpenChange={(open) => { |
| 103 | + if (!open) setDialogState(null); |
| 104 | + }} |
| 105 | + onRetry={() => void handleCheckUpdate()} |
| 106 | + /> |
| 107 | + </> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function AdminAboutVersionBadge({ updateRelease }: { updateRelease: ReleaseInfo | null }) { |
| 112 | + const t = useTranslations("adminUsers.aboutPage"); |
| 113 | + const currentVersion = formatReleaseVersion(packageMeta.version); |
| 114 | + |
| 115 | + return ( |
| 116 | + <span className="inline-flex items-center gap-1.5"> |
| 117 | + <span>{currentVersion}</span> |
| 118 | + {updateRelease ? ( |
| 119 | + <CircleArrowUp className="size-3.5 text-rose-500" aria-label={t("updateAvailableIndicator")} /> |
| 120 | + ) : null} |
| 121 | + </span> |
| 122 | + ); |
| 123 | +} |
| 124 | + |
| 125 | +function UpdateResultDialog({ |
| 126 | + state, |
| 127 | + onOpenChange, |
| 128 | + onRetry, |
| 129 | +}: { |
| 130 | + state: UpdateDialogState | null; |
| 131 | + onOpenChange: (open: boolean) => void; |
| 132 | + onRetry: () => void; |
| 133 | +}) { |
| 134 | + const t = useTranslations("adminUsers.aboutPage"); |
| 135 | + const currentVersion = formatReleaseVersion(packageMeta.version); |
| 136 | + const latestVersion = state?.type === "available" ? formatReleaseVersion(state.release.version) : ""; |
| 137 | + |
| 138 | + return ( |
| 139 | + <Dialog open={state !== null} onOpenChange={onOpenChange}> |
| 140 | + <DialogContent className="sm:max-w-[420px]"> |
| 141 | + <DialogHeader> |
| 142 | + <DialogTitle> |
| 143 | + {state?.type === "available" |
| 144 | + ? t("updateDialog.availableTitle") |
| 145 | + : state?.type === "failed" |
| 146 | + ? t("updateDialog.failedTitle") |
| 147 | + : t("updateDialog.currentTitle")} |
| 148 | + </DialogTitle> |
| 149 | + <DialogDescription> |
| 150 | + {state?.type === "available" |
| 151 | + ? t("updateDialog.availableDescription", { current: currentVersion, latest: latestVersion }) |
| 152 | + : state?.type === "failed" |
| 153 | + ? t("updateDialog.failedDescription") |
| 154 | + : t("updateDialog.currentDescription", { current: currentVersion })} |
| 155 | + </DialogDescription> |
| 156 | + </DialogHeader> |
| 157 | + {state?.type === "available" ? ( |
| 158 | + <div className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-2 rounded-md bg-muted/50 px-3 py-2 text-xs"> |
| 159 | + <span className="text-muted-foreground">{t("updateDialog.currentVersion")}</span> |
| 160 | + <span className="font-medium">{currentVersion}</span> |
| 161 | + <span className="text-muted-foreground">{t("updateDialog.latestVersion")}</span> |
| 162 | + <span className="font-medium">{latestVersion}</span> |
| 163 | + </div> |
| 164 | + ) : null} |
| 165 | + <DialogFooter> |
| 166 | + {state?.type === "failed" ? ( |
| 167 | + <Button type="button" variant="ghost" size="sm" onClick={onRetry}> |
| 168 | + {t("updateDialog.retry")} |
| 169 | + </Button> |
| 170 | + ) : null} |
| 171 | + <DialogClose asChild> |
| 172 | + <Button type="button" variant="ghost" size="sm"> |
| 173 | + {t("updateDialog.close")} |
| 174 | + </Button> |
| 175 | + </DialogClose> |
| 176 | + {state?.type === "available" ? ( |
| 177 | + <Button asChild type="button" size="sm"> |
| 178 | + <a href={state.release.url} target="_blank" rel="noopener noreferrer"> |
| 179 | + {t("updateDialog.openRelease")} |
| 180 | + </a> |
| 181 | + </Button> |
| 182 | + ) : null} |
| 183 | + </DialogFooter> |
| 184 | + </DialogContent> |
| 185 | + </Dialog> |
| 186 | + ); |
| 187 | +} |
6 | 188 |
|
7 | 189 | export function AdminAboutPage() { |
8 | 190 | const t = useTranslations("adminUsers.aboutPage"); |
| 191 | + const cachedLatestRelease = useSyncExternalStore( |
| 192 | + subscribeLatestReleaseChange, |
| 193 | + getCachedLatestReleaseSnapshot, |
| 194 | + getServerLatestReleaseSnapshot, |
| 195 | + ); |
| 196 | + const updateRelease = resolveAvailableRelease(packageMeta.version, cachedLatestRelease); |
9 | 197 |
|
10 | 198 | return ( |
11 | 199 | <AboutSettingsContent |
12 | 200 | title={t("title")} |
13 | 201 | description={t("description")} |
14 | 202 | consoleLabel={t("adminConsole")} |
| 203 | + versionBadgeContent={<AdminAboutVersionBadge updateRelease={updateRelease} />} |
| 204 | + versionBadgeTooltip={<AdminUpdateTooltipContent updateRelease={updateRelease} />} |
| 205 | + versionActions={<AdminUpdateCheck />} |
15 | 206 | labels={{ |
16 | 207 | details: t("details"), |
17 | 208 | official: t("official"), |
|
0 commit comments