Skip to content

Commit f2100d1

Browse files
committed
fix: skip search fetch when query is blank, keep dir error in own row
- Add enabled flag to useRegistryQuery so the search route no longer briefly flashes a loading skeleton (and no unsafe double-cast) when q is empty; the empty state renders immediately - Give dirError a full-width basis and let the files row wrap so the error message drops below both panes instead of forming a third column
1 parent 08c4dd1 commit f2100d1

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎apps/web/app/components/cnpm/FilesView.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function FilesView({ pkgName, spec }: { pkgName: string; spec: string })
101101
}
102102

103103
return (
104-
<div className="flex flex-col gap-4 md:flex-row">
104+
<div className="flex flex-col gap-4 md:flex-row md:flex-wrap">
105105
<div className="max-h-[70vh] w-full overflow-auto rounded-lg border bg-muted/30 p-2 md:max-w-sm">
106106
<FileNode
107107
entries={root.files}
@@ -126,7 +126,7 @@ export function FilesView({ pkgName, spec }: { pkgName: string; spec: string })
126126
{dirError && (
127127
<p
128128
role="status"
129-
className="mt-2 rounded-md border border-destructive/40 bg-destructive/10 px-2 py-1 text-xs text-destructive"
129+
className="w-full basis-full rounded-md border border-destructive/40 bg-destructive/10 px-2 py-1 text-xs text-destructive"
130130
>
131131
目录加载失败:{dirError},请重新展开重试
132132
</p>

‎apps/web/app/lib/registry/client.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ export async function getFileContent(pkg: string, spec: string, path: string) {
9191
return res.text();
9292
}
9393

94-
export function useRegistryQuery<T>(fetcher: () => Promise<T>, deps: unknown[]) {
94+
export function useRegistryQuery<T>(
95+
fetcher: () => Promise<T>,
96+
deps: unknown[],
97+
enabled = true,
98+
) {
9599
const [data, setData] = useState<T | null>(null);
96100
const [error, setError] = useState<RegistryError | null>(null);
97101
const [loading, setLoading] = useState(true);
@@ -100,6 +104,12 @@ export function useRegistryQuery<T>(fetcher: () => Promise<T>, deps: unknown[])
100104
fetcherRef.current = fetcher;
101105

102106
useEffect(() => {
107+
if (!enabled) {
108+
setLoading(false);
109+
setError(null);
110+
setData(null);
111+
return;
112+
}
103113
let cancelled = false;
104114
setLoading(true);
105115
setError(null);
@@ -125,7 +135,7 @@ export function useRegistryQuery<T>(fetcher: () => Promise<T>, deps: unknown[])
125135
cancelled = true;
126136
};
127137
// eslint-disable-next-line react-hooks/exhaustive-deps
128-
}, [...deps, attempt]);
138+
}, [...deps, attempt, enabled]);
129139

130140
return { data, error, loading, retry: () => setAttempt((value) => value + 1) };
131141
}

‎apps/web/app/routes/cnpm.search.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ export default function CnpmSearch() {
3434
const from = (page - 1) * PAGE_SIZE;
3535

3636
const { data, error, loading, retry } = useRegistryQuery(
37-
() =>
38-
q
39-
? searchPackages(q, from, PAGE_SIZE)
40-
: Promise.resolve({ objects: [], total: 0 } as unknown as Awaited<ReturnType<typeof searchPackages>>),
37+
() => searchPackages(q, from, PAGE_SIZE),
4138
[q, from],
39+
Boolean(q),
4240
);
4341

4442
return (

0 commit comments

Comments
 (0)