Home: ignore paging for trending panel

This commit is contained in:
thislight 2024-08-22 15:31:15 +08:00
parent 964298f407
commit 8dc261a863
No known key found for this signature in database
GPG key ID: A50F9451AC56A63E
2 changed files with 61 additions and 20 deletions

View file

@ -1,5 +1,5 @@
import { type mastodon } from "masto"; import { type mastodon } from "masto";
import { Accessor, createEffect, createResource, createSignal } from "solid-js"; import { Accessor, createEffect, createResource } from "solid-js";
import { createStore } from "solid-js/store"; import { createStore } from "solid-js/store";
type TimelineFetchTips = { type TimelineFetchTips = {
@ -8,19 +8,27 @@ type TimelineFetchTips = {
type Timeline = { type Timeline = {
list(params: { list(params: {
maxId?: string; readonly limit?: number;
minId?: string;
}): mastodon.Paginator<mastodon.v1.Status[], unknown>; }): mastodon.Paginator<mastodon.v1.Status[], unknown>;
}; };
export function useTimeline(timeline: Accessor<Timeline>) { export function useTimeline(
timeline: Accessor<Timeline>,
cfg?: {
/**
* Use full refresh mode. This mode ignores paging, it will refetch the specified number
* of toots at every refetch().
*/
fullRefresh?: number;
},
) {
let otl: Timeline | undefined; let otl: Timeline | undefined;
let npager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined; let npager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined;
let opager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined; let opager: mastodon.Paginator<mastodon.v1.Status[], unknown> | undefined;
const [snapshot, { refetch }] = createResource< const [snapshot, { refetch }] = createResource<
{ {
records: mastodon.v1.Status[]; records: mastodon.v1.Status[];
direction: "new" | "old"; direction: "new" | "old" | "items";
tlChanged: boolean; tlChanged: boolean;
}, },
[Timeline], [Timeline],
@ -35,6 +43,20 @@ export function useTimeline(timeline: Accessor<Timeline>) {
otl = tl; otl = tl;
tlChanged = true; tlChanged = true;
} }
const fullRefresh = cfg?.fullRefresh;
if (typeof fullRefresh !== "undefined") {
const records = await tl
.list({
limit: fullRefresh,
})
.next();
return {
direction: "items",
records: records.value ?? [],
end: records.done,
tlChanged,
};
}
const direction = const direction =
typeof info.refetching !== "boolean" typeof info.refetching !== "boolean"
? (info.refetching?.direction ?? "old") ? (info.refetching?.direction ?? "old")
@ -70,10 +92,12 @@ export function useTimeline(timeline: Accessor<Timeline>) {
if (tlChanged) { if (tlChanged) {
setStore(() => []); setStore(() => []);
} }
if (direction == "new") { if (direction === "new") {
setStore((x) => [...records, ...x]); setStore((x) => [...records, ...x]);
} else if (direction == "old") { } else if (direction === "old") {
setStore((x) => [...x, ...records]); setStore((x) => [...x, ...records]);
} else if (direction === "items") {
setStore(() => records);
} }
}); });

View file

@ -9,6 +9,8 @@ import {
type ParentComponent, type ParentComponent,
children, children,
Suspense, Suspense,
Match,
Switch as JsSwitch
} from "solid-js"; } from "solid-js";
import { useDocumentTitle } from "../utils"; import { useDocumentTitle } from "../utils";
import { type mastodon } from "masto"; import { type mastodon } from "masto";
@ -49,6 +51,7 @@ const TimelinePanel: Component<{
client: mastodon.rest.Client; client: mastodon.rest.Client;
name: "home" | "public" | "trends"; name: "home" | "public" | "trends";
prefetch?: boolean; prefetch?: boolean;
fullRefetch?: number;
openFullScreenToot: ( openFullScreenToot: (
toot: mastodon.v1.Status, toot: mastodon.v1.Status,
@ -60,10 +63,12 @@ const TimelinePanel: Component<{
timeline, timeline,
snapshot, snapshot,
{ refetch: refetchTimeline, mutate: mutateTimeline }, { refetch: refetchTimeline, mutate: mutateTimeline },
] = useTimeline(() => ] = useTimeline(
() =>
props.name !== "trends" props.name !== "trends"
? props.client.v1.timelines[props.name] ? props.client.v1.timelines[props.name]
: props.client.v1.trends.statuses, : props.client.v1.trends.statuses,
{ fullRefresh: props.fullRefetch },
); );
const [expandedThreadId, setExpandedThreadId] = createSignal<string>(); const [expandedThreadId, setExpandedThreadId] = createSignal<string>();
@ -176,15 +181,26 @@ const TimelinePanel: Component<{
"justify-content": "center", "justify-content": "center",
}} }}
> >
<JsSwitch>
<Match when={snapshot.error}>
<Button <Button
variant="contained" variant="contained"
onClick={[refetchTimeline, "old"]} onClick={[refetchTimeline, "old"]}
disabled={snapshot.loading} disabled={snapshot.loading}
> >
<Show when={snapshot.error} fallback={<>Load More</>}>
Retry Retry
</Show>
</Button> </Button>
</Match>
<Match when={typeof props.fullRefetch === "undefined"}>
<Button
variant="contained"
onClick={[refetchTimeline, "old"]}
disabled={snapshot.loading}
>
Load More
</Button>
</Match>
</JsSwitch>
</div> </div>
</> </>
); );
@ -403,6 +419,7 @@ const Home: ParentComponent = (props) => {
name="trends" name="trends"
prefetch={prefetching()} prefetch={prefetching()}
openFullScreenToot={openFullScreenToot} openFullScreenToot={openFullScreenToot}
fullRefetch={120}
/> />
</div> </div>
</div> </div>