refactor: stage uploads before drive and git delivery
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
import { useState, useRef, useCallback } from 'react'
|
||||
import type { FolderEntry } from '@/lib/client-types'
|
||||
import type { FileDiff } from '@/lib/types'
|
||||
import { checkFolderDiffs, uploadDrive, uploadGit } from '@/lib/upload-api'
|
||||
import { checkFolderDiffs, stageUpload, uploadDrive, uploadGit } from '@/lib/upload-api'
|
||||
import type { CheckResult } from '@/lib/upload-api'
|
||||
|
||||
interface UseUploadOrchestratorParams {
|
||||
@@ -42,6 +42,7 @@ export function useUploadOrchestrator({
|
||||
const abortRef = useRef<AbortController | null>(null)
|
||||
const checkResultRef = useRef<CheckResult>({ exists: false, diffs: [] })
|
||||
const uploadActionRef = useRef(false)
|
||||
const stagingIdRef = useRef<string | null>(null)
|
||||
|
||||
// Refs for values used inside callbacks to avoid stale closures
|
||||
const secretRef = useRef(secret)
|
||||
@@ -51,10 +52,14 @@ export function useUploadOrchestrator({
|
||||
|
||||
// ---- Internal: push a single folder to Git ----
|
||||
const pushGit = useCallback(async (index: number, signal?: AbortSignal) => {
|
||||
const folderEntry = entriesRef.current[index]
|
||||
const stagingId = stagingIdRef.current
|
||||
if (!stagingId) {
|
||||
updateEntry(index, { status: 'error', error: 'Preparation serveur introuvable' })
|
||||
return
|
||||
}
|
||||
|
||||
const gitResult = await uploadGit(
|
||||
folderEntry,
|
||||
stagingId,
|
||||
secretRef.current,
|
||||
(pct) => updateEntry(index, { progress: 50 + Math.round(pct / 2) }),
|
||||
signal,
|
||||
@@ -89,6 +94,11 @@ export function useUploadOrchestrator({
|
||||
|
||||
const folderEntry = currentEntries[i]
|
||||
const driveAction = checkResultRef.current.exists ? 'replace' : 'new'
|
||||
const stagingId = stagingIdRef.current
|
||||
if (!stagingId) {
|
||||
updateEntry(i, { status: 'error', error: 'Preparation serveur introuvable' })
|
||||
return
|
||||
}
|
||||
|
||||
// ---- Step 1: Drive upload ----
|
||||
updateEntry(i, {
|
||||
@@ -100,7 +110,7 @@ export function useUploadOrchestrator({
|
||||
})
|
||||
|
||||
const driveResult = await uploadDrive(
|
||||
folderEntry,
|
||||
stagingId,
|
||||
secretRef.current,
|
||||
driveAction as 'new' | 'replace',
|
||||
controller.signal,
|
||||
@@ -141,12 +151,17 @@ export function useUploadOrchestrator({
|
||||
setGlobalError(null)
|
||||
|
||||
const folder = entriesRef.current[0]
|
||||
const controller = new AbortController()
|
||||
abortRef.current = controller
|
||||
|
||||
try {
|
||||
const staged = await stageUpload(folder, secretRef.current, controller.signal)
|
||||
stagingIdRef.current = staged.stagingId
|
||||
|
||||
const check = await checkFolderDiffs(
|
||||
folder,
|
||||
staged.stagingId,
|
||||
secretRef.current,
|
||||
abortRef.current?.signal,
|
||||
controller.signal,
|
||||
)
|
||||
checkResultRef.current = check
|
||||
|
||||
@@ -155,11 +170,13 @@ export function useUploadOrchestrator({
|
||||
setNoChangesFolder(folder.folderName)
|
||||
uploadActionRef.current = false
|
||||
setIsChecking(false)
|
||||
abortRef.current = null
|
||||
return
|
||||
}
|
||||
setOverwriteConfirm({ folderName: folder.folderName, diffs: check.diffs })
|
||||
uploadActionRef.current = false
|
||||
setIsChecking(false)
|
||||
abortRef.current = null
|
||||
return
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -167,10 +184,12 @@ export function useUploadOrchestrator({
|
||||
setGlobalError(message)
|
||||
uploadActionRef.current = false
|
||||
setIsChecking(false)
|
||||
abortRef.current = null
|
||||
return
|
||||
}
|
||||
|
||||
uploadActionRef.current = false
|
||||
abortRef.current = null
|
||||
await proceedUpload()
|
||||
}, [setSecretError, proceedUpload, isChecking, isUploading])
|
||||
|
||||
@@ -231,6 +250,8 @@ export function useUploadOrchestrator({
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
if (isChecking) {
|
||||
abortRef.current?.abort()
|
||||
abortRef.current = null
|
||||
uploadActionRef.current = false
|
||||
setIsChecking(false)
|
||||
return
|
||||
@@ -240,6 +261,7 @@ export function useUploadOrchestrator({
|
||||
uploadActionRef.current = false
|
||||
setIsResolvingDriveError(false)
|
||||
setIsUploading(false)
|
||||
stagingIdRef.current = null
|
||||
}, [isChecking])
|
||||
|
||||
const handleReset = useCallback(() => {
|
||||
@@ -251,6 +273,7 @@ export function useUploadOrchestrator({
|
||||
setDriveError(null)
|
||||
checkResultRef.current = { exists: false, diffs: [] }
|
||||
uploadActionRef.current = false
|
||||
stagingIdRef.current = null
|
||||
}, [resetEntries])
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user