| 210 | }, [viewport, isInfiniteScrollLoading]); |
| 211 | |
| 212 | async function loadMore(next: boolean = false) { |
| 213 | const key = next ? 'next' : 'prev'; |
| 214 | if (isInfiniteScrollLoading || isScrolling) return; |
| 215 | if (!cursor[key]) return; |
| 216 | try { |
| 217 | setInfiniteScrollLoading(true); |
| 218 | if (cursor[key]) { |
| 219 | const data = await api.getThreads({ |
| 220 | channelId: currentChannel.id, |
| 221 | cursor: cursor[key] || undefined, |
| 222 | accountId: currentCommunity.id, |
| 223 | }); |
| 224 | if (data) { |
| 225 | setCursor({ ...cursor, [key]: data?.nextCursor?.[key] }); |
| 226 | if (next) { |
| 227 | setThreads((threads) => [...data.threads, ...threads]); |
| 228 | } else { |
| 229 | setThreads((threads) => [...threads, ...data.threads]); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | setIsScrolling(true); |
| 234 | setInfiniteScrollLoading(false); |
| 235 | } catch (err) { |
| 236 | setError({ ...error, [key]: err }); |
| 237 | setInfiniteScrollLoading(false); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | async function loadMoreNext() { |
| 242 | return loadMore(true); |