33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
import { ref, watch } from 'vue'
|
|
import { useUserLookup } from '@/composables/useUserLookup'
|
|
import { namesWithDeletedFallback } from '@/lib/deletedUser'
|
|
import type { ContentEntry } from '@/lib/contentTypes'
|
|
|
|
function authorIds(entry: ContentEntry | null) {
|
|
return entry ? [entry.created_by, entry.updated_by].filter((id) => id > 0) : []
|
|
}
|
|
|
|
export function useEntryNames(entry: () => ContentEntry | null) {
|
|
const { lookupUsers } = useUserLookup()
|
|
const names = ref<Record<number, string>>({})
|
|
const requested = new Set<number>()
|
|
|
|
async function resolve() {
|
|
const wanted = [...new Set(authorIds(entry()))].filter((id) => !requested.has(id))
|
|
if (wanted.length === 0) {
|
|
return
|
|
}
|
|
wanted.forEach((id) => requested.add(id))
|
|
try {
|
|
const users = await lookupUsers(wanted)
|
|
names.value = { ...names.value, ...namesWithDeletedFallback(wanted, users) }
|
|
} catch {
|
|
wanted.forEach((id) => requested.delete(id))
|
|
}
|
|
}
|
|
|
|
watch(() => authorIds(entry()).join(','), resolve, { immediate: true })
|
|
|
|
return names
|
|
}
|