forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {createContext, useContext} from 'react'
2
3import {
4 convertBskyAppUrlIfNeeded,
5 isBskyCustomFeedUrl,
6 makeRecordUri,
7} from '#/lib/strings/url-helpers'
8import {useLiveEventPreferences} from '#/features/liveEvents/preferences'
9import {type LiveEventsWorkerResponse} from '#/features/liveEvents/types'
10
11export const DEFAULT_LIVE_EVENTS = {
12 feeds: [],
13}
14
15const Context = createContext<LiveEventsWorkerResponse>(DEFAULT_LIVE_EVENTS)
16
17export function Provider({children}: React.PropsWithChildren<{}>) {
18 return (
19 <Context.Provider value={DEFAULT_LIVE_EVENTS}>{children}</Context.Provider>
20 )
21}
22
23export async function prefetchLiveEvents() {}
24
25export function useLiveEvents() {
26 const ctx = useContext(Context)
27 if (!ctx) {
28 throw new Error('useLiveEventsContext must be used within a Provider')
29 }
30 return ctx
31}
32
33export function useUserPreferencedLiveEvents() {
34 const events = useLiveEvents()
35 const {data, isLoading} = useLiveEventPreferences()
36 if (isLoading) return DEFAULT_LIVE_EVENTS
37 const {hideAllFeeds, hiddenFeedIds} = data
38 return {
39 ...events,
40 feeds: hideAllFeeds
41 ? []
42 : events.feeds.filter(f => {
43 const hidden = f?.id ? hiddenFeedIds.includes(f?.id || '') : false
44 return !hidden
45 }),
46 }
47}
48
49export function useActiveLiveEventFeedUris() {
50 const {feeds} = useLiveEvents()
51
52 return new Set(
53 feeds
54 // insurance
55 .filter(f => isBskyCustomFeedUrl(f.url))
56 .map(f => {
57 const uri = convertBskyAppUrlIfNeeded(f.url)
58 const [_0, did, _1, rkey] = uri.split('/').filter(Boolean)
59 const urip = makeRecordUri(did, 'app.bsky.feed.generator', rkey)
60 return urip.toString()
61 }),
62 )
63}