Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions apps/web/components/memories-grid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use client"

Check failure on line 1 in apps/web/components/memories-grid.tsx

View workflow job for this annotation

GitHub Actions / Quality Checks

format

File content differs from formatting output

import { useAuth } from "@lib/auth-context"
import { $fetch } from "@lib/api"
Expand Down Expand Up @@ -73,6 +73,36 @@
image?: string
}

const ogCache = new Map<string, OgData>()
const ogInflight = new Map<string, Promise<OgData | null>>()

function fetchOgData(url: string): Promise<OgData | null> {
const cached = ogCache.get(url)
if (cached) return Promise.resolve(cached)

const inflight = ogInflight.get(url)
if (inflight) return inflight

const promise = fetch(`/api/og?url=${encodeURIComponent(url)}`)
.then((res) => {
if (!res.ok) throw new Error("Failed")
return res.json()
})
.then((data) => {
const result: OgData = { title: data?.title, image: data?.image }
ogCache.set(url, result)
Comment thread
ved015 marked this conversation as resolved.
ogInflight.delete(url)
return result
})
.catch(() => {
ogInflight.delete(url)
return null
Comment thread
entelligence-ai-pr-reviews[bot] marked this conversation as resolved.
})

ogInflight.set(url, promise)
return promise
}

const PAGE_SIZE = 100
const MAX_TOTAL = 1000

Expand Down Expand Up @@ -696,23 +726,9 @@
useEffect(() => {
if (needsOgData && !ogData && !isLoadingOg && document.url) {
setIsLoadingOg(true)
fetch(`/api/og?url=${encodeURIComponent(document.url)}`)
.then((res) => {
if (!res.ok) throw new Error("Failed")
return res.json()
})
.then((data) => {
setOgData({
title: data?.title,
image: data?.image,
})
})
.catch(() => {
setOgData({})
})
.finally(() => {
setIsLoadingOg(false)
})
fetchOgData(document.url)
.then((data) => { if (data) setOgData(data) })
.finally(() => setIsLoadingOg(false))
Comment thread
ved015 marked this conversation as resolved.
Outdated
}
}, [needsOgData, ogData, isLoadingOg, document.url])
Comment thread
ved015 marked this conversation as resolved.
Outdated

Expand Down
Loading