TootBottomSheet: use new cache semantic

This commit is contained in:
thislight 2024-12-26 20:09:03 +08:00
parent 5ab0d4d0a2
commit 6dd6065711
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
4 changed files with 22 additions and 50 deletions

View file

@ -1,16 +1,13 @@
import { useLocation, useParams } from "@solidjs/router";
import { useParams } from "@solidjs/router";
import {
catchError,
createEffect,
createRenderEffect,
createResource,
Show,
type Component,
} from "solid-js";
import Scaffold from "~material/Scaffold";
import { AppBar, CircularProgress, IconButton, Toolbar } from "@suid/material";
import { CircularProgress } from "@suid/material";
import { Title } from "~material/typography";
import { Close as CloseIcon } from "@suid/icons-material";
import { useSessionForAcctStr } from "../masto/clients";
import { resolveCustomEmoji } from "../masto/toot";
import RegularToot, {
@ -33,18 +30,8 @@ import ItemSelectionProvider, {
createSingluarItemSelection,
} from "./toots/ItemSelectionProvider";
import AppTopBar from "~material/AppTopBar";
let cachedEntry: [string, mastodon.v1.Status] | undefined;
export function setCache(acct: string, status: mastodon.v1.Status) {
cachedEntry = [acct, status];
}
function getCache(acct: string, id: string) {
if (acct === cachedEntry?.[0] && id === cachedEntry?.[1].id) {
return cachedEntry[1];
}
}
import { fetchStatus } from "../masto/statuses";
import { type Account } from "../accounts/stores";
const TootBottomSheet: Component = (props) => {
const params = useParams<{ acct: string; id: string }>();
@ -54,25 +41,15 @@ const TootBottomSheet: Component = (props) => {
const session = useSessionForAcctStr(acctText);
const [, selectionState] = createSingluarItemSelection();
const [remoteToot, { mutate: setRemoteToot }] = createResource(
() => [session().client, params.id] as const,
async ([client, id]) => {
return await client.v1.statuses.$select(id).fetch();
},
);
const [remoteToot, { mutate: setRemoteToot }] =
fetchStatus.cachedAndRevalidate(
() => [session().account, params.id] as const,
);
const toot = () =>
catchError(remoteToot, (error) => {
console.error(error);
}) ?? getCache(acctText(), params.id);
createEffect((lastTootId?: string) => {
const tootId = toot()?.id;
if (!tootId || lastTootId === tootId) return tootId;
const elementId = `toot-${tootId}`;
document.getElementById(elementId)?.scrollIntoView({ behavior: "smooth" });
return tootId;
});
});
const [tootContextErrorUncaught, { refetch: refetchContext }] =
createResource(
@ -94,12 +71,6 @@ const TootBottomSheet: Component = (props) => {
() => tootContext()?.descendants,
);
createEffect(() => {
if (ancestors.list.length > 0) {
document.querySelector(`#toot-${toot()!.id}`)?.scrollIntoView();
}
});
useDocumentTitle(() => {
const t = toot()?.reblog ?? toot();
const name = t?.account.displayName ?? "Someone";
@ -300,10 +271,10 @@ const TootBottomSheet: Component = (props) => {
</Show>
</article>
<Show when={session()!.account}>
<Show when={(session().account as Account).inf}>
<TootComposer
mentions={defaultMentions()}
profile={session().account!}
profile={(session().account! as Account).inf}
replyToDisplayName={toot()?.account?.displayName || ""}
client={session().client}
onSent={() => refetchContext()}