Files
tweakcn/hooks/use-font-search.ts
Sahaj JainandLuis Llanes 8c107dd53e Feature/dynamic fonts (#197)
* chore: allow overriding ControlSection classNames

* feat: Support Google Fonts API and load fonts dynamically

* feat: Enforce System Prompt to use Google Fonts API if needed

* styles: Improve GoogleFontPicker styles

* feat: Pull Fonts from fileSystem instead of hitting the Google Fonts API every time

* feat: Improve System Prompt to reference Google Fonts

* refactor: Use Next.js Cache instead of manually fetching and reading from the filesystem

* feat: Only enable Font picker fetch when it's open

* chore: Remove JSON for Google Fonts catalogue

* feat: Scroll to selected font if it's already fetched and add clean search button

* chore: update imports

* fix: dashboard preview

---------

Co-authored-by: Luis Llanes <luisllaboj@gmail.com>
2025-07-27 23:40:36 +05:30

49 lines
1.2 KiB
TypeScript

import { PaginatedFontsResponse, type FontCategory } from "@/types/fonts";
import { useInfiniteQuery } from "@tanstack/react-query";
export type FilterFontCategory = "all" | FontCategory;
interface UseFontSearchParams {
query: string;
category?: FilterFontCategory;
limit?: number;
enabled?: boolean;
}
export function useFontSearch({
query,
category = "all",
limit = 20,
enabled = true,
}: UseFontSearchParams) {
return useInfiniteQuery({
queryKey: ["fonts", query, category],
queryFn: async ({ pageParam }) => {
const offset = pageParam || 0;
const searchParams = new URLSearchParams({
q: query,
limit: limit.toString(),
offset: offset.toString(),
});
if (category && category !== "all") {
searchParams.append("category", category);
}
const response = await fetch(`/api/google-fonts?${searchParams}`);
if (!response.ok) {
throw new Error("Failed to fetch fonts");
}
return response.json() as Promise<PaginatedFontsResponse>;
},
initialPageParam: 0,
getNextPageParam: (lastPage) => {
return lastPage.hasMore ? lastPage.offset + lastPage.limit : undefined;
},
staleTime: 1000 * 60 * 60 * 24, // 1 day
enabled,
});
}