fix(compiler): migrate template-version-history to useQuery
set-state-in-effect: 45 → 44. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
import { useState } from 'react';
|
||||||
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
import { RotateCcw, Clock } from 'lucide-react';
|
import { RotateCcw, Clock } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
@@ -25,29 +26,25 @@ export function TemplateVersionHistory({
|
|||||||
currentVersion,
|
currentVersion,
|
||||||
onRollback,
|
onRollback,
|
||||||
}: TemplateVersionHistoryProps) {
|
}: TemplateVersionHistoryProps) {
|
||||||
const [versions, setVersions] = useState<TemplateVersion[]>([]);
|
const queryClient = useQueryClient();
|
||||||
const [loading, setLoading] = useState(true);
|
const queryKey = ['admin', 'template-versions', templateId] as const;
|
||||||
const [rollingBack, setRollingBack] = useState<number | null>(null);
|
const [rollingBack, setRollingBack] = useState<number | null>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
const fetchVersions = useCallback(async () => {
|
const {
|
||||||
setLoading(true);
|
data: versions = [],
|
||||||
setError(null);
|
isLoading: loading,
|
||||||
try {
|
error: queryError,
|
||||||
const res = await apiFetch<{ data: TemplateVersion[] }>(
|
} = useQuery<TemplateVersion[]>({
|
||||||
`/api/v1/admin/templates/${templateId}/versions`,
|
queryKey,
|
||||||
);
|
queryFn: () =>
|
||||||
setVersions(res.data);
|
apiFetch<{ data: TemplateVersion[] }>(`/api/v1/admin/templates/${templateId}/versions`).then(
|
||||||
} catch (err: unknown) {
|
(r) => r.data,
|
||||||
setError(err instanceof Error ? err.message : 'Failed to load versions');
|
),
|
||||||
} finally {
|
});
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [templateId]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
const fetchVersions = () => queryClient.invalidateQueries({ queryKey });
|
||||||
void fetchVersions();
|
const effectiveError = error ?? (queryError instanceof Error ? queryError.message : null);
|
||||||
}, [fetchVersions]);
|
|
||||||
|
|
||||||
async function handleRollback(version: number) {
|
async function handleRollback(version: number) {
|
||||||
if (
|
if (
|
||||||
@@ -92,8 +89,10 @@ export function TemplateVersionHistory({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{error && (
|
{effectiveError && (
|
||||||
<p className="rounded bg-destructive/10 px-3 py-2 text-sm text-destructive">{error}</p>
|
<p className="rounded bg-destructive/10 px-3 py-2 text-sm text-destructive">
|
||||||
|
{effectiveError}
|
||||||
|
</p>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<p className="text-sm text-muted-foreground">
|
<p className="text-sm text-muted-foreground">
|
||||||
|
|||||||
Reference in New Issue
Block a user