fix: (web) prevent flickering during refresh

- Track isInitialLoading separately from isRefreshing
- Only show skeleton on initial page load (when quests is empty)
- During refresh, keep existing content visible
- Spinning refresh icon indicates refresh in progress without clearing table
This commit is contained in:
syntaxbullet
2026-01-16 15:22:28 +01:00
parent 7d658bbef9
commit 5491551544
2 changed files with 25 additions and 16 deletions

View File

@@ -16,7 +16,8 @@ interface QuestListItem {
interface QuestTableProps { interface QuestTableProps {
quests: QuestListItem[]; quests: QuestListItem[];
isLoading: boolean; isInitialLoading: boolean;
isRefreshing: boolean;
onRefresh?: () => void; onRefresh?: () => void;
} }
@@ -211,14 +212,16 @@ function QuestTableContent({ quests }: { quests: QuestListItem[] }) {
); );
} }
export function QuestTable({ quests, isLoading, onRefresh }: QuestTableProps) { export function QuestTable({ quests, isInitialLoading, isRefreshing, onRefresh }: QuestTableProps) {
const showSkeleton = isInitialLoading && quests.length === 0;
return ( return (
<Card className="glass-card overflow-hidden"> <Card className="glass-card overflow-hidden">
<CardHeader className="pb-3"> <CardHeader className="pb-3">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<CardTitle className="text-xl font-bold text-primary">Quest Inventory</CardTitle> <CardTitle className="text-xl font-bold text-primary">Quest Inventory</CardTitle>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
{isLoading ? ( {showSkeleton ? (
<Badge variant="secondary" className="animate-pulse"> <Badge variant="secondary" className="animate-pulse">
Loading... Loading...
</Badge> </Badge>
@@ -229,10 +232,10 @@ export function QuestTable({ quests, isLoading, onRefresh }: QuestTableProps) {
)} )}
<button <button
onClick={onRefresh} onClick={onRefresh}
disabled={isLoading} disabled={isRefreshing}
className={cn( className={cn(
"p-2 rounded-md hover:bg-muted/50 transition-colors", "p-2 rounded-md hover:bg-muted/50 transition-colors",
isLoading && "cursor-wait" isRefreshing && "cursor-wait"
)} )}
title="Refresh quests" title="Refresh quests"
> >
@@ -248,7 +251,7 @@ export function QuestTable({ quests, isLoading, onRefresh }: QuestTableProps) {
strokeLinejoin="round" strokeLinejoin="round"
className={cn( className={cn(
"text-muted-foreground transition-transform", "text-muted-foreground transition-transform",
isLoading && "animate-spin" isRefreshing && "animate-spin"
)} )}
> >
<path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /> <path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
@@ -261,7 +264,7 @@ export function QuestTable({ quests, isLoading, onRefresh }: QuestTableProps) {
</div> </div>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{isLoading ? ( {showSkeleton ? (
<QuestTableSkeleton /> <QuestTableSkeleton />
) : ( ) : (
<QuestTableContent quests={quests} /> <QuestTableContent quests={quests} />

View File

@@ -15,12 +15,16 @@ interface QuestListItem {
export function AdminQuests() { export function AdminQuests() {
const [quests, setQuests] = React.useState<QuestListItem[]>([]); const [quests, setQuests] = React.useState<QuestListItem[]>([]);
const [isLoading, setIsLoading] = React.useState(true); const [isInitialLoading, setIsInitialLoading] = React.useState(true);
const [refreshKey, setRefreshKey] = React.useState(0); const [isRefreshing, setIsRefreshing] = React.useState(false);
const [lastCreatedQuestId, setLastCreatedQuestId] = React.useState<number | null>(null); const [lastCreatedQuestId, setLastCreatedQuestId] = React.useState<number | null>(null);
const fetchQuests = React.useCallback(async () => { const fetchQuests = React.useCallback(async (isRefresh = false) => {
setIsLoading(true); if (isRefresh) {
setIsRefreshing(true);
} else {
setIsInitialLoading(true);
}
try { try {
const response = await fetch("/api/quests"); const response = await fetch("/api/quests");
if (!response.ok) { if (!response.ok) {
@@ -36,12 +40,13 @@ export function AdminQuests() {
description: error instanceof Error ? error.message : "Unknown error", description: error instanceof Error ? error.message : "Unknown error",
}); });
} finally { } finally {
setIsLoading(false); setIsInitialLoading(false);
setIsRefreshing(false);
} }
}, []); }, []);
React.useEffect(() => { React.useEffect(() => {
fetchQuests(); fetchQuests(false);
}, [fetchQuests]); }, [fetchQuests]);
React.useEffect(() => { React.useEffect(() => {
@@ -59,7 +64,7 @@ export function AdminQuests() {
}, [lastCreatedQuestId, quests]); }, [lastCreatedQuestId, quests]);
const handleQuestCreated = () => { const handleQuestCreated = () => {
fetchQuests(); fetchQuests(true);
toast.success("Quest list updated", { toast.success("Quest list updated", {
description: "The quest inventory has been refreshed.", description: "The quest inventory has been refreshed.",
}); });
@@ -76,8 +81,9 @@ export function AdminQuests() {
<div className="animate-in fade-in slide-in-from-bottom-4 duration-700"> <div className="animate-in fade-in slide-in-from-bottom-4 duration-700">
<QuestTable <QuestTable
quests={quests} quests={quests}
isLoading={isLoading} isInitialLoading={isInitialLoading}
onRefresh={fetchQuests} isRefreshing={isRefreshing}
onRefresh={() => fetchQuests(true)}
/> />
</div> </div>