fix: prevent duplicate uploads and group asset commits

This commit is contained in:
Tom Boullay
2026-04-24 16:58:49 +02:00
parent fe8a6f0f54
commit 53c4c0ed60
15 changed files with 329 additions and 152 deletions
+98 -52
View File
@@ -26,6 +26,8 @@ export function useUploadOrchestrator({
resetEntries,
}: UseUploadOrchestratorParams) {
const [isUploading, setIsUploading] = useState(false)
const [isChecking, setIsChecking] = useState(false)
const [isResolvingDriveError, setIsResolvingDriveError] = useState(false)
const [globalError, setGlobalError] = useState<string | null>(null)
const [overwriteConfirm, setOverwriteConfirm] = useState<{
folderName: string
@@ -39,6 +41,7 @@ export function useUploadOrchestrator({
const abortRef = useRef<AbortController | null>(null)
const checkResultRef = useRef<CheckResult>({ exists: false, diffs: [] })
const uploadActionRef = useRef(false)
// Refs for values used inside callbacks to avoid stale closures
const secretRef = useRef(secret)
@@ -67,63 +70,73 @@ export function useUploadOrchestrator({
// ---- Main upload flow: Drive first, then Git ----
const proceedUpload = useCallback(async () => {
if (uploadActionRef.current) return
uploadActionRef.current = true
setOverwriteConfirm(null)
setIsChecking(false)
setIsUploading(true)
setGlobalError(null)
const controller = new AbortController()
abortRef.current = controller
const currentEntries = entriesRef.current
try {
const currentEntries = entriesRef.current
for (let i = 0; i < currentEntries.length; i++) {
if (currentEntries[i].status === 'success') continue
if (controller.signal.aborted) break
for (let i = 0; i < currentEntries.length; i++) {
if (currentEntries[i].status === 'success') continue
if (controller.signal.aborted) break
const folderEntry = currentEntries[i]
const driveAction = checkResultRef.current.exists ? 'replace' : 'new'
const folderEntry = currentEntries[i]
const driveAction = checkResultRef.current.exists ? 'replace' : 'new'
// ---- Step 1: Drive upload ----
updateEntry(i, {
status: 'uploading',
progress: 1,
error: undefined,
driveStatus: 'uploading',
driveError: undefined,
})
// ---- Step 1: Drive upload ----
updateEntry(i, {
status: 'uploading',
progress: 1,
error: undefined,
driveStatus: 'uploading',
driveError: undefined,
})
const driveResult = await uploadDrive(
folderEntry,
secretRef.current,
driveAction as 'new' | 'replace',
controller.signal,
)
const driveResult = await uploadDrive(
folderEntry,
secretRef.current,
driveAction as 'new' | 'replace',
controller.signal,
)
if (!driveResult.success) {
updateEntry(i, { driveStatus: 'error', driveError: driveResult.error })
setDriveError({ error: driveResult.error || 'Erreur inconnue', folderIndex: i })
return
if (!driveResult.success) {
updateEntry(i, { driveStatus: 'error', driveError: driveResult.error })
setDriveError({ error: driveResult.error || 'Erreur inconnue', folderIndex: i })
return
}
updateEntry(i, { driveStatus: 'success', progress: 50 })
// ---- Step 2: Git upload ----
await pushGit(i, controller.signal)
}
updateEntry(i, { driveStatus: 'success', progress: 50 })
// ---- Step 2: Git upload ----
await pushGit(i, controller.signal)
} finally {
abortRef.current = null
setIsUploading(false)
uploadActionRef.current = false
}
abortRef.current = null
setIsUploading(false)
}, [updateEntry, pushGit])
// ---- Handlers ----
const handleUpload = useCallback(async () => {
if (uploadActionRef.current || isChecking || isUploading) return
if (!secretRef.current.trim()) {
setSecretError("La cle d'acces est requise")
return
}
if (entriesRef.current.length === 0) return
uploadActionRef.current = true
setIsChecking(true)
setSecretError(null)
setGlobalError(null)
@@ -140,47 +153,60 @@ export function useUploadOrchestrator({
if (check.exists) {
if (check.diffs.length === 0) {
setNoChangesFolder(folder.folderName)
uploadActionRef.current = false
setIsChecking(false)
return
}
setOverwriteConfirm({ folderName: folder.folderName, diffs: check.diffs })
uploadActionRef.current = false
setIsChecking(false)
return
}
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur inconnue'
setGlobalError(message)
uploadActionRef.current = false
setIsChecking(false)
return
}
uploadActionRef.current = false
await proceedUpload()
}, [setSecretError, proceedUpload])
}, [setSecretError, proceedUpload, isChecking, isUploading])
const handleDriveContinue = useCallback(async () => {
if (!driveError) return
if (!driveError || uploadActionRef.current) return
uploadActionRef.current = true
setIsResolvingDriveError(true)
const idx = driveError.folderIndex
setDriveError(null)
updateEntry(idx, { driveStatus: 'skipped' })
try {
updateEntry(idx, { driveStatus: 'skipped' })
const signal = abortRef.current?.signal
await pushGit(idx, signal)
const signal = abortRef.current?.signal
await pushGit(idx, signal)
const currentEntries = entriesRef.current
for (let i = idx + 1; i < currentEntries.length; i++) {
if (currentEntries[i].status === 'success') continue
if (abortRef.current?.signal.aborted) break
const currentEntries = entriesRef.current
for (let i = idx + 1; i < currentEntries.length; i++) {
if (currentEntries[i].status === 'success') continue
if (abortRef.current?.signal.aborted) break
updateEntry(i, {
status: 'uploading',
progress: 0,
error: undefined,
driveStatus: 'skipped',
})
updateEntry(i, {
status: 'uploading',
progress: 0,
error: undefined,
driveStatus: 'skipped',
})
await pushGit(i, abortRef.current?.signal)
await pushGit(i, abortRef.current?.signal)
}
} finally {
abortRef.current = null
setIsUploading(false)
setIsResolvingDriveError(false)
uploadActionRef.current = false
}
abortRef.current = null
setIsUploading(false)
}, [driveError, updateEntry, pushGit])
const handleDriveCancel = useCallback(() => {
@@ -197,25 +223,40 @@ export function useUploadOrchestrator({
abortRef.current?.abort()
abortRef.current = null
uploadActionRef.current = false
setIsChecking(false)
setIsResolvingDriveError(false)
setIsUploading(false)
}, [driveError, updateEntry])
const handleCancel = useCallback(() => {
if (isChecking) {
uploadActionRef.current = false
setIsChecking(false)
return
}
abortRef.current?.abort()
abortRef.current = null
uploadActionRef.current = false
setIsResolvingDriveError(false)
setIsUploading(false)
}, [])
}, [isChecking])
const handleReset = useCallback(() => {
resetEntries()
setGlobalError(null)
setIsChecking(false)
setIsUploading(false)
setIsResolvingDriveError(false)
setDriveError(null)
checkResultRef.current = { exists: false, diffs: [] }
uploadActionRef.current = false
}, [resetEntries])
return {
isUploading,
isChecking,
isResolvingDriveError,
globalError,
setGlobalError,
overwriteConfirm,
@@ -224,6 +265,11 @@ export function useUploadOrchestrator({
setNoChangesFolder,
driveError,
handleUpload,
handleOverwriteCancel: () => {
setOverwriteConfirm(null)
uploadActionRef.current = false
setIsChecking(false)
},
handleDriveContinue,
handleDriveCancel,
handleCancel,