Download src/hooks/useScrapeQueries.ts from Jessedbrown/daisy-client: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/Jessedbrown/daisy-client/resolve/main/src/hooks/useScrapeQueries.ts
- Command line
-
hf download hf://Jessedbrown/daisy-client/src/hooks/useScrapeQueries.ts
-
curl -L -o useScrapeQueries.ts https://huggingface.co/Jessedbrown/daisy-client/resolve/main/src/hooks/useScrapeQueries.ts
2.35 kB
| import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | |
| import { scrapeApi } from '@/api/scrapeApi'; | |
| import { CACHE_CONFIG } from '@/lib/config'; | |
| import { | |
| handleMutationError, | |
| handleMutationSuccess, | |
| handleQueryError, | |
| } from '@/lib/errors'; | |
| import { QUERY_KEYS } from '@/utils/constants'; | |
| // Query keys for consistent caching | |
| export const scrapeQueryKeys = { | |
| all: [QUERY_KEYS.SCRAPE] as const, | |
| brands: () => [...scrapeQueryKeys.all, 'brands'] as const, | |
| jobs: (params?: { limit?: number; source?: string }) => | |
| [...scrapeQueryKeys.all, 'jobs', params] as const, | |
| stats: () => [...scrapeQueryKeys.all, 'stats'] as const, | |
| }; | |
| // Get available brands for scraping | |
| export const useAvailableBrands = () => { | |
| return useQuery<string[]>({ | |
| queryKey: scrapeQueryKeys.brands(), | |
| queryFn: () => scrapeApi.getAvailableBrands(), | |
| staleTime: CACHE_CONFIG.STALE_TIME, | |
| throwOnError: (error: unknown) => { | |
| handleQueryError(error, 'Loading available brands'); | |
| return false; | |
| }, | |
| }); | |
| }; | |
| // Scrape specific brand mutation | |
| export const useScrapeBrand = () => { | |
| const queryClient = useQueryClient(); | |
| return useMutation({ | |
| mutationFn: (brand: string) => scrapeApi.scrapeBrand(brand), | |
| onSuccess: (data, brand) => { | |
| // Invalidate jobs and stats to show updated data | |
| void queryClient.invalidateQueries({ queryKey: scrapeQueryKeys.jobs() }); | |
| void queryClient.invalidateQueries({ queryKey: scrapeQueryKeys.stats() }); | |
| handleMutationSuccess(`Started scraping ${brand}: ${data.message}`, data); | |
| }, | |
| onError: (error: unknown) => { | |
| handleMutationError(error, 'Failed to start scraping'); | |
| }, | |
| }); | |
| }; | |
| // Scrape all brands mutation | |
| export const useScrapeAll = () => { | |
| const queryClient = useQueryClient(); | |
| return useMutation({ | |
| mutationFn: () => scrapeApi.scrapeAll(), | |
| onSuccess: (data) => { | |
| // Invalidate jobs and stats to show updated data | |
| void queryClient.invalidateQueries({ queryKey: scrapeQueryKeys.jobs() }); | |
| void queryClient.invalidateQueries({ queryKey: scrapeQueryKeys.stats() }); | |
| handleMutationSuccess( | |
| `Started scraping all brands: ${data.message}`, | |
| data | |
| ); | |
| }, | |
| onError: (error: unknown) => { | |
| handleMutationError(error, 'Failed to start scraping all brands'); | |
| }, | |
| }); | |
| }; | |