import { useLocation, useNavigate, useParams } from "@solidjs/router"; import { catchError, createEffect, createRenderEffect, createResource, createSignal, Show, type Component, } from "solid-js"; import Scaffold from "../material/Scaffold"; import { AppBar, CircularProgress, IconButton, Toolbar } 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, { findElementActionable } from "./RegularToot"; import type { mastodon } from "masto"; import cards from "../material/cards.module.css"; import { css } from "solid-styled"; import { vibrate } from "../platform/hardware"; import { createTimeSource, TimeSourceProvider } from "../platform/timesrc"; import TootComposer from "./TootComposer"; import { useDocumentTitle } from "../utils"; import { createTimelineControlsForArray } from "../masto/timelines"; import TootList from "./TootList"; import "./TootBottomSheet.css"; 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]; } } const TootBottomSheet: Component = (props) => { const params = useParams<{ acct: string; id: string }>(); const location = useLocation<{ tootReply?: boolean; }>(); const navigate = useNavigate(); const time = createTimeSource(); const acctText = () => decodeURIComponent(params.acct); const session = useSessionForAcctStr(acctText); const [remoteToot, { mutate: setRemoteToot }] = createResource( () => [session().client, params.id] as const, async ([client, id]) => { return await client.v1.statuses.$select(id).fetch(); }, ); 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( () => [session().client, params.id] as const, async ([client, id]) => { return await client.v1.statuses.$select(id).context.fetch(); }, ); const tootContext = () => catchError(tootContextErrorUncaught, (error) => { console.error(error); }); const ancestors = createTimelineControlsForArray( () => tootContext()?.ancestors, ); const descendants = createTimelineControlsForArray( () => 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"; return `${name}'s toot`; }); const tootDisplayName = () => { const t = toot()?.reblog ?? toot(); if (t) { return resolveCustomEmoji(t.account.displayName, t.account.emojis); } }; const actSession = () => { const s = session(); return s.account ? s : undefined; }; const onBookmark = async () => { const status = remoteToot()!; const client = actSession()!.client; setRemoteToot( Object.assign({}, status, { bookmarked: !status.bookmarked, }), ); const result = await (status.bookmarked ? client.v1.statuses.$select(status.id).unbookmark() : client.v1.statuses.$select(status.id).bookmark()); setRemoteToot(result); }; const onBoost = async () => { const status = remoteToot()!; const client = actSession()!.client; vibrate(50); setRemoteToot( Object.assign({}, status, { reblogged: !status.reblogged, }), ); const result = await (status.reblogged ? client.v1.statuses.$select(status.id).unreblog() : client.v1.statuses.$select(status.id).reblog()); vibrate([20, 30]); setRemoteToot(result.reblog!); }; const onFav = async () => { const status = remoteToot()!; const client = actSession()!.client; setRemoteToot( Object.assign({}, status, { favourited: !status.favourited, }), ); const result = await (status.favourited ? client.v1.statuses.$select(status.id).favourite() : client.v1.statuses.$select(status.id).unfavourite()); setRemoteToot(result); }; const defaultMentions = () => { const tootAcct = remoteToot()?.reblog?.account ?? remoteToot()?.account; if (!tootAcct) { return; } const others = ancestors.list.map((x) => ancestors.get(x)!.value.account); const values = [tootAcct, ...others].map((x) => `@${x.acct}`); return Array.from(new Set(values).keys()); }; const handleMainTootClick = ( event: MouseEvent & { currentTarget: HTMLElement }, ) => { const actionableElement = findElementActionable( event.target as HTMLElement, event.currentTarget, ); if (actionableElement) { if (actionableElement.dataset.action === "acct") { event.stopPropagation(); const target = actionableElement as HTMLAnchorElement; const acct = encodeURIComponent( target.dataset.client || `@${new URL(target.href).origin}`, ); navigate(`/${acct}/profile/${target.dataset.acctId}`); return; } else { console.warn("unknown action", actionableElement.dataset.rel); } } }; css` .name :global(img) { max-height: 1em; } .name { display: grid; grid-template-columns: 1fr auto; height: calc(var(--title-size) * var(--title-line-height)); > :first-child { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } } `; return ( <span ref={(e: HTMLElement) => createRenderEffect( () => (e.innerHTML = tootDisplayName() ?? "Someone"), ) } ></span> <span>'s toot</span> } class="TootBottomSheet" >
refetchContext()} inReplyToId={remoteToot()?.reblog?.id ?? remoteToot()?.id} />
); }; export default TootBottomSheet;