mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-16 08:36:42 +01:00
32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
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 { LaAccount, Link, Topic } from "@/lib/schema"
|
|
|
|
const GLOBAL_GROUP_ID = process.env.NEXT_PUBLIC_JAZZ_GLOBAL_GROUP as ID<PublicGlobalGroup>
|
|
|
|
export function useTopicData(topicName: string, me?: LaAccount) {
|
|
const findTopic = useMemo(() => me && Topic.findUnique({ topicName }, GLOBAL_GROUP_ID, me), [me])
|
|
const topic = useCoState(Topic, findTopic, { latestGlobalGuide: { sections: [{ links: [{}] }] } })
|
|
|
|
const transformedData = useMemo(() => {
|
|
if (!topic?.latestGlobalGuide?.sections) return []
|
|
|
|
return topic.latestGlobalGuide.sections.flatMap(section => [
|
|
{ type: "section", data: section },
|
|
...(section?.links?.filter(link => !!link?.url).map(link => ({ type: "link", data: link })) || [])
|
|
])
|
|
}, [topic?.latestGlobalGuide?.sections])
|
|
|
|
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, transformedData }
|
|
}
|