diff options
Diffstat (limited to 'hooks')
| -rw-r--r-- | hooks/use-sync-status.ts | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/hooks/use-sync-status.ts b/hooks/use-sync-status.ts index 99810fdc..52a67343 100644 --- a/hooks/use-sync-status.ts +++ b/hooks/use-sync-status.ts @@ -11,7 +11,7 @@ interface SyncStatus { failedChanges: number lastSyncAt?: string | null error?: string | null - contractId?: number + projectId?: number targetSystem?: string lastUpdated?: string } @@ -23,7 +23,7 @@ interface ApiError extends Error { interface SyncBatch { id: string - contractId: number + projectId: number targetSystem: string status: 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED' startedAt?: string @@ -35,7 +35,7 @@ interface SyncBatch { } interface SyncConfig { - contractId: number + projectId: number targetSystem: string autoSyncEnabled: boolean syncInterval: number @@ -77,21 +77,21 @@ const fetcher = async (url: string): Promise<any> => { } // π§ μμ ν κΈ°λ³Έκ° μμ± -const createDefaultSyncStatus = (error?: string, contractId?: number): SyncStatus => ({ +const createDefaultSyncStatus = (error?: string, projectId?: number): SyncStatus => ({ syncEnabled: false, pendingChanges: 0, syncedChanges: 0, failedChanges: 0, lastSyncAt: null, error, - contractId, + projectId, lastUpdated: new Date().toISOString() }) // β
λ¨μΌ κ³μ½ λκΈ°ν μν μ‘°ν -export function useSyncStatus(contractId: number | null, targetSystem: string = 'SHI') { - const key = contractId - ? `/api/sync/status?contractId=${contractId}&targetSystem=${targetSystem}` +export function useSyncStatus(projectId: number | null, targetSystem: string = 'SHI') { + const key = projectId + ? `/api/sync/status?projectId=${projectId}&targetSystem=${targetSystem}` : null const { data, error, isLoading, mutate: localMutate } = useSWR<SyncStatus>( @@ -130,7 +130,7 @@ export function useSyncStatus(contractId: number | null, targetSystem: string = failedChanges: Number(data.failedChanges) || 0, lastSyncAt: data.lastSyncAt || null, error: data.error || (error ? (error as ApiError).message : null), - contractId: contractId || data.contractId, + projectId: projectId || data.projectId, targetSystem: targetSystem, lastUpdated: new Date().toISOString() } @@ -138,13 +138,13 @@ export function useSyncStatus(contractId: number | null, targetSystem: string = return createDefaultSyncStatus( error ? (error as ApiError).message : undefined, - contractId || undefined + projectId || undefined ) - }, [data, error, contractId, targetSystem]) + }, [data, error, projectId, targetSystem]) return { syncStatus: safeData, - isLoading: isLoading && Boolean(contractId), + isLoading: isLoading && Boolean(projectId), error: error as ApiError | null, refetch } @@ -154,16 +154,16 @@ export function useSyncStatus(contractId: number | null, targetSystem: string = // λμ useDynamicSyncStatus μ¬μ© κΆμ₯ // β
λ€μ€ κ³μ½ λκΈ°ν μν μ‘°ν (Hook κ·μΉ μ€μ) -export function useDynamicSyncStatus(contractIds: number[], targetSystem: string = 'SHI') { +export function useDynamicSyncStatus(projectIds: number[], targetSystem: string = 'SHI') { // Hook κ·μΉ μ€μ: κ³ μ λ μμ Hook νΈμΆ const paddedContractIds = React.useMemo(() => { // μ
λ ₯ κ²μ¦ λ° κ²½κ³ - if (contractIds.length > MAX_CONTRACTS) { - console.warn(`Contract count (${contractIds.length}) exceeds maximum (${MAX_CONTRACTS}). Only first ${MAX_CONTRACTS} will be processed.`) + if (projectIds.length > MAX_CONTRACTS) { + console.warn(`Contract count (${projectIds.length}) exceeds maximum (${MAX_CONTRACTS}). Only first ${MAX_CONTRACTS} will be processed.`) } // π₯ λͺ
μμ νμ
μ μΈ - const padded: (number | null)[] = [...contractIds.slice(0, MAX_CONTRACTS)] + const padded: (number | null)[] = [...projectIds.slice(0, MAX_CONTRACTS)] // nullλ‘ ν¨λ© while (padded.length < MAX_CONTRACTS) { @@ -171,17 +171,17 @@ export function useDynamicSyncStatus(contractIds: number[], targetSystem: string } return padded - }, [contractIds]) + }, [projectIds]) // κ° contractIdμ λν΄ κ³ μ λ μμ Hook νΈμΆ - const allResults = paddedContractIds.map((contractId) => { - const result = useSyncStatus(contractId, targetSystem) - return contractId ? { contractId, ...result } : null + const allResults = paddedContractIds.map((projectId) => { + const result = useSyncStatus(projectId, targetSystem) + return projectId ? { projectId, ...result } : null }) // μ ν¨ν κ²°κ³Όλ§ νν°λ§ const validResults = React.useMemo(() => { return allResults.filter((result): result is { - contractId: number + projectId: number syncStatus: SyncStatus isLoading: boolean error: ApiError | null @@ -214,9 +214,9 @@ export function useDynamicSyncStatus(contractIds: number[], targetSystem: string totalFailed, hasError, isLoading, - canSync: totalPending > 0 && !hasError && contractIds.length > 0 + canSync: totalPending > 0 && !hasError && projectIds.length > 0 } - }, [validResults, contractIds.length]) + }, [validResults, projectIds.length]) const refetchAll = React.useCallback(() => { validResults.forEach(({ refetch }) => { @@ -236,7 +236,7 @@ export function useDynamicSyncStatus(contractIds: number[], targetSystem: string } // β
ν΄λΌμ΄μΈνΈ μ μ© λκΈ°ν μν μ‘°ν (μλ² μ¬μ΄λ λ λλ§ νΈν) -export function useClientSyncStatus(contractIds: number[], targetSystem: string = 'SHI') { +export function useClientSyncStatus(projectIds: number[], targetSystem: string = 'SHI',) { const [isClient, setIsClient] = React.useState(false) React.useEffect(() => { @@ -244,7 +244,7 @@ export function useClientSyncStatus(contractIds: number[], targetSystem: string }, []) const syncResult = useDynamicSyncStatus( - isClient ? contractIds : [], + isClient ? projectIds : [], targetSystem ) @@ -268,9 +268,9 @@ export function useClientSyncStatus(contractIds: number[], targetSystem: string } // β
λκΈ°ν λ°°μΉ λͺ©λ‘ μ‘°ν -export function useSyncBatches(contractId: number | null, targetSystem: string = 'SHI') { - const key = contractId - ? `/api/sync/batches?contractId=${contractId}&targetSystem=${targetSystem}` +export function useSyncBatches(projectId: number | null, targetSystem: string = 'SHI') { + const key = projectId + ? `/api/sync/batches?projectId=${projectId}&targetSystem=${targetSystem}` : null const { data, error, isLoading } = useSWR<SyncBatch[]>( @@ -285,15 +285,15 @@ export function useSyncBatches(contractId: number | null, targetSystem: string = return { syncBatches: data || [], - isLoading: isLoading && Boolean(contractId), + isLoading: isLoading && Boolean(projectId), error: error as ApiError | null } } // β
λκΈ°ν μ€μ μ‘°ν -export function useSyncConfig(contractId: number | null, targetSystem: string = 'SHI') { - const key = contractId - ? `/api/sync/config?contractId=${contractId}&targetSystem=${targetSystem}` +export function useSyncConfig(projectId: number | null, targetSystem: string = 'SHI') { + const key = projectId + ? `/api/sync/config?projectId=${projectId}&targetSystem=${targetSystem}` : null const { data, error, isLoading, mutate: localMutate } = useSWR<SyncConfig>( @@ -313,7 +313,7 @@ export function useSyncConfig(contractId: number | null, targetSystem: string = return { syncConfig: data, - isLoading: isLoading && Boolean(contractId), + isLoading: isLoading && Boolean(projectId), error: error as ApiError | null, refetch } @@ -323,7 +323,7 @@ export function useSyncConfig(contractId: number | null, targetSystem: string = export function useTriggerSync() { const { trigger, isMutating, error } = useSWRMutation( '/api/sync/trigger', - async (url: string, { arg }: { arg: { contractId: number; targetSystem?: string } }) => { + async (url: string, { arg }: { arg: { projectId: number; targetSystem?: string } }) => { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -342,14 +342,14 @@ export function useTriggerSync() { } ) - const triggerSync = React.useCallback(async (arg: { contractId: number; targetSystem?: string }) => { + const triggerSync = React.useCallback(async (arg: { projectId: number; targetSystem?: string }) => { try { const result = await trigger(arg) // μ±κ³΅ ν κ΄λ ¨ μΊμ 무ν¨ν const targetSystem = arg.targetSystem || 'SHI' - const statusKey = `/api/sync/status?contractId=${arg.contractId}&targetSystem=${targetSystem}` - const batchesKey = `/api/sync/batches?contractId=${arg.contractId}&targetSystem=${targetSystem}` + const statusKey = `/api/sync/status?projectId=${arg.projectId}&targetSystem=${targetSystem}` + const batchesKey = `/api/sync/batches?projectId=${arg.projectId}&targetSystem=${targetSystem}` globalMutate(statusKey) globalMutate(batchesKey) @@ -372,7 +372,7 @@ export function useTriggerSync() { export function useUpdateSyncConfig() { const { trigger, isMutating, error } = useSWRMutation( '/api/sync/config', - async (url: string, { arg }: { arg: Partial<SyncConfig> & { contractId: number; targetSystem: string } }) => { + async (url: string, { arg }: { arg: Partial<SyncConfig> & { projectId: number; targetSystem: string } }) => { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -393,13 +393,13 @@ export function useUpdateSyncConfig() { ) // β
μλ μΊμ 무ν¨νλ₯Ό ν¬ν¨ν λν ν¨μ - const updateConfig = React.useCallback(async (arg: Partial<SyncConfig> & { contractId: number; targetSystem: string }) => { + const updateConfig = React.useCallback(async (arg: Partial<SyncConfig> & { projectId: number; targetSystem: string }) => { try { const result = await trigger(arg) // β
μ±κ³΅ ν μλμΌλ‘ μΊμ 무ν¨ν - const { contractId, targetSystem } = arg - const configKey = `/api/sync/config?contractId=${contractId}&targetSystem=${targetSystem}` + const { projectId, targetSystem } = arg + const configKey = `/api/sync/config?projectId=${projectId}&targetSystem=${targetSystem}` globalMutate(configKey) return result @@ -416,9 +416,9 @@ export function useUpdateSyncConfig() { } } // β
μ€μκ° λκΈ°ν μν ν
(λμ κ°±μ λΉλ) -export function useRealtimeSyncStatus(contractId: number | null, targetSystem: string = 'SHI') { - const key = contractId - ? `/api/sync/status?contractId=${contractId}&targetSystem=${targetSystem}&realtime=true` +export function useRealtimeSyncStatus(projectId: number | null, targetSystem: string = 'SHI') { + const key = projectId + ? `/api/sync/status?projectId=${projectId}&targetSystem=${targetSystem}&realtime=true` : null const { data, error, isLoading } = useSWR<SyncStatus>( @@ -436,13 +436,13 @@ export function useRealtimeSyncStatus(contractId: number | null, targetSystem: s const safeData = React.useMemo(() => { return data || createDefaultSyncStatus( error ? (error as ApiError).message : undefined, - contractId || undefined + projectId || undefined ) - }, [data, error, contractId]) + }, [data, error, projectId]) return { syncStatus: safeData, - isLoading: isLoading && Boolean(contractId), + isLoading: isLoading && Boolean(projectId), error: error as ApiError | null } } @@ -450,10 +450,10 @@ export function useRealtimeSyncStatus(contractId: number | null, targetSystem: s // π§ μ νΈλ¦¬ν° ν¨μλ€ export const syncUtils = { // μΊμ μλ 무ν¨ν - invalidateCache: (contractId: number, targetSystem: string = 'SHI') => { - const statusKey = `/api/sync/status?contractId=${contractId}&targetSystem=${targetSystem}` - const batchesKey = `/api/sync/batches?contractId=${contractId}&targetSystem=${targetSystem}` - const configKey = `/api/sync/config?contractId=${contractId}&targetSystem=${targetSystem}` + invalidateCache: (projectId: number, targetSystem: string = 'SHI') => { + const statusKey = `/api/sync/status?projectId=${projectId}&targetSystem=${targetSystem}` + const batchesKey = `/api/sync/batches?projectId=${projectId}&targetSystem=${targetSystem}` + const configKey = `/api/sync/config?projectId=${projectId}&targetSystem=${targetSystem}` globalMutate(statusKey) globalMutate(batchesKey) |
