perf: Lazy loading for links in topic sections (#127)

This commit is contained in:
Anselm Eickhoff
2024-09-03 11:27:17 +01:00
committed by GitHub
parent c4d4afd4df
commit d2a3baa9a2
7 changed files with 74 additions and 33 deletions

View File

@@ -1,29 +1,15 @@
import { useMemo } from "react"
import { useCoState } from "@/lib/providers/jazz-provider"
import { PublicGlobalGroup } from "@/lib/schema/master/public-group"
import { ID } from "jazz-tools"
import { Link } from "@/lib/schema"
import { Account, ID } from "jazz-tools"
import { Link, Topic } from "@/lib/schema"
const GLOBAL_GROUP_ID = process.env.NEXT_PUBLIC_JAZZ_GLOBAL_GROUP as ID<PublicGlobalGroup>
export function useTopicData(topicName: string) {
const group = useCoState(PublicGlobalGroup, GLOBAL_GROUP_ID, {
root: { topics: [] }
})
export function useTopicData(topicName: string, me: Account | undefined) {
const topicID = useMemo(() => me && Topic.findUnique({topicName}, GLOBAL_GROUP_ID, me), [topicName, me])
// const topic = useCoState(Topic, "co_zS3TH4Lkj5MK9GEehinxhjjNTxB" as ID<Topic>, {})
const topic = useMemo(
() => group?.root.topics.find(topic => topic?.name === topicName),
[group?.root.topics, topicName]
)
const topic = useCoState(Topic, topicID, {latestGlobalGuide: {sections: [{links: []}]}})
const allLinks = useMemo(() => {
if (!topic?.latestGlobalGuide?.sections) return []
return topic.latestGlobalGuide.sections.flatMap(
section => section?.links?.filter((link): link is Link => !!link?.url) ?? []
)
}, [topic?.latestGlobalGuide?.sections])
return { topic, allLinks }
return { topic }
}