fix: fall back to public Convex manuals queries
This commit is contained in:
parent
8e9676b86d
commit
56e9e13b56
4 changed files with 84 additions and 170 deletions
|
|
@ -1,9 +1,8 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useMemo, useState } from 'react'
|
||||||
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'
|
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||||
import { fetchPublishedManualsDashboard } from '@/lib/manuals-live-catalog'
|
|
||||||
import {
|
import {
|
||||||
BarChart3,
|
BarChart3,
|
||||||
FileText,
|
FileText,
|
||||||
|
|
@ -33,51 +32,29 @@ interface ManualsDashboardClientProps {
|
||||||
|
|
||||||
export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
||||||
const [searchTerm, setSearchTerm] = useState('')
|
const [searchTerm, setSearchTerm] = useState('')
|
||||||
const [liveData, setLiveData] = useState(data)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let cancelled = false
|
|
||||||
|
|
||||||
void (async () => {
|
|
||||||
const refreshed = await fetchPublishedManualsDashboard()
|
|
||||||
if (cancelled || !refreshed) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setLiveData((current) => {
|
|
||||||
const currentCount = Array.isArray(current?.metadata) ? current.metadata.length : 0
|
|
||||||
const refreshedCount = Array.isArray(refreshed?.metadata) ? refreshed.metadata.length : 0
|
|
||||||
return refreshedCount > currentCount ? refreshed : current
|
|
||||||
})
|
|
||||||
})()
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
cancelled = true
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Calculate statistics
|
// Calculate statistics
|
||||||
const stats = useMemo(() => {
|
const stats = useMemo(() => {
|
||||||
const missing = liveData.missingManuals?.summary || {}
|
const missing = data.missingManuals?.summary || {}
|
||||||
const qaCategories = liveData.qaData.reduce((acc: any, item: any) => {
|
const qaCategories = data.qaData.reduce((acc: any, item: any) => {
|
||||||
const cat = item.category || 'unknown'
|
const cat = item.category || 'unknown'
|
||||||
acc[cat] = (acc[cat] || 0) + 1
|
acc[cat] = (acc[cat] || 0) + 1
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
const metadataWithSpecs = liveData.metadata.filter((m: any) =>
|
const metadataWithSpecs = data.metadata.filter((m: any) =>
|
||||||
m.specifications?.dimensions && Object.keys(m.specifications.dimensions).length > 0
|
m.specifications?.dimensions && Object.keys(m.specifications.dimensions).length > 0
|
||||||
).length
|
).length
|
||||||
|
|
||||||
const metadataWithParts = liveData.metadata.filter((m: any) =>
|
const metadataWithParts = data.metadata.filter((m: any) =>
|
||||||
m.parts_list && m.parts_list.length > 0
|
m.parts_list && m.parts_list.length > 0
|
||||||
).length
|
).length
|
||||||
|
|
||||||
const metadataWithTroubleshooting = liveData.metadata.filter((m: any) =>
|
const metadataWithTroubleshooting = data.metadata.filter((m: any) =>
|
||||||
m.troubleshooting && m.troubleshooting.length > 0
|
m.troubleshooting && m.troubleshooting.length > 0
|
||||||
).length
|
).length
|
||||||
|
|
||||||
const schemaTypes = liveData.structuredData.reduce((acc: any, item: any) => {
|
const schemaTypes = data.structuredData.reduce((acc: any, item: any) => {
|
||||||
item.schemas?.forEach((schema: any) => {
|
item.schemas?.forEach((schema: any) => {
|
||||||
const type = schema['@type'] || 'unknown'
|
const type = schema['@type'] || 'unknown'
|
||||||
acc[type] = (acc[type] || 0) + 1
|
acc[type] = (acc[type] || 0) + 1
|
||||||
|
|
@ -89,36 +66,36 @@ export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
||||||
totalModels: missing.total_expected_models || 0,
|
totalModels: missing.total_expected_models || 0,
|
||||||
missingAll: missing.models_missing_all || 0,
|
missingAll: missing.models_missing_all || 0,
|
||||||
partial: missing.models_partial || 0,
|
partial: missing.models_partial || 0,
|
||||||
totalQAPairs: liveData.qaData.length,
|
totalQAPairs: data.qaData.length,
|
||||||
qaCategories,
|
qaCategories,
|
||||||
totalManuals: liveData.metadata.length,
|
totalManuals: data.metadata.length,
|
||||||
metadataWithSpecs,
|
metadataWithSpecs,
|
||||||
metadataWithParts,
|
metadataWithParts,
|
||||||
metadataWithTroubleshooting,
|
metadataWithTroubleshooting,
|
||||||
totalChunks: liveData.semanticIndex?.total_chunks || 0,
|
totalChunks: data.semanticIndex?.total_chunks || 0,
|
||||||
schemaTypes,
|
schemaTypes,
|
||||||
highPriority: liveData.acquisitionList?.high_priority || 0,
|
highPriority: data.acquisitionList?.high_priority || 0,
|
||||||
mediumPriority: liveData.acquisitionList?.medium_priority || 0,
|
mediumPriority: data.acquisitionList?.medium_priority || 0,
|
||||||
lowPriority: liveData.acquisitionList?.low_priority || 0,
|
lowPriority: data.acquisitionList?.low_priority || 0,
|
||||||
}
|
}
|
||||||
}, [liveData])
|
}, [data])
|
||||||
|
|
||||||
// Filter Q&A data
|
// Filter Q&A data
|
||||||
const filteredQA = useMemo(() => {
|
const filteredQA = useMemo(() => {
|
||||||
if (!searchTerm) return liveData.qaData.slice(0, 50)
|
if (!searchTerm) return data.qaData.slice(0, 50)
|
||||||
const term = searchTerm.toLowerCase()
|
const term = searchTerm.toLowerCase()
|
||||||
return liveData.qaData.filter((item: any) =>
|
return data.qaData.filter((item: any) =>
|
||||||
item.question?.toLowerCase().includes(term) ||
|
item.question?.toLowerCase().includes(term) ||
|
||||||
item.answer?.toLowerCase().includes(term)
|
item.answer?.toLowerCase().includes(term)
|
||||||
).slice(0, 50)
|
).slice(0, 50)
|
||||||
}, [liveData.qaData, searchTerm])
|
}, [data.qaData, searchTerm])
|
||||||
|
|
||||||
// Get high priority missing manuals
|
// Get high priority missing manuals
|
||||||
const highPriorityMissing = useMemo(() => {
|
const highPriorityMissing = useMemo(() => {
|
||||||
return (liveData.acquisitionList?.acquisition_list || [])
|
return (data.acquisitionList?.acquisition_list || [])
|
||||||
.filter((item: any) => item.priority === 'high')
|
.filter((item: any) => item.priority === 'high')
|
||||||
.slice(0, 20)
|
.slice(0, 20)
|
||||||
}, [liveData.acquisitionList])
|
}, [data.acquisitionList])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-8">
|
<div className="space-y-8">
|
||||||
|
|
@ -414,13 +391,13 @@ export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
||||||
<CardTitle>Sample Manual Metadata</CardTitle>
|
<CardTitle>Sample Manual Metadata</CardTitle>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
{liveData.metadata.length > 0 && (
|
{data.metadata.length > 0 && (
|
||||||
<div className="space-y-2 text-sm">
|
<div className="space-y-2 text-sm">
|
||||||
<p><strong>Manufacturer:</strong> {liveData.metadata[0].manufacturer}</p>
|
<p><strong>Manufacturer:</strong> {data.metadata[0].manufacturer}</p>
|
||||||
<p><strong>Model:</strong> {liveData.metadata[0].model_number || 'N/A'}</p>
|
<p><strong>Model:</strong> {data.metadata[0].model_number || 'N/A'}</p>
|
||||||
<p><strong>Type:</strong> {liveData.metadata[0].manual_type}</p>
|
<p><strong>Type:</strong> {data.metadata[0].manual_type}</p>
|
||||||
{liveData.metadata[0].specifications?.interfaces && (
|
{data.metadata[0].specifications?.interfaces && (
|
||||||
<p><strong>Interfaces:</strong> {liveData.metadata[0].specifications.interfaces.join(', ')}</p>
|
<p><strong>Interfaces:</strong> {data.metadata[0].specifications.interfaces.join(', ')}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
@ -439,7 +416,7 @@ export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<p className="text-sm">
|
<p className="text-sm">
|
||||||
<strong>Total Schemas:</strong> {liveData.structuredData.length}
|
<strong>Total Schemas:</strong> {data.structuredData.length}
|
||||||
</p>
|
</p>
|
||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
<p className="text-sm font-semibold mb-2">Schema Types:</p>
|
<p className="text-sm font-semibold mb-2">Schema Types:</p>
|
||||||
|
|
@ -466,7 +443,7 @@ export function ManualsDashboardClient({ data }: ManualsDashboardClientProps) {
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-between">
|
<div className="flex justify-between">
|
||||||
<span>Manuals Indexed</span>
|
<span>Manuals Indexed</span>
|
||||||
<span className="font-semibold">{liveData.semanticIndex?.total_manuals || 0}</span>
|
<span className="font-semibold">{data.semanticIndex?.total_manuals || 0}</span>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-xs text-muted-foreground mt-4">
|
<p className="text-xs text-muted-foreground mt-4">
|
||||||
Note: Using placeholder embeddings. Ready for production integration.
|
Note: Using placeholder embeddings. Ready for production integration.
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useMemo } from 'react'
|
||||||
import { PublicInset, PublicPageHeader } from '@/components/public-surface'
|
import { PublicInset, PublicPageHeader } from '@/components/public-surface'
|
||||||
import { ManualsPageShell } from '@/components/manuals-page-shell'
|
import { ManualsPageShell } from '@/components/manuals-page-shell'
|
||||||
import { groupManuals, getCategories, getManufacturers } from '@/lib/manuals-catalog'
|
import { groupManuals, getCategories, getManufacturers } from '@/lib/manuals-catalog'
|
||||||
import { fetchPublishedManualsCatalog } from '@/lib/manuals-live-catalog'
|
|
||||||
import type { Manual } from '@/lib/manuals-types'
|
import type { Manual } from '@/lib/manuals-types'
|
||||||
|
|
||||||
interface ManualsPageExperienceProps {
|
interface ManualsPageExperienceProps {
|
||||||
|
|
@ -12,34 +11,9 @@ interface ManualsPageExperienceProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ManualsPageExperience({ initialManuals }: ManualsPageExperienceProps) {
|
export function ManualsPageExperience({ initialManuals }: ManualsPageExperienceProps) {
|
||||||
const [manuals, setManuals] = useState(initialManuals)
|
const groupedManuals = useMemo(() => groupManuals(initialManuals), [initialManuals])
|
||||||
|
const manufacturers = useMemo(() => getManufacturers(initialManuals), [initialManuals])
|
||||||
useEffect(() => {
|
const categories = useMemo(() => getCategories(initialManuals), [initialManuals])
|
||||||
let cancelled = false
|
|
||||||
|
|
||||||
void (async () => {
|
|
||||||
const liveManuals = await fetchPublishedManualsCatalog()
|
|
||||||
if (cancelled || liveManuals.length === 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
setManuals((current) => {
|
|
||||||
if (liveManuals.length === current.length) {
|
|
||||||
return current
|
|
||||||
}
|
|
||||||
|
|
||||||
return liveManuals
|
|
||||||
})
|
|
||||||
})()
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
cancelled = true
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const groupedManuals = useMemo(() => groupManuals(manuals), [manuals])
|
|
||||||
const manufacturers = useMemo(() => getManufacturers(manuals), [manuals])
|
|
||||||
const categories = useMemo(() => getCategories(manuals), [manuals])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -50,14 +24,14 @@ export function ManualsPageExperience({ initialManuals }: ManualsPageExperienceP
|
||||||
>
|
>
|
||||||
<PublicInset className="inline-flex w-fit items-center gap-2 rounded-full px-4 py-2 text-sm text-muted-foreground shadow-none">
|
<PublicInset className="inline-flex w-fit items-center gap-2 rounded-full px-4 py-2 text-sm text-muted-foreground shadow-none">
|
||||||
<span>
|
<span>
|
||||||
<strong>{manuals.length}</strong> manuals available from <strong>{manufacturers.length}</strong> manufacturers
|
<strong>{initialManuals.length}</strong> manuals available from <strong>{manufacturers.length}</strong> manufacturers
|
||||||
</span>
|
</span>
|
||||||
</PublicInset>
|
</PublicInset>
|
||||||
</PublicPageHeader>
|
</PublicPageHeader>
|
||||||
|
|
||||||
<div className="mt-8 md:mt-10">
|
<div className="mt-8 md:mt-10">
|
||||||
<ManualsPageShell
|
<ManualsPageShell
|
||||||
manuals={manuals}
|
manuals={initialManuals}
|
||||||
groupedManuals={groupedManuals}
|
groupedManuals={groupedManuals}
|
||||||
manufacturers={manufacturers}
|
manufacturers={manufacturers}
|
||||||
categories={categories}
|
categories={categories}
|
||||||
|
|
|
||||||
|
|
@ -36,27 +36,67 @@ type ConvexManualDoc = {
|
||||||
const LIST_MANUALS = makeFunctionReference<"query">("manuals:list")
|
const LIST_MANUALS = makeFunctionReference<"query">("manuals:list")
|
||||||
const MANUALS_DASHBOARD = makeFunctionReference<"query">("manuals:dashboard")
|
const MANUALS_DASHBOARD = makeFunctionReference<"query">("manuals:dashboard")
|
||||||
|
|
||||||
let cachedServerConvexClient: ConvexHttpClient | null | undefined
|
let cachedServerConvexAdminClient: ConvexHttpClient | null | undefined
|
||||||
|
let cachedServerConvexPublicClient: ConvexHttpClient | null | undefined
|
||||||
|
|
||||||
function getServerConvexClient() {
|
function getServerConvexClient(useAdminAuth: boolean) {
|
||||||
if (cachedServerConvexClient !== undefined) {
|
const cacheKey = useAdminAuth ? 'admin' : 'public'
|
||||||
return cachedServerConvexClient
|
const cachedClient = useAdminAuth ? cachedServerConvexAdminClient : cachedServerConvexPublicClient
|
||||||
|
if (cachedClient !== undefined) {
|
||||||
|
return cachedClient
|
||||||
}
|
}
|
||||||
|
|
||||||
const convexUrl = process.env.CONVEX_URL || process.env.NEXT_PUBLIC_CONVEX_URL
|
const convexUrl = process.env.CONVEX_URL || process.env.NEXT_PUBLIC_CONVEX_URL
|
||||||
if (!convexUrl) {
|
if (!convexUrl) {
|
||||||
cachedServerConvexClient = null
|
if (useAdminAuth) {
|
||||||
return cachedServerConvexClient
|
cachedServerConvexAdminClient = null
|
||||||
|
return cachedServerConvexAdminClient
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedServerConvexPublicClient = null
|
||||||
|
return cachedServerConvexPublicClient
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new ConvexHttpClient(convexUrl)
|
const client = new ConvexHttpClient(convexUrl)
|
||||||
const adminKey = process.env.CONVEX_SELF_HOSTED_ADMIN_KEY
|
const adminKey = process.env.CONVEX_SELF_HOSTED_ADMIN_KEY
|
||||||
if (adminKey) {
|
if (useAdminAuth && adminKey) {
|
||||||
client.setAdminAuth(adminKey)
|
client.setAdminAuth(adminKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedServerConvexClient = client
|
if (cacheKey === 'admin') {
|
||||||
return cachedServerConvexClient
|
cachedServerConvexAdminClient = client
|
||||||
|
return cachedServerConvexAdminClient
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedServerConvexPublicClient = client
|
||||||
|
return cachedServerConvexPublicClient
|
||||||
|
}
|
||||||
|
|
||||||
|
async function queryManualsWithAuthFallback<TData>(
|
||||||
|
label: string,
|
||||||
|
queryRef: ReturnType<typeof makeFunctionReference<'query'>>,
|
||||||
|
fallback: TData,
|
||||||
|
): Promise<TData> {
|
||||||
|
const adminKey = process.env.CONVEX_SELF_HOSTED_ADMIN_KEY
|
||||||
|
const adminClient = getServerConvexClient(true)
|
||||||
|
|
||||||
|
if (adminClient) {
|
||||||
|
try {
|
||||||
|
return await adminClient.query(queryRef, {}) as TData
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[convex-service] ${label} admin query failed`, error)
|
||||||
|
if (!adminKey) {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const publicClient = getServerConvexClient(false)
|
||||||
|
if (!publicClient) {
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
return await safeFetchQuery(`${label}.public`, publicClient.query(queryRef, {}), fallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConvexOrderItem = {
|
type ConvexOrderItem = {
|
||||||
|
|
@ -167,12 +207,7 @@ export async function listConvexManuals(): Promise<Manual[]> {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getServerConvexClient()
|
const manuals = await queryManualsWithAuthFallback("manuals.list", LIST_MANUALS, [] as ConvexManualDoc[])
|
||||||
if (!client) {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
const manuals = await safeFetchQuery("manuals.list", client.query(LIST_MANUALS, {}), [] as ConvexManualDoc[])
|
|
||||||
return (manuals as ConvexManualDoc[]).map(mapConvexManual)
|
return (manuals as ConvexManualDoc[]).map(mapConvexManual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -181,12 +216,7 @@ export async function getConvexManualDashboard() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getServerConvexClient()
|
return await queryManualsWithAuthFallback("manuals.dashboard", MANUALS_DASHBOARD, null)
|
||||||
if (!client) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return await safeFetchQuery("manuals.dashboard", client.query(MANUALS_DASHBOARD, {}), null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getConvexOrderMetrics() {
|
export async function getConvexOrderMetrics() {
|
||||||
|
|
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue