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>
70 lines
3.4 KiB
JavaScript
70 lines
3.4 KiB
JavaScript
export function folderAssets(library, jobs, folderId) {
|
|
const metadata=new Map(jobs.flatMap(j=>j.outputs || []).map(a=>[a.id,a.studio2]))
|
|
const kinds=[['still','stills'],['clip','clips'],['track','tracks']]
|
|
return kinds.flatMap(([kind,key])=>(library[key] || []).filter(a=>a.id && a.folderId===folderId && a.hasVideo!==false && a.available!==false).map(a=>({...a,kind,studio2:a.studio2 || metadata.get(a.id)}))).sort((a,b)=>(a.createdAt || a.studio2?.savedAt || 0)-(b.createdAt || b.studio2?.savedAt || 0))
|
|
}
|
|
export function newestOutput(jobs, seen, folderId) {
|
|
return jobs.filter(j=>j.request.folderId===folderId).flatMap(j=>j.outputs || []).filter(a=>!seen.has(a.id)).sort((a,b)=>(a.studio2?.savedAt || 0)-(b.studio2?.savedAt || 0)).at(-1) || null
|
|
}
|
|
export function reconcileSelection(selected, assets) {
|
|
const list = assets || []
|
|
if (!selected?.id) return list.at(-1) || null
|
|
return list.find(a => a.id === selected.id) || list.at(-1) || null
|
|
}
|
|
export function activeJobs(jobs) { return jobs.filter(j=>['waking','enhancing','submitting','rendering','saving'].includes(j.state)) }
|
|
export function previewHidden(asset, revealed, xaigen) {
|
|
if (!xaigen || !asset?.id) return false
|
|
return !revealed?.has?.(asset.id)
|
|
}
|
|
/** A reported imageCount of 0 is an empty folder. Do not replace it with a local list length. */
|
|
export function folderImageCount(folder, localStillCount = 0) {
|
|
if (folder && typeof folder.imageCount === 'number' && Number.isFinite(folder.imageCount)) return folder.imageCount
|
|
return Number(localStillCount) || 0
|
|
}
|
|
|
|
/** Replace library rows from a wipe response. Rows already in the wiped folder stay gone. */
|
|
export function libraryAfterWipe(previous, response, folderId) {
|
|
const suppressed = new Set(previous?.suppressedIds || [])
|
|
const target = String(folderId || '')
|
|
for (const list of [previous?.stills, previous?.clips, previous?.tracks, response?.stills, response?.clips, response?.tracks]) {
|
|
for (const item of list || []) {
|
|
if (item?.id && String(item.folderId || '') === target) suppressed.add(item.id)
|
|
}
|
|
}
|
|
const take = (list) => (Array.isArray(list) ? list : []).filter(item => item?.id && !suppressed.has(item.id))
|
|
return {
|
|
folders: Array.isArray(response?.folders) ? response.folders : (previous?.folders || []),
|
|
stills: take(response?.stills),
|
|
clips: take(response?.clips),
|
|
tracks: take(response?.tracks),
|
|
suppressedIds: [...suppressed]
|
|
}
|
|
}
|
|
|
|
/** Later pages and a stale full list must not put a suppressed id back. */
|
|
export function mergeLibraryRows(prev, next, suppressedIds) {
|
|
const drop = new Set(suppressedIds || [])
|
|
const map = new Map()
|
|
for (const item of prev || []) {
|
|
if (item?.id && !drop.has(item.id)) map.set(item.id, item)
|
|
}
|
|
for (const item of next || []) {
|
|
if (item?.id && !drop.has(item.id)) map.set(item.id, item)
|
|
}
|
|
return [...map.values()]
|
|
}
|
|
|
|
export function replaceLibraryRows(next, suppressedIds) {
|
|
const drop = new Set(suppressedIds || [])
|
|
return (next || []).filter(item => item?.id && !drop.has(item.id))
|
|
}
|
|
|
|
export function selectResult(state, asset) { return {...state,selected:asset} }
|
|
export function assetLabel(asset) {
|
|
if (!asset) return 'None'
|
|
if (asset.name || asset.originalName || asset.filename) return asset.name || asset.originalName || asset.filename
|
|
if (asset.kind==='track') return 'Track '+asset.id.slice(0,8)
|
|
if (asset.kind==='clip') return 'Clip '+asset.id.slice(0,8)
|
|
return 'Still '+asset.id.slice(0,8)
|
|
}
|