82 lines
3.8 KiB
JavaScript
82 lines
3.8 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
|
|
}
|
|
|
|
/** Cache-bust a library media URL. Always includes v so a pre-deploy cached URL is not reused. */
|
|
export function libraryMediaUrl(path, version) {
|
|
const base = String(path || '')
|
|
if (!base) return ''
|
|
const v = version == null || version === '' ? '0' : String(version)
|
|
const joiner = base.includes('?') ? '&' : '?'
|
|
return `${base}${joiner}v=${encodeURIComponent(v)}`
|
|
}
|
|
|
|
/** Replace library rows from a wipe response. Does not keep a suppression list. */
|
|
export function libraryAfterWipe(previous, response, folderId) {
|
|
const target = String(folderId || '')
|
|
const dropped = new Set(response?.deletedIds || [])
|
|
for (const list of [previous?.stills, previous?.clips, previous?.tracks]) {
|
|
for (const item of list || []) {
|
|
if (item?.id && String(item.folderId || '') === target) dropped.add(item.id)
|
|
}
|
|
}
|
|
const take = (list) => (Array.isArray(list) ? list : []).filter((item) => {
|
|
if (!item?.id || dropped.has(item.id)) return false
|
|
return String(item.folderId || '') !== target
|
|
})
|
|
return {
|
|
folders: Array.isArray(response?.folders) ? response.folders : (previous?.folders || []),
|
|
stills: take(response?.stills),
|
|
clips: take(response?.clips),
|
|
tracks: take(response?.tracks),
|
|
suppressedIds: []
|
|
}
|
|
}
|
|
|
|
/** 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)
|
|
}
|