forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.ts
More file actions
167 lines (152 loc) · 5.12 KB
/
Copy pathreact.ts
File metadata and controls
167 lines (152 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { useCallback, useEffect, useState, useRef } from 'react'
import { GlobalFeedClient } from './feed'
import { LeaderboardClient } from './leaderboard'
import { SocialSubscription } from './realtime'
import type { EchoMirrorClient } from '@echomirror/core'
import type { GlobalFeedEntry } from '@echomirror/core'
import type { LeaderboardEntry } from '@echomirror/core'
import type { LeaderboardWindow, FeedResponse, CacheConfig, SocialLiveEvent } from './types'
/**
* React hook providing global feed state with infinite-scroll support.
*
* Cache behavior: the hook creates a `GlobalFeedClient` internally, which
* owns its own `TtlCache`. All instances of `useGlobalFeed()` within the
* same component tree that share the same `EchoMirrorClient` **will NOT**
* share cache — each hook call creates its own `GlobalFeedClient`. If you
* need cache sharing, create a `GlobalFeedClient` externally, pass it via
* context, and use `useGlobalFeedWithClient()`.
*
* @example
* const { entries, isLoading, fetchMore, hasMore, refresh } = useGlobalFeed(client)
*/
export function useGlobalFeed(
client: EchoMirrorClient,
options?: { limit?: number; cache?: CacheConfig },
): {
entries: GlobalFeedEntry[]
isLoading: boolean
error: Error | null
fetchMore: () => void
refresh: () => void
hasMore: boolean
} {
const [entries, setEntries] = useState<GlobalFeedEntry[]>([])
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
const [nextCursor, setNextCursor] = useState<string | null>(null)
const feedRef = useRef<GlobalFeedClient | null>(null)
const subscriptionRef = useRef<SocialSubscription | null>(null)
// Lazy init
if (!feedRef.current) {
feedRef.current = new GlobalFeedClient(client, { cache: options?.cache })
}
const fetchInitial = useCallback(async () => {
setIsLoading(true)
setError(null)
try {
const res: FeedResponse = await feedRef.current!.fetchFeed({ limit: options?.limit })
setEntries(res.entries)
setNextCursor(res.nextCursor)
} catch (err) {
setError(err as Error)
} finally {
setIsLoading(false)
}
}, [options?.limit])
const fetchMore = useCallback(async () => {
if (!nextCursor || isLoading) return
setIsLoading(true)
try {
const res: FeedResponse = await feedRef.current!.fetchFeed({
cursor: nextCursor,
limit: options?.limit,
})
setEntries((prev) => [...prev, ...res.entries])
setNextCursor(res.nextCursor)
} catch (err) {
setError(err as Error)
} finally {
setIsLoading(false)
}
}, [nextCursor, isLoading, options?.limit])
const refresh = useCallback(async () => {
feedRef.current?.clearCache()
await fetchInitial()
}, [fetchInitial])
// Initial fetch on mount
useEffect(() => {
fetchInitial()
}, [fetchInitial])
// Real-time subscription for new feed entries
useEffect(() => {
if (!subscriptionRef.current) {
subscriptionRef.current = new SocialSubscription()
}
const sub = subscriptionRef.current
const unsubscribe = sub.subscribe((event: SocialLiveEvent) => {
if (event.type === 'feed:new_entry') {
setEntries((prev) => [event.entry, ...prev])
}
})
return unsubscribe
}, [])
return { entries, isLoading, error, fetchMore, refresh, hasMore: nextCursor !== null }
}
/**
* React hook providing leaderboard state with configurable time window.
*
* @example
* const { entries, isLoading, refresh } = useLeaderboard(client, 'daily')
*/
export function useLeaderboard(
client: EchoMirrorClient,
window: LeaderboardWindow = 'weekly',
options?: { cache?: CacheConfig },
): {
entries: LeaderboardEntry[]
isLoading: boolean
error: Error | null
refresh: () => void
} {
const [entries, setEntries] = useState<LeaderboardEntry[]>([])
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
const leaderboardRef = useRef<LeaderboardClient | null>(null)
const subscriptionRef = useRef<SocialSubscription | null>(null)
if (!leaderboardRef.current) {
leaderboardRef.current = new LeaderboardClient(client, { cache: options?.cache })
}
const fetchData = useCallback(async () => {
setIsLoading(true)
setError(null)
try {
const data = await leaderboardRef.current!.fetchLeaderboard({ window })
setEntries(data)
} catch (err) {
setError(err as Error)
} finally {
setIsLoading(false)
}
}, [window])
const refresh = useCallback(async () => {
leaderboardRef.current?.clearCache()
await fetchData()
}, [fetchData])
useEffect(() => {
fetchData()
}, [fetchData])
// Real-time subscription for leaderboard updates
useEffect(() => {
if (!subscriptionRef.current) {
subscriptionRef.current = new SocialSubscription()
}
const sub = subscriptionRef.current
const unsubscribe = sub.subscribe((event: SocialLiveEvent) => {
if (event.type === 'leaderboard:updated' && event.window === window) {
setEntries(event.entries)
}
})
return unsubscribe
}, [window])
return { entries, isLoading, error, refresh }
}