67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { ConvexHttpClient } from 'convex/browser'
|
|
import { api } from '@/convex/_generated/api'
|
|
import type { Manual } from '@/lib/manuals-types'
|
|
|
|
type ConvexManualDoc = {
|
|
filename: string
|
|
path: string
|
|
manufacturer: string
|
|
category: string
|
|
size?: number
|
|
lastModified?: number
|
|
searchTerms?: string[]
|
|
commonNames?: string[]
|
|
thumbnailUrl?: string
|
|
}
|
|
|
|
function getClient() {
|
|
const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL
|
|
if (!convexUrl) {
|
|
return null
|
|
}
|
|
|
|
return new ConvexHttpClient(convexUrl)
|
|
}
|
|
|
|
function mapManual(manual: ConvexManualDoc): Manual {
|
|
return {
|
|
filename: manual.filename,
|
|
path: manual.path,
|
|
manufacturer: manual.manufacturer,
|
|
category: manual.category,
|
|
size: manual.size,
|
|
lastModified: manual.lastModified ? new Date(manual.lastModified) : undefined,
|
|
searchTerms: manual.searchTerms,
|
|
commonNames: manual.commonNames,
|
|
thumbnailUrl: manual.thumbnailUrl,
|
|
}
|
|
}
|
|
|
|
export async function fetchPublishedManualsCatalog(): Promise<Manual[]> {
|
|
const client = getClient()
|
|
if (!client) {
|
|
return []
|
|
}
|
|
|
|
try {
|
|
const manuals = await client.query(api.manuals.list, {})
|
|
return (manuals as ConvexManualDoc[]).map(mapManual)
|
|
} catch (error) {
|
|
console.error('[manuals-live-catalog] catalog refresh failed', error)
|
|
return []
|
|
}
|
|
}
|
|
|
|
export async function fetchPublishedManualsDashboard() {
|
|
const client = getClient()
|
|
if (!client) {
|
|
return null
|
|
}
|
|
|
|
try {
|
|
return await client.query(api.manuals.dashboard, {})
|
|
} catch (error) {
|
|
console.error('[manuals-live-catalog] dashboard refresh failed', error)
|
|
return null
|
|
}
|
|
}
|