2024-09-14 13:40:50 +08:00
|
|
|
import { useLocation, useNavigate, useParams } from "@solidjs/router";
|
2024-08-13 15:39:05 +08:00
|
|
|
import {
|
|
|
|
createEffect,
|
|
|
|
createRenderEffect,
|
|
|
|
createResource,
|
2024-09-25 21:24:39 +08:00
|
|
|
createSignal,
|
2024-08-22 16:49:27 +08:00
|
|
|
For,
|
2024-08-13 15:39:05 +08:00
|
|
|
Show,
|
|
|
|
type Component,
|
|
|
|
} from "solid-js";
|
2024-08-12 17:25:03 +08:00
|
|
|
import Scaffold from "../material/Scaffold";
|
2024-09-25 21:24:39 +08:00
|
|
|
import { AppBar, CircularProgress, IconButton, Toolbar } from "@suid/material";
|
2024-08-12 17:25:03 +08:00
|
|
|
import { Title } from "../material/typography";
|
2024-09-14 13:40:50 +08:00
|
|
|
import {
|
|
|
|
ArrowBack as BackIcon,
|
|
|
|
Close as CloseIcon,
|
|
|
|
} from "@suid/icons-material";
|
2024-08-12 21:55:26 +08:00
|
|
|
import { createUnauthorizedClient, useSessions } from "../masto/clients";
|
|
|
|
import { resolveCustomEmoji } from "../masto/toot";
|
|
|
|
import RegularToot from "./RegularToot";
|
2024-08-13 14:17:33 +08:00
|
|
|
import type { mastodon } from "masto";
|
2024-08-13 15:39:05 +08:00
|
|
|
import cards from "../material/cards.module.css";
|
|
|
|
import { css } from "solid-styled";
|
2024-09-14 13:15:11 +08:00
|
|
|
import { vibrate } from "../platform/hardware";
|
2024-09-25 19:30:05 +08:00
|
|
|
import { createTimeSource, TimeSourceProvider } from "../platform/timesrc";
|
2024-09-28 15:15:06 +08:00
|
|
|
import TootComposer from "./TootComposer";
|
2024-10-14 20:05:08 +08:00
|
|
|
import { useDocumentTitle } from "../utils";
|
2024-08-13 14:17:33 +08:00
|
|
|
|
|
|
|
let cachedEntry: [string, mastodon.v1.Status] | undefined;
|
|
|
|
|
|
|
|
export function setCache(acct: string, status: mastodon.v1.Status) {
|
2024-08-13 15:39:05 +08:00
|
|
|
cachedEntry = [acct, status];
|
2024-08-13 14:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCache(acct: string, id: string) {
|
|
|
|
if (acct === cachedEntry?.[0] && id === cachedEntry?.[1].id) {
|
2024-08-13 15:39:05 +08:00
|
|
|
return cachedEntry[1];
|
2024-08-13 14:17:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 20:28:44 +08:00
|
|
|
const TootBottomSheet: Component = (props) => {
|
2024-08-12 21:55:26 +08:00
|
|
|
const params = useParams<{ acct: string; id: string }>();
|
2024-09-28 15:29:21 +08:00
|
|
|
const location = useLocation<{
|
|
|
|
tootBottomSheetPushedCount?: number;
|
|
|
|
tootReply?: boolean;
|
|
|
|
}>();
|
2024-08-12 21:55:26 +08:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const allSession = useSessions();
|
2024-09-25 19:30:05 +08:00
|
|
|
const time = createTimeSource();
|
2024-09-25 21:24:39 +08:00
|
|
|
const [isInTyping, setInTyping] = createSignal(false);
|
2024-08-13 15:39:05 +08:00
|
|
|
const acctText = () => decodeURIComponent(params.acct);
|
2024-08-12 21:55:26 +08:00
|
|
|
const session = () => {
|
2024-08-13 15:39:05 +08:00
|
|
|
const [inputUsername, inputSite] = acctText().split("@", 2);
|
2024-08-12 21:55:26 +08:00
|
|
|
const authedSession = allSession().find(
|
|
|
|
(x) =>
|
|
|
|
x.account.site === inputSite &&
|
|
|
|
x.account.inf?.username === inputUsername,
|
|
|
|
);
|
2024-08-13 15:39:05 +08:00
|
|
|
return (
|
|
|
|
authedSession ?? {
|
|
|
|
client: createUnauthorizedClient(inputSite),
|
|
|
|
account: undefined,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2024-08-12 21:55:26 +08:00
|
|
|
|
2024-09-14 13:40:50 +08:00
|
|
|
const pushedCount = () => {
|
|
|
|
return location.state?.tootBottomSheetPushedCount || 0;
|
|
|
|
};
|
|
|
|
|
2024-09-14 13:15:11 +08:00
|
|
|
const [remoteToot, { mutate: setRemoteToot }] = createResource(
|
2024-08-12 21:55:26 +08:00
|
|
|
() => [session().client, params.id] as const,
|
|
|
|
async ([client, id]) => {
|
|
|
|
return await client.v1.statuses.$select(id).fetch();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-08-13 14:17:33 +08:00
|
|
|
const toot = () => remoteToot() ?? getCache(acctText(), params.id);
|
2024-08-12 21:55:26 +08:00
|
|
|
|
2024-09-23 16:00:39 +08:00
|
|
|
createEffect((lastTootId?: string) => {
|
2024-09-14 13:40:50 +08:00
|
|
|
const tootId = toot()?.id;
|
2024-09-23 16:00:39 +08:00
|
|
|
if (!tootId || lastTootId === tootId) return tootId;
|
2024-09-14 13:40:50 +08:00
|
|
|
const elementId = `toot-${tootId}`;
|
|
|
|
document.getElementById(elementId)?.scrollIntoView({ behavior: "smooth" });
|
2024-09-23 16:00:39 +08:00
|
|
|
return tootId;
|
2024-09-14 13:40:50 +08:00
|
|
|
});
|
|
|
|
|
2024-09-28 15:29:21 +08:00
|
|
|
createEffect(() => {
|
|
|
|
if (location.state?.tootReply) {
|
|
|
|
setInTyping(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const [tootContext, { refetch: refetchContext }] = createResource(
|
2024-08-22 16:49:27 +08:00
|
|
|
() => [session().client, params.id] as const,
|
|
|
|
async ([client, id]) => {
|
|
|
|
return await client.v1.statuses.$select(id).context.fetch();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const ancestors = () => tootContext()?.ancestors ?? [];
|
|
|
|
const descendants = () => tootContext()?.descendants ?? [];
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
if (ancestors().length > 0) {
|
|
|
|
document.querySelector(`#toot-${toot()!.id}`)?.scrollIntoView();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-14 20:05:08 +08:00
|
|
|
useDocumentTitle(() => {
|
|
|
|
const t = toot()?.reblog ?? toot()
|
|
|
|
const name = t?.account.displayName ?? "Someone"
|
|
|
|
return `${name}'s toot`
|
|
|
|
})
|
|
|
|
|
2024-10-09 15:52:14 +08:00
|
|
|
const tootDisplayName = () => {
|
2024-10-14 20:05:08 +08:00
|
|
|
const t = toot()?.reblog ?? toot();
|
2024-08-12 21:55:26 +08:00
|
|
|
if (t) {
|
2024-10-09 15:52:14 +08:00
|
|
|
return resolveCustomEmoji(t.account.displayName, t.account.emojis);
|
2024-08-12 21:55:26 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-14 13:15:11 +08:00
|
|
|
const actSession = () => {
|
|
|
|
const s = session();
|
|
|
|
return s.account ? s : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onBookmark = async () => {
|
|
|
|
const status = remoteToot()!;
|
|
|
|
const client = actSession()!.client;
|
2024-09-25 20:32:50 +08:00
|
|
|
setRemoteToot(
|
|
|
|
Object.assign({}, status, {
|
|
|
|
bookmarked: !status.bookmarked,
|
|
|
|
}),
|
|
|
|
);
|
2024-09-14 13:15:11 +08:00
|
|
|
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]);
|
2024-09-25 20:32:50 +08:00
|
|
|
setRemoteToot(result.reblog!);
|
2024-09-14 13:15:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const onFav = async () => {
|
|
|
|
const status = remoteToot()!;
|
|
|
|
const client = actSession()!.client;
|
2024-09-25 20:32:50 +08:00
|
|
|
setRemoteToot(
|
|
|
|
Object.assign({}, status, {
|
|
|
|
favourited: !status.favourited,
|
|
|
|
}),
|
|
|
|
);
|
2024-09-14 13:15:11 +08:00
|
|
|
const result = await (status.favourited
|
|
|
|
? client.v1.statuses.$select(status.id).favourite()
|
|
|
|
: client.v1.statuses.$select(status.id).unfavourite());
|
|
|
|
setRemoteToot(result);
|
|
|
|
};
|
|
|
|
|
2024-09-14 13:40:50 +08:00
|
|
|
const switchContext = (status: mastodon.v1.Status) => {
|
2024-09-25 21:24:39 +08:00
|
|
|
if (isInTyping()) {
|
|
|
|
setInTyping(false);
|
|
|
|
return;
|
|
|
|
}
|
2024-09-14 13:40:50 +08:00
|
|
|
setCache(params.acct, status);
|
|
|
|
navigate(`/${params.acct}/${status.id}`, {
|
|
|
|
state: {
|
|
|
|
tootBottomSheetPushedCount: pushedCount() + 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-09-28 14:39:20 +08:00
|
|
|
const defaultMentions = () => {
|
2024-09-28 15:29:21 +08:00
|
|
|
const tootAcct = remoteToot()?.reblog?.account ?? remoteToot()?.account;
|
2024-09-28 14:39:20 +08:00
|
|
|
if (!tootAcct) {
|
2024-09-28 15:29:21 +08:00
|
|
|
return;
|
2024-09-28 14:39:20 +08:00
|
|
|
}
|
|
|
|
|
2024-09-28 15:29:21 +08:00
|
|
|
const others = ancestors().map((x) => x.account);
|
2024-09-28 14:39:20 +08:00
|
|
|
|
2024-09-28 15:29:21 +08:00
|
|
|
const values = [tootAcct, ...others].map((x) => `@${x.acct}`);
|
|
|
|
return Array.from(new Set(values).keys());
|
|
|
|
};
|
2024-09-28 14:39:20 +08:00
|
|
|
|
2024-08-13 15:39:05 +08:00
|
|
|
css`
|
|
|
|
.name :global(img) {
|
|
|
|
max-height: 1em;
|
|
|
|
}
|
2024-10-09 15:52:14 +08:00
|
|
|
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 15:39:05 +08:00
|
|
|
`;
|
|
|
|
|
2024-08-12 21:55:26 +08:00
|
|
|
return (
|
|
|
|
<Scaffold
|
|
|
|
topbar={
|
|
|
|
<AppBar
|
|
|
|
sx={{
|
|
|
|
backgroundColor: "var(--tutu-color-surface)",
|
|
|
|
color: "var(--tutu-color-on-surface)",
|
|
|
|
}}
|
|
|
|
elevation={1}
|
|
|
|
position="static"
|
|
|
|
>
|
|
|
|
<Toolbar
|
|
|
|
variant="dense"
|
|
|
|
sx={{ paddingTop: "var(--safe-area-inset-top, 0px)" }}
|
|
|
|
>
|
|
|
|
<IconButton color="inherit" onClick={[navigate, -1]} disableRipple>
|
2024-09-14 13:40:50 +08:00
|
|
|
{pushedCount() > 0 ? <BackIcon /> : <CloseIcon />}
|
2024-08-12 21:55:26 +08:00
|
|
|
</IconButton>
|
2024-08-13 15:39:05 +08:00
|
|
|
<Title
|
2024-10-09 15:52:14 +08:00
|
|
|
component="div"
|
2024-08-13 15:39:05 +08:00
|
|
|
class="name"
|
2024-10-09 15:52:14 +08:00
|
|
|
use:solid-styled
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
ref={(e: HTMLElement) =>
|
|
|
|
createRenderEffect(
|
|
|
|
() => (e.innerHTML = tootDisplayName() ?? "Someone"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
></span>
|
|
|
|
<span>'s toot</span>
|
|
|
|
</Title>
|
2024-08-12 21:55:26 +08:00
|
|
|
</Toolbar>
|
|
|
|
</AppBar>
|
|
|
|
}
|
|
|
|
>
|
2024-09-25 19:30:05 +08:00
|
|
|
<TimeSourceProvider value={time}>
|
|
|
|
<For each={ancestors()}>
|
|
|
|
{(item) => (
|
|
|
|
<RegularToot
|
|
|
|
id={`toot-${item.id}`}
|
|
|
|
class={cards.card}
|
|
|
|
status={item}
|
|
|
|
actionable={false}
|
|
|
|
onClick={[switchContext, item]}
|
|
|
|
></RegularToot>
|
|
|
|
)}
|
|
|
|
</For>
|
2024-08-22 16:49:27 +08:00
|
|
|
|
2024-09-25 19:30:05 +08:00
|
|
|
<article>
|
|
|
|
<Show when={toot()}>
|
|
|
|
<RegularToot
|
|
|
|
id={`toot-${toot()!.id}`}
|
|
|
|
class={cards.card}
|
|
|
|
style={{
|
|
|
|
"scroll-margin-top":
|
|
|
|
"calc(var(--scaffold-topbar-height) + 20px)",
|
|
|
|
}}
|
|
|
|
status={toot()!}
|
|
|
|
actionable={!!actSession()}
|
|
|
|
evaluated={true}
|
|
|
|
onBookmark={onBookmark}
|
|
|
|
onRetoot={onBoost}
|
|
|
|
onFavourite={onFav}
|
|
|
|
></RegularToot>
|
|
|
|
</Show>
|
|
|
|
</article>
|
2024-08-13 15:39:05 +08:00
|
|
|
|
2024-09-28 14:39:20 +08:00
|
|
|
<Show when={session()!.account}>
|
2024-09-28 15:15:06 +08:00
|
|
|
<TootComposer
|
2024-09-25 21:24:39 +08:00
|
|
|
isTyping={isInTyping()}
|
|
|
|
onTypingChange={setInTyping}
|
2024-09-28 14:39:20 +08:00
|
|
|
mentions={defaultMentions()}
|
|
|
|
profile={session().account!}
|
2024-09-25 19:30:05 +08:00
|
|
|
replyToDisplayName={toot()?.account?.displayName || ""}
|
2024-09-28 14:39:20 +08:00
|
|
|
client={session().client}
|
|
|
|
onSent={() => refetchContext()}
|
|
|
|
inReplyToId={remoteToot()?.reblog?.id ?? remoteToot()?.id}
|
2024-09-25 19:30:05 +08:00
|
|
|
/>
|
|
|
|
</Show>
|
|
|
|
|
|
|
|
<Show when={tootContext.loading}>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
"justify-content": "center",
|
|
|
|
"margin-block": "12px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CircularProgress style="width: 1.5em; height: 1.5em;" />
|
2024-08-13 15:39:05 +08:00
|
|
|
</div>
|
2024-08-12 21:55:26 +08:00
|
|
|
</Show>
|
2024-09-25 19:30:05 +08:00
|
|
|
|
|
|
|
<For each={descendants()}>
|
|
|
|
{(item) => (
|
|
|
|
<RegularToot
|
|
|
|
id={`toot-${item.id}`}
|
|
|
|
class={cards.card}
|
|
|
|
status={item}
|
|
|
|
actionable={false}
|
|
|
|
onClick={[switchContext, item]}
|
|
|
|
></RegularToot>
|
|
|
|
)}
|
|
|
|
</For>
|
|
|
|
</TimeSourceProvider>
|
2024-09-28 15:29:21 +08:00
|
|
|
<div style={{ height: "var(--safe-area-inset-bottom, 0)" }}></div>
|
2024-08-12 21:55:26 +08:00
|
|
|
</Scaffold>
|
|
|
|
);
|
2024-08-05 15:33:00 +08:00
|
|
|
};
|
2024-07-14 20:28:44 +08:00
|
|
|
|
2024-08-05 15:33:00 +08:00
|
|
|
export default TootBottomSheet;
|