Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectReactQuery.ts

42 lines1.4 KB

1import type { NativeApi, ProjectSearchEntriesResult } from "@t3tools/contracts";
2import { queryOptions } from "@tanstack/react-query";
3​
4export const projectQueryKeys = {
5 all: ["projects"] as const,
6 searchEntries: (cwd: string | null, query: string, limit: number) =>
7 ["projects", "search-entries", cwd, query, limit] as const,
8};
9​
10const DEFAULT_SEARCH_ENTRIES_LIMIT = 80;
11const DEFAULT_SEARCH_ENTRIES_STALE_TIME = 15_000;
12const EMPTY_SEARCH_ENTRIES_RESULT: ProjectSearchEntriesResult = {
13 entries: [],
14 truncated: false,
15};
16​
17export function projectSearchEntriesQueryOptions(input: {
18 api: NativeApi | undefined;
19 cwd: string | null;
20 query: string;
21 enabled?: boolean;
22 limit?: number;
23 staleTime?: number;
24}) {
25 const limit = input.limit ?? DEFAULT_SEARCH_ENTRIES_LIMIT;
26 return queryOptions({
27 queryKey: projectQueryKeys.searchEntries(input.cwd, input.query, limit),
28 queryFn: async () => {
29 if (!input.api || !input.cwd) {
30 throw new Error("Workspace entry search is unavailable.");
31 }
32 return input.api.projects.searchEntries({
33 cwd: input.cwd,
34 query: input.query,
35 limit,
36 });
37 },
38 enabled: (input.enabled ?? true) && Boolean(input.api && input.cwd),
39 staleTime: input.staleTime ?? DEFAULT_SEARCH_ENTRIES_STALE_TIME,
40 placeholderData: (previous) => previous ?? EMPTY_SEARCH_ENTRIES_RESULT,
41 });