Stop a folder wipe from restoring the pre-wipe library catalog.

The in-memory catalog could outlive the write, so delete-all came back as thousands of empty tiles and a stale badge.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-24 12:01:25 -05:00
co-authored by Cursor
parent 39aa4b65d0
commit 545a3d749b
12 changed files with 393 additions and 131 deletions
+38 -49
View File
@@ -154,7 +154,7 @@ import {
type LibraryTrack
} from '~/utils/libraryBrowse'
import { clipSettingsRows, stillSettingsRows, trackSettingsRows } from '~/utils/libraryGeneration'
import { assetLabel } from '~/shared/studio2/viewer.mjs'
import { assetLabel, libraryAfterWipe, mergeLibraryRows, replaceLibraryRows } from '~/shared/studio2/viewer.mjs'
const props = defineProps<{
open: boolean
@@ -177,6 +177,7 @@ const emit = defineEmits<{
'load-input': [asset: any]
'load-input-raw': [asset: any]
delete: [asset: any]
wiped: [payload: any]
'toggle-preview': []
}>()
@@ -298,32 +299,19 @@ function onDeleteClick() {
emit('delete', asset)
}
function mergeIds(prev: { id: string }[], next: { id: string }[]) {
const drop = dropIds()
const map = new Map<string, { id: string }>()
for (const item of prev) {
if (!drop.has(item.id)) map.set(item.id, item)
}
for (const item of next) {
if (!drop.has(item.id)) map.set(item.id, item)
}
return [...map.values()]
}
function applyLibraryPage(data: any, replace: boolean) {
folders.value = data?.folders || folders.value
const drop = dropIds()
const keep = <T extends { id: string }>(list: T[]) => list.filter(item => !drop.has(item.id))
if (Array.isArray(data?.folders) && data.folders.length) folders.value = data.folders
const drop = [...dropIds()]
if (replace) {
clips.value = keep(data?.clips || []) as LibraryClip[]
stills.value = keep(data?.stills || []) as LibraryStill[]
tracks.value = keep(data?.tracks || []) as LibraryTrack[]
clips.value = replaceLibraryRows(data?.clips || [], drop) as LibraryClip[]
stills.value = replaceLibraryRows(data?.stills || [], drop) as LibraryStill[]
tracks.value = replaceLibraryRows(data?.tracks || [], drop) as LibraryTrack[]
} else {
clips.value = mergeIds(clips.value, data?.clips || []) as LibraryClip[]
stills.value = mergeIds(stills.value, data?.stills || []) as LibraryStill[]
tracks.value = mergeIds(tracks.value, data?.tracks || []) as LibraryTrack[]
clips.value = mergeLibraryRows(clips.value, data?.clips || [], drop) as LibraryClip[]
stills.value = mergeLibraryRows(stills.value, data?.stills || [], drop) as LibraryStill[]
tracks.value = mergeLibraryRows(tracks.value, data?.tracks || [], drop) as LibraryTrack[]
}
nextCursor.value = data?.nextCursor || null
nextCursor.value = data?.nextCursor ?? null
fetchedAt.value = Date.now()
}
@@ -343,20 +331,35 @@ async function confirmWipeFolder() {
if (!pending || wipeBusy.value) return
pendingWipe.value = null
wipeBusy.value = true
fetchGen += 1
const gone = [
...stills.value.filter(item => item.folderId === pending.folderId).map(item => item.id),
...clips.value.filter(item => item.folderId === pending.folderId).map(item => item.id),
...tracks.value.filter(item => item.folderId === pending.folderId).map(item => item.id)
]
const pendingIds = new Set(suppressedDeletedIds.value)
for (const id of gone) pendingIds.add(id)
suppressedDeletedIds.value = pendingIds
stills.value = stills.value.filter(item => item.folderId !== pending.folderId)
clips.value = clips.value.filter(item => item.folderId !== pending.folderId)
tracks.value = tracks.value.filter(item => item.folderId !== pending.folderId)
try {
const gone = [
...stills.value.filter(item => item.folderId === pending.folderId).map(item => item.id),
...clips.value.filter(item => item.folderId === pending.folderId).map(item => item.id),
...tracks.value.filter(item => item.folderId === pending.folderId).map(item => item.id)
]
const next = new Set(suppressedDeletedIds.value)
for (const id of gone) next.add(id)
suppressedDeletedIds.value = next
stills.value = stills.value.filter(item => item.folderId !== pending.folderId)
clips.value = clips.value.filter(item => item.folderId !== pending.folderId)
tracks.value = tracks.value.filter(item => item.folderId !== pending.folderId)
const result = await $fetch<any>(`/api/library/folders/${pending.folderId}/wipe`, { method: 'POST' })
applyLibraryPage(result, true)
const next = libraryAfterWipe({
folders: folders.value,
stills: [...stills.value, ...gone.map(id => ({ id, folderId: pending.folderId }))],
clips: clips.value,
tracks: tracks.value,
suppressedIds: [...suppressedDeletedIds.value]
}, result, pending.folderId)
suppressedDeletedIds.value = new Set(next.suppressedIds)
folders.value = next.folders
stills.value = next.stills as LibraryStill[]
clips.value = next.clips as LibraryClip[]
tracks.value = next.tracks as LibraryTrack[]
nextCursor.value = null
fetchedAt.value = 0
emit('wiped', { ...result, folderId: pending.folderId, suppressedIds: next.suppressedIds })
await fetchLibrary(true)
} catch (e: any) {
loadError.value = e?.data?.statusMessage || e?.message || 'Could not empty that folder'
@@ -378,14 +381,6 @@ async function fetchLibraryPage(cursor: string | null) {
let fetchGen = 0
function pageIds(data: { stills?: { id: string }[]; clips?: { id: string }[]; tracks?: { id: string }[] }) {
return [
...(data?.stills || []).map(item => item.id),
...(data?.clips || []).map(item => item.id),
...(data?.tracks || []).map(item => item.id)
]
}
async function fetchLibrary(force = false) {
const stale = !fetchedAt.value || Date.now() - fetchedAt.value > 15_000
const empty = !stills.value.length && !clips.value.length && !tracks.value.length
@@ -394,11 +389,9 @@ async function fetchLibrary(force = false) {
loading.value = true
loadError.value = ''
liveThumbs.value = new Set()
const serverIds = new Set<string>()
try {
const first = await fetchLibraryPage(null)
if (gen !== fetchGen) return
for (const id of pageIds(first)) serverIds.add(id)
applyLibraryPage(first, true)
loading.value = false
nextTick(() => observeThumbs())
@@ -407,14 +400,10 @@ async function fetchLibrary(force = false) {
while (cursor && gen === fetchGen) {
const more = await fetchLibraryPage(cursor)
if (gen !== fetchGen) return
for (const id of pageIds(more)) serverIds.add(id)
applyLibraryPage(more, false)
cursor = more?.nextCursor || null
nextTick(() => observeThumbs())
}
if (gen === fetchGen) {
suppressedDeletedIds.value = new Set([...suppressedDeletedIds.value].filter(id => serverIds.has(id)))
}
} catch (e: any) {
if (gen !== fetchGen) return
loadError.value = e?.data?.statusMessage || e?.message || 'Could not load library'