From 941bca203d7846221c5e8ea65eaaffea4c1ff62a Mon Sep 17 00:00:00 2001 From: Patrick O'Sullivan Date: Wed, 8 Nov 2023 15:46:22 -0600 Subject: [PATCH 1/5] channels: fix various cache issues --- ui/src/app.tsx | 2 +- ui/src/diary/DiaryNote.tsx | 12 +- ui/src/heap/HeapChannel.tsx | 2 +- .../HeapDetailSidebar/HeapDetailComments.tsx | 9 +- ui/src/state/channel/channel.ts | 443 +++++++++++------- ui/src/types/channel.ts | 24 +- 6 files changed, 309 insertions(+), 183 deletions(-) diff --git a/ui/src/app.tsx b/ui/src/app.tsx index d4c9c65732..8b84d41654 100644 --- a/ui/src/app.tsx +++ b/ui/src/app.tsx @@ -569,7 +569,7 @@ function GroupsRoutes({ state, location, isMobile, isSmall }: RoutesProps) { element={} /> } /> v !== null).reverse() - : []; // natural reading order + ? replies + .filter(([k, v]) => v !== null) + .sort(([a], [b]) => { + if (sort === 'asc') { + return a.toString().localeCompare(b.toString()); + } + + return b.toString().localeCompare(a.toString()); + }) + : []; const canWrite = canWriteChannel(perms, vessel, group?.bloc); const { title: noteTitle, image } = getKindDataFromEssay(note.essay); const groupedReplies = setNewDaysForReplies( diff --git a/ui/src/heap/HeapChannel.tsx b/ui/src/heap/HeapChannel.tsx index f9fc1ee70f..45ffc67316 100644 --- a/ui/src/heap/HeapChannel.tsx +++ b/ui/src/heap/HeapChannel.tsx @@ -85,7 +85,7 @@ function HeapChannel({ title }: ViewProps) { const empty = useMemo(() => posts.length === 0, [posts]); const sortedPosts = posts - .filter((k, v) => v !== null) + .filter(([k, v]) => v !== null) .sort(([a], [b]) => { if (sortMode === 'time') { return b.compare(a); diff --git a/ui/src/heap/HeapDetail/HeapDetailSidebar/HeapDetailComments.tsx b/ui/src/heap/HeapDetail/HeapDetailSidebar/HeapDetailComments.tsx index 50482c762e..7a00f84934 100644 --- a/ui/src/heap/HeapDetail/HeapDetailSidebar/HeapDetailComments.tsx +++ b/ui/src/heap/HeapDetail/HeapDetailSidebar/HeapDetailComments.tsx @@ -29,10 +29,17 @@ export default function HeapDetailComments({ const vessel = useVessel(groupFlag, window.our); const canWrite = canWriteChannel(perms, vessel, group?.bloc); const unread = useUnread(nest); + const sortedComments = + comments?.sort(([a], [b]) => { + if (sort === 'asc') { + return a.toString().localeCompare(b.toString()); + } + return b.toString().localeCompare(a.toString()); + }) ?? []; const groupedReplies = !comments ? [] : setNewDaysForReplies( - groupReplies(time, comments, unread).sort(([a], [b]) => { + groupReplies(time, sortedComments, unread).sort(([a], [b]) => { if (sort === 'asc') { return a.localeCompare(b); } diff --git a/ui/src/state/channel/channel.ts b/ui/src/state/channel/channel.ts index e7c6a4b887..80be82fb96 100644 --- a/ui/src/state/channel/channel.ts +++ b/ui/src/state/channel/channel.ts @@ -28,7 +28,6 @@ import { Nest, PageMap, newPostMap, - newReplyMap, PageTuple, UnreadUpdate, PagedPosts, @@ -42,12 +41,6 @@ import { HiddenPosts, TogglePost, } from '@/types/channel'; -import { - extendCurrentWindow, - getWindow, - Window, - WindowSet, -} from '@/logic/windows'; import api from '@/api'; import { checkNest, log, nestToFlag, restoreMap } from '@/logic/utils'; import useReactQuerySubscription from '@/logic/useReactQuerySubscription'; @@ -63,22 +56,26 @@ async function updatePostInCache( updater: (post: PostDataResponse | undefined) => PostDataResponse | undefined ) { const [han, flag] = nestToFlag(variables.nest); - await queryClient.cancelQueries({ - queryKey: [han, 'posts', flag, variables.postId], - exact: true, - }); + await queryClient.cancelQueries([han, 'posts', flag, variables.postId]); queryClient.setQueryData([han, 'posts', flag, variables.postId], updater); } +interface PostsInCachePrev { + pages: PagedPosts[]; + pageParams: PageParam[]; +} + async function updatePostsInCache( variables: { nest: Nest }, - updater: (posts: Posts | undefined) => Posts | undefined + updater: ( + prev: PostsInCachePrev | undefined + ) => { pageParams: PageParam[]; pages: PagedPosts[] } | undefined ) { const [han, flag] = nestToFlag(variables.nest); - await queryClient.cancelQueries([han, 'posts', flag]); + await queryClient.cancelQueries([han, 'posts', flag, 'infinite']); - queryClient.setQueryData([han, 'posts', flag], updater); + queryClient.setQueryData([han, 'posts', flag, 'infinite'], updater); } export function channelAction( @@ -106,10 +103,6 @@ export function channelPostAction(nest: Nest, action: PostAction) { }); } -export interface PostWindows { - [nest: string]: WindowSet; -} - export type PostStatus = 'pending' | 'sent' | 'delivered'; export interface TrackedPost { @@ -124,17 +117,13 @@ export interface CacheId { export interface State { trackedPosts: TrackedPost[]; - postWindows: PostWindows; addTracked: (id: CacheId) => void; updateStatus: (id: CacheId, status: PostStatus) => void; - getCurrentWindow: (nest: string, time?: string) => Window | undefined; - extendCurrentWindow: (nest: Nest, newWindow: Window, time?: string) => void; [key: string]: unknown; } export const usePostsStore = create((set, get) => ({ trackedPosts: [], - postWindows: {}, addTracked: (id) => { set((state) => ({ trackedPosts: [{ status: 'pending', cacheId: id }, ...state.trackedPosts], @@ -152,33 +141,8 @@ export const usePostsStore = create((set, get) => ({ }), })); }, - getCurrentWindow: (nest, time) => { - const currentSet = get().postWindows[nest]; - return getWindow(currentSet, time); - }, - extendCurrentWindow: (nest, newWindow, time) => { - set((state) => { - const currentSet = state.postWindows[nest]; - - return { - postWindows: { - ...state.postWindows, - [nest]: extendCurrentWindow(newWindow, currentSet, time), - }, - }; - }); - }, })); -export function useCurrentWindow(nest: Nest, time?: string) { - const getCurrentWindow = useCallback( - () => usePostsStore.getState().getCurrentWindow(nest, time), - [time, nest] - ); - - return getCurrentWindow(); -} - export function useTrackedPosts() { return usePostsStore((s) => s.trackedPosts); } @@ -326,7 +290,7 @@ const infinitePostUpdater = ( const postResponse = response.post['r-post']; const { id } = response.post; - const time = bigInt(udToDec(id)); + const time = decToUd(id); if ('set' in postResponse) { const post = postResponse.set; @@ -335,100 +299,98 @@ const infinitePostUpdater = ( queryClient.setQueryData<{ pages: PagedPosts[]; pageParams: PageParam[]; - }>( - queryKey, - (d: { pages: PagedPosts[]; pageParams: PageParam[] } | undefined) => { - if (d === undefined) { - return undefined; - } + }>(queryKey, (d: PostsInCachePrev | undefined) => { + if (d === undefined) { + return undefined; + } - const newPages = d.pages.map((page) => { - const newPage = { - ...page, - }; + const newPages = d.pages.map((page) => { + const newPage = { + ...page, + }; - const inPage = - Object.keys(newPage.posts).some((k) => k === time.toString()) ?? - false; + const inPage = + Object.keys(newPage.posts).some((k) => k === time) ?? false; - if (inPage) { - const pagePosts = { ...newPage.posts }; + if (inPage) { + const pagePosts = { ...newPage.posts }; - pagePosts[time.toString()] = null; + pagePosts[time] = null; - newPage.posts = pagePosts; - } + newPage.posts = pagePosts; + } - return newPage; - }); + return newPage; + }); - return { - pages: newPages, - pageParams: d.pageParams, - }; - } - ); + return { + pages: newPages, + pageParams: d.pageParams, + }; + }); } else { queryClient.setQueryData<{ pages: PagedPosts[]; pageParams: PageParam[]; - }>( - queryKey, - (d: { pages: PagedPosts[]; pageParams: PageParam[] } | undefined) => { - if (d === undefined) { - return { - pages: [ - { - posts: { - [time.toString()]: post, - }, - newer: null, - older: null, - total: 1, + }>(queryKey, (d: PostsInCachePrev | undefined) => { + if (d === undefined) { + return { + pages: [ + { + posts: { + [time]: post, }, - ], - pageParams: [], - }; - } - - const lastPage = _.last(d.pages); - - if (lastPage === undefined) { - return undefined; - } - - const newPosts = { - ...lastPage.posts, - [time.toString()]: post, + newer: null, + older: null, + total: 1, + }, + ], + pageParams: [], }; + } - const newLastPage = { - ...lastPage, - posts: newPosts, - }; + const lastPage = _.last(d.pages); - const cachedPost = - lastPage.posts[unixToDa(post.essay.sent).toString()]; + if (lastPage === undefined) { + return undefined; + } - if (cachedPost && id !== unixToDa(post.essay.sent).toString()) { - // remove cached post if it exists - delete newLastPage.posts[unixToDa(post.essay.sent).toString()]; + const newPosts = { + ...lastPage.posts, + [time]: post, + }; - // set delivered now that we have the real post - usePostsStore - .getState() - .updateStatus( - { author: post.essay.author, sent: post.essay.sent }, - 'delivered' - ); - } + const newLastPage = { + ...lastPage, + posts: newPosts, + }; - return { - pages: [...d.pages.slice(0, -1), newLastPage], - pageParams: d.pageParams, - }; + const cachedPost = + lastPage.posts[decToUd(unixToDa(post.essay.sent).toString())]; + + if ( + cachedPost && + id !== decToUd(unixToDa(post.essay.sent).toString()) + ) { + // remove cached post if it exists + delete newLastPage.posts[ + decToUd(unixToDa(post.essay.sent).toString()) + ]; + + // set delivered now that we have the real post + usePostsStore + .getState() + .updateStatus( + { author: post.essay.author, sent: post.essay.sent }, + 'delivered' + ); } - ); + + return { + pages: [...d.pages.slice(0, -1), newLastPage], + pageParams: d.pageParams, + }; + }); } } else if ('reacts' in postResponse) { queryClient.setQueryData<{ @@ -449,15 +411,14 @@ const infinitePostUpdater = ( }; const inPage = - Object.keys(newPage.posts).some((k) => k === time.toString()) ?? - false; + Object.keys(newPage.posts).some((k) => k === time) ?? false; if (inPage) { - const post = newPage.posts[time.toString()]; + const post = newPage.posts[time]; if (!post) { return newPage; } - newPage.posts[time.toString()] = { + newPage.posts[time] = { ...post, seal: { ...post.seal, @@ -496,15 +457,14 @@ const infinitePostUpdater = ( }; const inPage = - Object.keys(newPage.posts).some((k) => k === time.toString()) ?? - false; + Object.keys(newPage.posts).some((k) => k === time) ?? false; if (inPage) { - const post = newPage.posts[time.toString()]; + const post = newPage.posts[time]; if (!post) { return page; } - newPage.posts[time.toString()] = { + newPage.posts[time] = { ...post, essay, }; @@ -676,10 +636,6 @@ export function useInfinitePosts(nest: Nest, initialTime?: string) { }; } - // const posts: PageTuple[] = data.pages - // .map((page) => page.posts.toArray()) - // .flat(); - const posts: PageTuple[] = data.pages .map((page) => { const pagePosts = Object.entries(page.posts).map( @@ -1455,23 +1411,56 @@ export function useEditPostMutation() { }; }; - const postsUpdater = (prev: Posts | undefined) => { + const postsUpdater = (prev: PostsInCachePrev | undefined) => { if (prev === undefined) { return prev; } - const prevPost = prev[decToUd(variables.time)]; + if (prev.pages === undefined) { + return prev; + } + + const allPostsInCache = prev.pages.flatMap((page) => + Object.entries(page.posts) + ); + + const prevPost = allPostsInCache.find( + ([k]) => k === decToUd(variables.time) + )?.[1]; - if (prevPost === null) { + if (prevPost === null || prevPost === undefined) { + return prev; + } + + const pageInCache = prev.pages.find((page) => + Object.keys(page.posts).some((k) => k === decToUd(variables.time)) + ); + + const pageInCacheIdx = prev.pages.findIndex((page) => + Object.keys(page.posts).some((k) => k === decToUd(variables.time)) + ); + + if (pageInCache === undefined) { return prev; } return { ...prev, - [variables.time]: { - seal: prevPost.seal, - essay: variables.essay, - }, + pages: [ + ...prev.pages.slice(0, pageInCacheIdx), + { + ...pageInCache, + posts: { + ...pageInCache?.posts, + [decToUd(variables.time)]: { + ...prevPost, + essay: variables.essay, + seal: prevPost.seal, + }, + }, + }, + ...prev.pages.slice(pageInCacheIdx + 1), + ], }; }; @@ -1485,6 +1474,16 @@ export function useEditPostMutation() { await updatePostsInCache(variables, postsUpdater); }, + onSettled: async (_data, _error, variables) => { + const [han, flag] = nestToFlag(variables.nest); + await queryClient.invalidateQueries({ + queryKey: [han, 'posts', flag, variables.time], + refetchType: 'none', + }); + // await queryClient.invalidateQueries({ + // queryKey: [han, 'posts', flag, 'infinite'], + // }); + }, }); } @@ -1515,29 +1514,67 @@ export function useDeletePostMutation() { return useMutation({ mutationFn, onMutate: async (variables) => { - const [han, flag] = nestToFlag(variables.nest); - - const updater = (prev: Posts | undefined) => { + const updater = (prev: PostsInCachePrev | undefined) => { if (prev === undefined) { return prev; } - const { [decToUd(variables.time)]: _n, ...rest } = prev; + if (prev.pages === undefined) { + return prev; + } + + const allPostsInCache = prev.pages.flatMap((page) => + Object.entries(page.posts) + ); + + const prevPost = allPostsInCache.find( + ([k]) => k === decToUd(variables.time) + )?.[1]; + + if (prevPost === null || prevPost === undefined) { + return prev; + } + + const pageInCache = prev.pages.find((page) => + Object.keys(page.posts).some((k) => k === decToUd(variables.time)) + ); - return rest; + const pageInCacheIdx = prev.pages.findIndex((page) => + Object.keys(page.posts).some((k) => k === decToUd(variables.time)) + ); + + if (pageInCache === undefined) { + return prev; + } + + return { + ...prev, + pages: [ + ...prev.pages.slice(0, pageInCacheIdx), + { + ...pageInCache, + posts: Object.fromEntries( + Object.entries(pageInCache.posts).filter( + ([k]) => k !== decToUd(variables.time) + ) + ), + }, + ...prev.pages.slice(pageInCacheIdx + 1), + ], + }; }; await updatePostsInCache(variables, updater); - - await queryClient.cancelQueries([han, 'posts', flag, variables.time]); }, onSuccess: async (_data, variables) => { removePostFromInfiniteQuery(variables.nest, variables.time); }, onSettled: async (_data, _error, variables) => { const [han, flag] = nestToFlag(variables.nest); - await queryClient.invalidateQueries([han, 'posts', flag]); - await queryClient.invalidateQueries([han, 'posts', flag, 'infinite']); + setTimeout(async () => { + await queryClient.invalidateQueries([han, 'posts', flag]); + await queryClient.invalidateQueries([han, 'posts', flag, 'infinite']); + }, 3000); }, }); } @@ -1701,15 +1738,22 @@ export function useAddReplyMutation() { return useMutation({ mutationFn, onMutate: async (variables) => { - const postsUpdater = (prev: Record | undefined) => { + const postsUpdater = (prev: PostsInCachePrev | undefined) => { if (prev === undefined) { return prev; } const replying = decToUd(variables.postId); - if (replying in prev) { - const replyingPost = prev[replying] as Post; - if (replyingPost === null) { + + const allPostsInCache = prev.pages.flatMap((page) => + Object.entries(page.posts) + ); + + if (replying in allPostsInCache) { + const replyingPost = allPostsInCache.find( + ([k]) => k === replying + )?.[1]; + if (replyingPost === null || replyingPost === undefined) { return prev; } @@ -1722,9 +1766,31 @@ export function useAddReplyMutation() { }, }; + const pageInCache = prev.pages.find((page) => + Object.keys(page.posts).some((k) => k === decToUd(replying)) + ); + + const pageInCacheIdx = prev.pages.findIndex((page) => + Object.keys(page.posts).some((k) => k === decToUd(replying)) + ); + + if (pageInCache === undefined) { + return prev; + } + return { ...prev, - [replying]: updatedPost, + pages: [ + ...prev.pages.slice(0, pageInCacheIdx), + { + ...pageInCache, + posts: { + ...pageInCache?.posts, + [decToUd(replying)]: updatedPost, + }, + }, + ...prev.pages.slice(pageInCacheIdx + 1), + ], }; } return prev; @@ -1741,7 +1807,7 @@ export function useAddReplyMutation() { [decToUd(unixToDa(dateTime).toString())]: { seal: { id: unixToDa(dateTime).toString(), - 'parent-id': decToUd(variables.postId), + 'parent-id': variables.postId, reacts: {}, }, memo: { @@ -1768,8 +1834,16 @@ export function useAddReplyMutation() { }, onSettled: async (_data, _error, variables) => { const [han, flag] = nestToFlag(variables.nest); - await queryClient.refetchQueries([han, 'posts', flag]); - await queryClient.refetchQueries([han, 'posts', flag, variables.postId]); + setTimeout(async () => { + // TODO: this is a hack to make sure the post is updated before refetching + // the queries. We need to figure out why the post is not updated immediately. + await queryClient.refetchQueries([ + han, + 'posts', + flag, + variables.postId, + ]); + }, 300); }, }); } @@ -1799,15 +1873,22 @@ export function useDeleteReplyMutation() { return useMutation({ mutationFn, onMutate: async (variables) => { - const postsUpdater = (prev: Record | undefined) => { + const postsUpdater = (prev: PostsInCachePrev | undefined) => { if (prev === undefined) { return prev; } const replying = decToUd(variables.postId); - if (replying in prev) { - const replyingPost = prev[replying] as Post; - if (replyingPost === null) { + const allPostsInCache = prev.pages.flatMap((page) => + Object.entries(page.posts) + ); + + if (replying in allPostsInCache) { + const replyingPost = allPostsInCache.find( + ([k]) => k === replying + )?.[1]; + + if (replyingPost === null || replyingPost === undefined) { return prev; } @@ -1822,9 +1903,31 @@ export function useDeleteReplyMutation() { }, }; + const pageInCache = prev.pages.find((page) => + Object.keys(page.posts).some((k) => k === decToUd(replying)) + ); + + const pageInCacheIdx = prev.pages.findIndex((page) => + Object.keys(page.posts).some((k) => k === decToUd(replying)) + ); + + if (pageInCache === undefined) { + return prev; + } + return { ...prev, - [replying]: updatedPost, + pages: [ + ...prev.pages.slice(0, pageInCacheIdx), + { + ...pageInCache, + posts: { + ...pageInCache?.posts, + [decToUd(replying)]: updatedPost, + }, + }, + ...prev.pages.slice(pageInCacheIdx + 1), + ], }; } return prev; @@ -1855,8 +1958,16 @@ export function useDeleteReplyMutation() { }, onSettled: async (_data, _error, variables) => { const [han, flag] = nestToFlag(variables.nest); - await queryClient.refetchQueries([han, 'posts', flag]); - await queryClient.refetchQueries([han, 'posts', flag, variables.postId]); + setTimeout(async () => { + // TODO: this is a hack to make sure the post is updated before refetching + // the queries. We need to figure out why the post is not updated immediately. + await queryClient.refetchQueries([ + han, + 'posts', + flag, + variables.postId, + ]); + }, 300); }, }); } diff --git a/ui/src/types/channel.ts b/ui/src/types/channel.ts index 2917ccefb6..32700f6eb0 100644 --- a/ui/src/types/channel.ts +++ b/ui/src/types/channel.ts @@ -217,22 +217,22 @@ export interface Replies { [id: string]: Reply; } -interface PageActionAdd { +interface PostActionAdd { add: PostEssay; } -interface PageActionEdit { +interface PostActionEdit { edit: { id: string; essay: PostEssay; }; } -interface PageActionDel { +interface PostActionDel { del: string; } -interface PageActionAddReact { +interface PostActionAddReact { 'add-react': { id: string; react: string; @@ -240,7 +240,7 @@ interface PageActionAddReact { }; } -interface PageActionDelReact { +interface PostActionDelReact { 'del-react': { id: string; ship: string; @@ -271,11 +271,11 @@ interface PostActionReply { } export type PostAction = - | PageActionAdd - | PageActionEdit - | PageActionDel - | PageActionAddReact - | PageActionDelReact + | PostActionAdd + | PostActionEdit + | PostActionDel + | PostActionAddReact + | PostActionDelReact | PostActionReply; export interface DiffView { @@ -300,8 +300,8 @@ export interface ReplyActionDel { export type ReplyAction = | ReplyActionAdd | ReplyActionDel - | PageActionAddReact - | PageActionDelReact; + | PostActionAddReact + | PostActionDelReact; export type DisplayMode = 'list' | 'grid'; From 9f65c428fbace451a09035c6cfe1780ae0c24cf4 Mon Sep 17 00:00:00 2001 From: Patrick O'Sullivan Date: Wed, 8 Nov 2023 16:01:11 -0600 Subject: [PATCH 2/5] channels: uncomment a query invalidation in onsettled in useEditPostMutation --- ui/src/state/channel/channel.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ui/src/state/channel/channel.ts b/ui/src/state/channel/channel.ts index 80be82fb96..2cff4236e6 100644 --- a/ui/src/state/channel/channel.ts +++ b/ui/src/state/channel/channel.ts @@ -1480,9 +1480,9 @@ export function useEditPostMutation() { queryKey: [han, 'posts', flag, variables.time], refetchType: 'none', }); - // await queryClient.invalidateQueries({ - // queryKey: [han, 'posts', flag, 'infinite'], - // }); + await queryClient.invalidateQueries({ + queryKey: [han, 'posts', flag, 'infinite'], + }); }, }); } From be99f3539c48b67942712667a40403d87006fab4 Mon Sep 17 00:00:00 2001 From: Patrick O'Sullivan Date: Thu, 9 Nov 2023 05:41:05 -0600 Subject: [PATCH 3/5] channels: fix issue with editing note titles, fixes LAND-1161 --- ui/src/diary/diary-add-note.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ui/src/diary/diary-add-note.tsx b/ui/src/diary/diary-add-note.tsx index 4f7b100d14..f56dc25813 100644 --- a/ui/src/diary/diary-add-note.tsx +++ b/ui/src/diary/diary-add-note.tsx @@ -143,7 +143,11 @@ export default function DiaryAddNote() { time: id, essay: { ...note.essay, - ...values, + 'kind-data': { + diary: { + ...values, + }, + }, content: noteContent, }, }); From 1752be248289eaf62266934922d963850a4adfd5 Mon Sep 17 00:00:00 2001 From: Patrick O'Sullivan Date: Thu, 9 Nov 2023 05:50:53 -0600 Subject: [PATCH 4/5] channels: another LAND-1154 related fix --- ui/src/heap/EditCurioForm.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ui/src/heap/EditCurioForm.tsx b/ui/src/heap/EditCurioForm.tsx index 7e5fa27d10..2859e82608 100644 --- a/ui/src/heap/EditCurioForm.tsx +++ b/ui/src/heap/EditCurioForm.tsx @@ -18,6 +18,7 @@ import { useChannelFlag } from '@/logic/channel'; import { useRouteGroup } from '@/state/groups'; import { chatStoryFromStory, storyFromChatStory } from '@/types/channel'; import getKindDataFromEssay from '@/logic/getKindData'; +import LoadingSpinner from '@/components/LoadingSpinner/LoadingSpinner'; import HeapTextInput from './HeapTextInput'; type EditCurioFormSchema = { @@ -177,6 +178,14 @@ export default function EditCurioForm() { } }, [isLoading, isLinkMode, contentAsChatStory.inline, draftText]); + if (isLoading) { + return ( +
+ +
+ ); + } + return ( <>
From c9a34b0d9872f289a05ab2515498d21992abbe30 Mon Sep 17 00:00:00 2001 From: Patrick O'Sullivan Date: Thu, 9 Nov 2023 09:13:57 -0600 Subject: [PATCH 5/5] channels: remove unused state hooks, disable usePost if the id is an empty string --- ui/src/state/channel/channel.ts | 83 +-------------------------------- 1 file changed, 1 insertion(+), 82 deletions(-) diff --git a/ui/src/state/channel/channel.ts b/ui/src/state/channel/channel.ts index 2cff4236e6..9ee67e3cdf 100644 --- a/ui/src/state/channel/channel.ts +++ b/ui/src/state/channel/channel.ts @@ -168,36 +168,6 @@ export function useTrackedPostStatus(cacheId: CacheId) { ); } -export function usePosts(nest: Nest) { - const [han, flag] = nestToFlag(nest); - const { data, ...rest } = useReactQuerySubscription({ - queryKey: [han, 'posts', flag], - app: 'channels', - path: `/${nest}`, - scry: `/${nest}/posts/newest/${INITIAL_MESSAGE_FETCH_PAGE_SIZE}/outline`, - priority: 2, - }); - - if (data === undefined || Object.entries(data).length === 0) { - return { - posts: newPostMap(), - ...rest, - }; - } - - const diff: [BigInteger, Post][] = Object.entries(data).map(([k, v]) => [ - bigInt(udToDec(k)), - v as Post, - ]); - - const postsMap = newPostMap(diff); - - return { - posts: postsMap as PageMap, - ...rest, - }; -} - export function usePostsOnHost( nest: Nest, enabled: boolean @@ -226,57 +196,6 @@ export function usePostsOnHost( return data as Posts; } -export function useOlderPosts(nest: Nest, count: number, enabled = false) { - checkNest(nest); - const { posts } = usePosts(nest); - - let postMap = restoreMap(posts); - - const index = postMap.peekSmallest()?.[0]; - const oldPostsSize = postMap.size ?? 0; - - const fetchStart = index ? decToUd(index.toString()) : decToUd('0'); - - const [han, flag] = nestToFlag(nest); - - const { data, ...rest } = useReactQueryScry({ - queryKey: [han, 'posts', flag, 'older', fetchStart], - app: 'channels', - path: `/${nest}/posts/older/${fetchStart}/${count}/outline`, - priority: 2, - options: { - enabled: - enabled && - index !== undefined && - oldPostsSize !== 0 && - !!fetchStart && - fetchStart !== decToUd('0'), - }, - }); - - if ( - rest.isError || - data === undefined || - Object.entries(data as object).length === 0 || - !enabled - ) { - return false; - } - - const diff = Object.entries(data as object).map(([k, v]) => ({ - tim: bigInt(udToDec(k)), - post: v as Post, - })); - - diff.forEach(({ tim, post }) => { - postMap = postMap.set(tim, post); - }); - - queryClient.setQueryData([han, 'posts', flag], postMap.root); - - return rest.isLoading; -} - const infinitePostUpdater = ( queryKey: QueryKey, data: ChannelsResponse, @@ -815,7 +734,7 @@ export function usePost(nest: Nest, postId: string, disabled = false) { ); const enabled = useMemo( - () => postId !== '0' && nest !== '' && !disabled, + () => postId !== '0' && postId !== '' && nest !== '' && !disabled, [postId, nest, disabled] ); const { data, ...rest } = useReactQueryScry({