UX polish pass 2: toasts, optimistic updates, mod update detection

- Install sonner; <Toaster> mounted in Providers, auto-tracks theme.
- Toasts replace inline result Alerts across ServerControls, PlayerManager,
  BackupManager, ModManager (install/remove/restore/delete/start/stop).
- PlayerManager: optimistic op/deop/whitelist/ban/pardon via onMutate +
  rollback; UI updates instantly before RCON round-trip.
- Modrinth search results now show author + "updated Xd ago" with full
  timestamp on hover; downloads on its own row.
- New /api/mods/updates endpoint: per-installed-mod Modrinth latest-version
  lookup (parallel, 30min memo). Amber "Update available" badge rendered
  next to installed mod rows when filenames differ.
- PlayerAvatar + Modrinth icons migrated to next/image (unoptimized, size
  hints) — fewer layout shifts.
- Login page surfaces ?error= + NextAuth error codes (CredentialsSignin,
  SessionRequired, etc.), preserves callbackUrl, adds autocomplete hints
  and role="alert". Wrapped in Suspense per Next 16 requirement.
- Snapshots + backups show relative "Xh ago" with exact timestamp on hover
  via new lib/time.ts helper.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
hurkicorgi 2026-04-13 05:11:17 -06:00
parent 6c91f7fef0
commit f9ae1afac1
12 changed files with 334 additions and 76 deletions

View file

@ -2,12 +2,15 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useState, useCallback, useRef, useEffect } from "react";
import Image from "next/image";
import { toast } from "sonner";
import { CheckCircle2, XCircle, Circle } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import { timeAgo } from "@/lib/time";
import {
Card,
CardContent,
@ -36,6 +39,9 @@ type SearchResult = {
icon_url: string;
downloads: number;
project_id: string;
author: string;
date_modified: string;
follows: number;
};
type ModDownload = {
@ -133,6 +139,17 @@ export function ModManager() {
staleTime: 30_000,
});
// Updates available (from Modrinth)
const { data: updates = [] } = useQuery<{ filename: string; latestFilename: string; dateModified: string }[]>({
queryKey: ["mod-updates"],
queryFn: () =>
fetch("/api/mods/updates").then((r) => (r.ok ? r.json() : [])),
staleTime: 15 * 60 * 1000,
refetchOnMount: false,
});
const updateMap = new Map(updates.map((u) => [u.filename, u]));
// Search (debounced)
const [debouncedQuery, setDebouncedQuery] = useState("");
@ -258,6 +275,11 @@ export function ModManager() {
if (data.installed?.length) {
setNewlyInstalled(new Set(data.installed));
}
toast.success(data.message || "Mods installed");
} else {
toast.error(data.message || "Install failed", {
description: data.rolledBack ? "Changes rolled back." : undefined,
});
}
queryClient.invalidateQueries({ queryKey: ["mods"] });
queryClient.invalidateQueries({ queryKey: ["status"] });
@ -267,6 +289,7 @@ export function ModManager() {
setInstallStatus("");
setTimelineSteps([]);
setInstallResult({ success: false, message: err.message });
toast.error("Install failed", { description: err.message });
},
});
@ -283,10 +306,12 @@ export function ModManager() {
},
onSuccess: (data) => {
setInstallResult(data);
(data.success ? toast.success : toast.error)(data.message || "Mod removed");
queryClient.invalidateQueries({ queryKey: ["mods"] });
queryClient.invalidateQueries({ queryKey: ["status"] });
queryClient.invalidateQueries({ queryKey: ["snapshots"] });
},
onError: (err) => toast.error("Remove failed", { description: err.message }),
});
// Restore snapshot
@ -302,10 +327,12 @@ export function ModManager() {
},
onSuccess: (data) => {
setInstallResult(data);
(data.success ? toast.success : toast.error)(data.message || "Snapshot restored");
queryClient.invalidateQueries({ queryKey: ["mods"] });
queryClient.invalidateQueries({ queryKey: ["status"] });
queryClient.invalidateQueries({ queryKey: ["snapshots"] });
},
onError: (err) => toast.error("Restore failed", { description: err.message }),
});
// Delete snapshot
@ -318,8 +345,10 @@ export function ModManager() {
});
},
onSuccess: () => {
toast.success("Snapshot deleted");
queryClient.invalidateQueries({ queryKey: ["snapshots"] });
},
onError: (err) => toast.error("Delete failed", { description: err.message }),
});
const toggleSelect = useCallback((result: SearchResult) => {
@ -488,30 +517,42 @@ export function ModManager() {
}`}
>
{result.icon_url ? (
<img
<Image
src={result.icon_url}
alt=""
width={40}
height={40}
loading="lazy"
decoding="async"
className="w-10 h-10 rounded-md shrink-0 bg-muted"
unoptimized
className="w-10 h-10 rounded-md shrink-0 bg-muted object-cover"
/>
) : (
<div className="w-10 h-10 rounded-md bg-muted shrink-0" />
)}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium truncate">
{result.title}
</span>
<span className="text-xs text-muted-foreground shrink-0">
{formatDownloads(result.downloads)} downloads
</span>
{result.author && (
<span className="text-xs text-muted-foreground shrink-0">
by {result.author}
</span>
)}
</div>
<p className="text-xs text-muted-foreground truncate">
{result.description}
</p>
<div className="flex items-center gap-2 mt-0.5 text-[10px] text-muted-foreground/80 tabular-nums">
<span>{formatDownloads(result.downloads)} downloads</span>
{result.date_modified && (
<>
<span aria-hidden>·</span>
<span title={new Date(result.date_modified).toLocaleString()}>
updated {timeAgo(result.date_modified)}
</span>
</>
)}
</div>
</div>
{isSelected && (
<Badge variant="default" className="shrink-0 text-xs">
@ -766,6 +807,15 @@ export function ModManager() {
New
</Badge>
)}
{updateMap.has(mod.filename) && (
<Badge
variant="outline"
className="text-xs px-1.5 py-0 border-amber-500/40 text-amber-300"
title={`Latest: ${updateMap.get(mod.filename)?.latestFilename}`}
>
Update available
</Badge>
)}
</div>
<p className="text-xs text-muted-foreground truncate">
{mod.filename} {mod.size}
@ -838,8 +888,11 @@ export function ModManager() {
{snap.modCount} mods
</Badge>
</div>
<p className="text-xs text-muted-foreground">
{new Date(snap.createdAt).toLocaleString()}
<p
className="text-xs text-muted-foreground"
title={new Date(snap.createdAt).toLocaleString()}
>
{timeAgo(snap.createdAt)}
</p>
</div>
<div className="flex gap-1 shrink-0 flex-wrap justify-end">