From 2c675f51c8034690d524f2db3ffeb674fe355273 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Thu, 10 Oct 2024 18:37:57 -0700 Subject: [PATCH 1/2] Add refresh button on main feed page --- src/components/post/feed/feed-checker.tsx | 122 ++++++++++++++++++++++ src/components/post/feed/index.tsx | 10 +- 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/components/post/feed/feed-checker.tsx diff --git a/src/components/post/feed/feed-checker.tsx b/src/components/post/feed/feed-checker.tsx new file mode 100644 index 0000000..a1460c6 --- /dev/null +++ b/src/components/post/feed/feed-checker.tsx @@ -0,0 +1,122 @@ +import { + Component, + createMemo, + createResource, + onCleanup, + ResourceFetcherInfo, + Show, +} from "solid-js"; +import { RequestHandler } from "."; +import { useFeedContext } from "./feed-context"; +import { useSearchParams } from "@solidjs/router"; +import { useAuthContext } from "~/lib/auth-context"; +import { Button } from "~/components/ui/button"; + +export interface FeedCheckerProps { + checkHandler: RequestHandler; + delayMs: number; +} + +interface NewPostTracker { + post_id?: string; + count: number; +} + +export const FeedChecker: Component = (props) => { + const feedContext = useFeedContext(); + const authContext = useAuthContext(); + + // function to check for whether there are new posts, and update accordingly + const postChecker = async (cur: NewPostTracker) => { + let since_id = cur.post_id; + console.log(`Current post count: ${cur.count}`); + if (since_id == undefined) { + const newest_post = feedContext + .postList() + ?.find((post) => post.pinned == false); + if (newest_post == undefined) { + console.warn("Unable to get latest post info"); + return cur; + } + // Try to get newest post from the existing feed (excluding pinned) + since_id = newest_post.id; + } + + const params = { + local: false, + limit: 25, + since_id: since_id, + }; + + let response = await props.checkHandler(authContext, params); + if (response == null) { + console.warn( + "While checking for new posts, got null from handler function" + ); + return cur; + } + + if (response.data.length > 0) { + const topLevelPosts = response.data.filter( + (post) => + post.in_reply_to_id == null && + post.reblog?.in_reply_to_id == null + ); + console.log(`Found ${topLevelPosts.length} top-level posts`); + cur.count += topLevelPosts.length; + cur.post_id = response.data[0].id; + } + console.log(`Returning new count ${cur.count}`); + return cur; + }; + + // resource to call said post checker + const [newPostInfo, postInfoActions] = createResource( + () => { + return { + count: 0, + }; + }, + (cur: NewPostTracker, info: ResourceFetcherInfo) => + postChecker(info.value ?? cur) + ); + + const checkTimer = setInterval(() => { + if ((newPostInfo()?.count ?? 0) == 0) { + console.log("checking for new posts..."); + postInfoActions.refetch(); + } + }, props.delayMs); + + onCleanup(() => { + console.log("cleaning up..."); + clearInterval(checkTimer); + }); + + const newPosts = createMemo((last) => { + if (newPostInfo.loading) { + return last; + } + + const postInfo = newPostInfo()!; + console.log(`newPostInfo exists! Count is ${postInfo.count}`); + return postInfo.count > 0; + }, false); + + return ( + +
+ +
+
+ ); +}; diff --git a/src/components/post/feed/index.tsx b/src/components/post/feed/index.tsx index 1820831..38395f6 100644 --- a/src/components/post/feed/index.tsx +++ b/src/components/post/feed/index.tsx @@ -8,12 +8,14 @@ import { createSignal, ErrorBoundary, For, + Show, } from "solid-js"; import { AuthProviderProps, useAuthContext } from "~/lib/auth-context"; import Post from ".."; import { PageNav } from "~/components/ui/page-footer"; import { Button } from "~/components/ui/button"; import { FeedContext } from "./feed-context"; +import { FeedChecker } from "./feed-checker"; interface GetTimelineOptionsApi { local?: boolean; @@ -25,7 +27,7 @@ interface GetTimelineOptionsApi { export interface GetTimelineOptions extends GetTimelineOptionsApi {} -type RequestHandler = ( +export type RequestHandler = ( authContext: AuthProviderProps, timelineOptions: GetTimelineOptions ) => Promise> | undefined> | undefined; @@ -100,6 +102,12 @@ export const PostFeed: Component = (props) => {
Failed to load posts.
}> + + + {(status, index) => ( From 5b1e258c287004794076a29de1568aba4a71ed80 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Fri, 11 Oct 2024 17:15:15 -0700 Subject: [PATCH 2/2] FeedChecker: use `variant` instead of `type` --- src/components/post/feed/feed-checker.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/post/feed/feed-checker.tsx b/src/components/post/feed/feed-checker.tsx index a1460c6..f9a0d54 100644 --- a/src/components/post/feed/feed-checker.tsx +++ b/src/components/post/feed/feed-checker.tsx @@ -107,7 +107,7 @@ export const FeedChecker: Component = (props) => {