|
| 1 | +import useSWR, { SWRConfiguration } from "swr"; |
| 2 | +import { useApi } from "@/shared/lib/hooks/useApi"; |
| 3 | +import { useCallback } from "react"; |
| 4 | +import { showErrorToast, showSuccessToast } from "@/shared/ui"; |
| 5 | +import { ColumnConfiguration } from "./types"; |
| 6 | +import { useRevalidateMultiple } from "@/shared/lib/state-utils"; |
| 7 | + |
| 8 | +type UsePresetColumnConfigOptions = { |
| 9 | + presetId?: string; |
| 10 | + enabled?: boolean; // Flag to control whether to fetch |
| 11 | +} & SWRConfiguration; |
| 12 | + |
| 13 | +const DEFAULT_COLUMN_CONFIG: ColumnConfiguration = { |
| 14 | + column_visibility: {}, |
| 15 | + column_order: [], |
| 16 | + column_rename_mapping: {}, |
| 17 | + column_time_formats: {}, |
| 18 | + column_list_formats: {}, |
| 19 | +}; |
| 20 | + |
| 21 | +export const usePresetColumnConfig = ({ |
| 22 | + presetId, |
| 23 | + enabled = true, |
| 24 | + ...options |
| 25 | +}: UsePresetColumnConfigOptions = {}) => { |
| 26 | + const api = useApi(); |
| 27 | + const revalidateMultiple = useRevalidateMultiple(); |
| 28 | + |
| 29 | + const { |
| 30 | + data: columnConfig = DEFAULT_COLUMN_CONFIG, |
| 31 | + isLoading, |
| 32 | + error, |
| 33 | + mutate, |
| 34 | + } = useSWR<ColumnConfiguration>( |
| 35 | + // Only make API call if enabled, API is ready AND presetId is provided |
| 36 | + enabled && api?.isReady?.() && presetId |
| 37 | + ? `/preset/${presetId}/column-config` |
| 38 | + : null, |
| 39 | + async (url) => { |
| 40 | + try { |
| 41 | + const result = await api.get(url); |
| 42 | + return result || DEFAULT_COLUMN_CONFIG; |
| 43 | + } catch (error: any) { |
| 44 | + // If the column config endpoint fails (e.g., 404), return default config |
| 45 | + // This prevents the page from failing to load |
| 46 | + console.warn( |
| 47 | + `Failed to fetch column config for preset ${presetId}:`, |
| 48 | + error |
| 49 | + ); |
| 50 | + // Don't throw the error, just return default config |
| 51 | + return DEFAULT_COLUMN_CONFIG; |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + fallbackData: DEFAULT_COLUMN_CONFIG, |
| 56 | + // Disable error retries to prevent blocking the page |
| 57 | + shouldRetryOnError: false, |
| 58 | + revalidateOnFocus: false, |
| 59 | + // Return default config on error |
| 60 | + onError: (error) => { |
| 61 | + console.warn("Column config fetch error:", error); |
| 62 | + }, |
| 63 | + ...options, |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + const updateColumnConfig = useCallback( |
| 68 | + async (config: Partial<ColumnConfiguration>) => { |
| 69 | + if (!presetId) { |
| 70 | + showErrorToast("No preset ID provided"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!api?.isReady?.()) { |
| 75 | + console.warn("API not ready, cannot update column config"); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + const response = await api.put( |
| 81 | + `/preset/${presetId}/column-config`, |
| 82 | + config |
| 83 | + ); |
| 84 | + showSuccessToast("Column configuration saved!"); |
| 85 | + mutate(); |
| 86 | + // Also revalidate preset list to update any cached data |
| 87 | + revalidateMultiple(["/preset", "/preset?"]); |
| 88 | + return response; |
| 89 | + } catch (error) { |
| 90 | + showErrorToast(error, "Failed to save column configuration"); |
| 91 | + throw error; |
| 92 | + } |
| 93 | + }, |
| 94 | + [api, presetId, mutate, revalidateMultiple] |
| 95 | + ); |
| 96 | + |
| 97 | + return { |
| 98 | + columnConfig, |
| 99 | + isLoading, |
| 100 | + error, |
| 101 | + updateColumnConfig, |
| 102 | + mutate, |
| 103 | + }; |
| 104 | +}; |
| 105 | + |
| 106 | +export type UsePresetColumnConfigValue = ReturnType< |
| 107 | + typeof usePresetColumnConfig |
| 108 | +>; |
0 commit comments