|
1 | 1 | import { useQuery } from '@tanstack/react-query' |
| 2 | +import { useMemo } from 'react' |
2 | 3 | import { config } from '@/lib/config' |
3 | 4 | import { Area, type Island } from '@/lib/const' |
4 | 5 | import { getData } from '@/lib/data' |
| 6 | +import { useMapDashboard } from './useDashboard' |
5 | 7 |
|
6 | 8 | const MAX_PAGE_SIZE = config.dataSource.area.pagination.maxPageSize |
7 | 9 |
|
@@ -29,10 +31,73 @@ async function fetchIslandsRecursively( |
29 | 31 | return [] |
30 | 32 | } |
31 | 33 |
|
32 | | -export function useIslands(regencyCode?: string) { |
33 | | - return useQuery({ |
34 | | - queryKey: ['islands', { parentCode: regencyCode }], |
35 | | - queryFn: () => fetchIslandsRecursively(regencyCode as string), |
| 34 | +export function useIslands() { |
| 35 | + const { selectedArea } = useMapDashboard() |
| 36 | + const provinceCode = selectedArea.province?.code |
| 37 | + const regencyCode = selectedArea.regency?.code |
| 38 | + |
| 39 | + // Load all islands in a province that does not belong to any regency |
| 40 | + // Fetch the full list of islands that don't belong to any regency once and |
| 41 | + // cache it. We only enable this when a province is selected to avoid |
| 42 | + // fetching unnecessarily on initial load. Different provinces will reuse |
| 43 | + // the same cached list and we filter it locally instead of refetching. |
| 44 | + const allNoRegencyQuery = useQuery<Island[], Error>({ |
| 45 | + queryKey: ['islands', 'without-regency'], |
| 46 | + queryFn: () => fetchIslandsRecursively(''), |
| 47 | + enabled: !!provinceCode, |
| 48 | + // keep it fresh for a while so switching provinces won't trigger refetch |
| 49 | + staleTime: 1000 * 60 * 60, // 1 hour |
| 50 | + }) |
| 51 | + |
| 52 | + // derive province-level islands from the cached full list |
| 53 | + const provinceIslands = useMemo(() => { |
| 54 | + if (!provinceCode) return [] |
| 55 | + const all: Island[] = (allNoRegencyQuery.data as Island[]) ?? [] |
| 56 | + return all.filter((island: Island) => |
| 57 | + island.code.startsWith(`${provinceCode}.00`), |
| 58 | + ) |
| 59 | + }, [allNoRegencyQuery.data, provinceCode]) |
| 60 | + |
| 61 | + // Load islands for a specific regency |
| 62 | + const regencyQuery = useQuery<Island[], Error>({ |
| 63 | + queryKey: ['islands', 'regency', regencyCode], |
| 64 | + queryFn: () => |
| 65 | + regencyCode ? fetchIslandsRecursively(regencyCode) : Promise.resolve([]), |
36 | 66 | enabled: !!regencyCode, |
37 | 67 | }) |
| 68 | + |
| 69 | + // Compute aggregated islands from province + regency query results |
| 70 | + const islands = useMemo(() => { |
| 71 | + const prov = provinceIslands ?? [] |
| 72 | + const reg = regencyQuery.data ?? [] |
| 73 | + const map = new Map<string, Island>() |
| 74 | + |
| 75 | + // add province islands first |
| 76 | + prov.forEach((i) => { |
| 77 | + map.set(i.code, i) |
| 78 | + }) |
| 79 | + // overlay regency islands (will replace duplicates) |
| 80 | + reg.forEach((i) => { |
| 81 | + map.set(i.code, i) |
| 82 | + }) |
| 83 | + |
| 84 | + return Array.from(map.values()) |
| 85 | + }, [provinceIslands, regencyQuery.data]) |
| 86 | + |
| 87 | + const isLoading = allNoRegencyQuery.isLoading || regencyQuery.isLoading |
| 88 | + const isFetching = allNoRegencyQuery.isFetching || regencyQuery.isFetching |
| 89 | + const aggregatedError = [regencyQuery.error, allNoRegencyQuery.error].filter( |
| 90 | + Boolean, |
| 91 | + ) |
| 92 | + |
| 93 | + return { |
| 94 | + data: islands, |
| 95 | + isLoading, |
| 96 | + isFetching, |
| 97 | + error: aggregatedError.length > 0 ? aggregatedError : null, |
| 98 | + refetch: () => { |
| 99 | + void allNoRegencyQuery.refetch() |
| 100 | + void regencyQuery.refetch() |
| 101 | + }, |
| 102 | + } |
38 | 103 | } |
0 commit comments