Query manuals from shared Convex directly
This commit is contained in:
parent
bab6cb2156
commit
e8fbefaa49
1 changed files with 41 additions and 2 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
|
import { ConvexHttpClient } from "convex/browser"
|
||||||
import { fetchQuery } from "convex/nextjs"
|
import { fetchQuery } from "convex/nextjs"
|
||||||
|
import { makeFunctionReference } from "convex/server"
|
||||||
import { api } from "@/convex/_generated/api"
|
import { api } from "@/convex/_generated/api"
|
||||||
import { hasConvexUrl } from "@/lib/convex-config"
|
import { hasConvexUrl } from "@/lib/convex-config"
|
||||||
import type { Product } from "@/lib/products/types"
|
import type { Product } from "@/lib/products/types"
|
||||||
|
|
@ -30,6 +32,33 @@ type ConvexManualDoc = {
|
||||||
manualUrl?: string
|
manualUrl?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const LIST_MANUALS = makeFunctionReference<"query">("manuals:list")
|
||||||
|
const MANUALS_DASHBOARD = makeFunctionReference<"query">("manuals:dashboard")
|
||||||
|
|
||||||
|
let cachedServerConvexClient: ConvexHttpClient | null | undefined
|
||||||
|
|
||||||
|
function getServerConvexClient() {
|
||||||
|
if (cachedServerConvexClient !== undefined) {
|
||||||
|
return cachedServerConvexClient
|
||||||
|
}
|
||||||
|
|
||||||
|
const convexUrl = process.env.CONVEX_URL || process.env.NEXT_PUBLIC_CONVEX_URL
|
||||||
|
if (!convexUrl) {
|
||||||
|
cachedServerConvexClient = null
|
||||||
|
return cachedServerConvexClient
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = new ConvexHttpClient(convexUrl)
|
||||||
|
const adminKey = process.env.CONVEX_SELF_HOSTED_ADMIN_KEY
|
||||||
|
if (adminKey) {
|
||||||
|
client.setAdminAuth(adminKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedServerConvexClient = client
|
||||||
|
return cachedServerConvexClient
|
||||||
|
}
|
||||||
|
|
||||||
type ConvexOrderItem = {
|
type ConvexOrderItem = {
|
||||||
productId?: string | null
|
productId?: string | null
|
||||||
productName: string
|
productName: string
|
||||||
|
|
@ -138,7 +167,12 @@ export async function listConvexManuals(): Promise<Manual[]> {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
const manuals = await safeFetchQuery("manuals.list", fetchQuery(api.manuals.list, {}), [] as ConvexManualDoc[])
|
const client = getServerConvexClient()
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,7 +181,12 @@ export async function getConvexManualDashboard() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
return await safeFetchQuery("manuals.dashboard", fetchQuery(api.manuals.dashboard, {}), null)
|
const client = getServerConvexClient()
|
||||||
|
if (!client) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return await safeFetchQuery("manuals.dashboard", client.query(MANUALS_DASHBOARD, {}), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getConvexOrderMetrics() {
|
export async function getConvexOrderMetrics() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue