tutu/src/timelines/RegularToot.tsx

214 lines
6.3 KiB
TypeScript
Raw Normal View History

2024-07-14 20:28:44 +08:00
import type { mastodon } from "masto";
import {
splitProps,
type Component,
type JSX,
Show,
2024-11-20 16:24:57 +08:00
createSignal,
type Setter,
2024-11-23 22:21:14 +08:00
createContext,
useContext,
2024-07-14 20:28:44 +08:00
} from "solid-js";
2024-11-22 17:16:56 +08:00
import { Body2 } from "~material/typography.js";
import { useTimeSource } from "~platform/timesrc.js";
2024-07-14 20:28:44 +08:00
import { resolveCustomEmoji } from "../masto/toot.js";
import { Divider } from "@suid/material";
2024-11-22 17:16:56 +08:00
import cardStyle from "~material/cards.module.css";
2024-11-22 14:53:23 +08:00
import MediaAttachmentGrid from "./toots/MediaAttachmentGrid.jsx";
2024-10-27 13:43:34 +08:00
import { makeAcctText, useDefaultSession } from "../masto/clients";
2024-11-20 16:26:05 +08:00
import TootContent from "./toots/TootContent";
import BoostIcon from "./toots/BoostIcon";
import PreviewCard from "./toots/PreviewCard";
2024-11-23 20:55:37 +08:00
import TootPoll from "./toots/TootPoll";
2024-11-23 23:44:34 +08:00
import TootActionGroup from "./toots/TootActionGroup.js";
2024-11-24 17:07:14 +08:00
import TootAuthorGroup from "./toots/TootAuthorGroup.js";
2024-11-23 23:44:34 +08:00
import "./RegularToot.css";
2024-07-14 20:28:44 +08:00
2024-11-23 22:21:14 +08:00
export type TootEnv = {
boost: (value: mastodon.v1.Status) => void;
favourite: (value: mastodon.v1.Status) => void;
bookmark: (value: mastodon.v1.Status) => void;
reply?: (
value: mastodon.v1.Status,
2024-10-12 19:48:34 +08:00
event: MouseEvent & { currentTarget: HTMLButtonElement },
) => void;
2024-11-23 22:21:14 +08:00
vote: (
status: mastodon.v1.Status,
votes: readonly number[],
) => void | Promise<void>;
2024-07-14 20:28:44 +08:00
};
2024-11-23 22:21:14 +08:00
const TootEnvContext = /* @__PURE__ */ createContext<TootEnv>();
export const TootEnvProvider = TootEnvContext.Provider;
export function useTootEnv() {
const env = useContext(TootEnvContext);
if (!env) {
2024-11-23 23:01:35 +08:00
throw new TypeError(
"environment not found, use TootEnvProvider to provide",
);
2024-11-23 22:21:14 +08:00
}
2024-11-23 23:01:35 +08:00
return env;
2024-11-23 22:21:14 +08:00
}
type RegularTootProps = {
2024-07-14 20:28:44 +08:00
status: mastodon.v1.Status;
actionable?: boolean;
evaluated?: boolean;
2024-10-12 19:48:34 +08:00
thread?: "top" | "bottom" | "middle";
2024-11-23 23:01:35 +08:00
} & JSX.HTMLElementTags["article"];
2024-07-14 20:28:44 +08:00
2024-10-12 19:48:34 +08:00
export function findRootToot(element: HTMLElement) {
let current: HTMLElement | null = element;
2024-11-24 17:07:14 +08:00
while (current && !current.classList.contains("RegularToot")) {
2024-10-12 19:48:34 +08:00
current = current.parentElement;
}
if (!current) {
2024-11-24 17:16:06 +08:00
throw Error(`the element must be placed under a element with .RegularToot`);
2024-10-12 19:48:34 +08:00
}
return current;
}
/**
* find bottom-to-top the element with `data-action`.
*/
export function findElementActionable(
element: HTMLElement,
top: HTMLElement,
): HTMLElement | undefined {
let current = element;
while (!current.dataset.action) {
if (!current.parentElement || current.parentElement === top) {
return undefined;
}
current = current.parentElement;
}
return current;
}
2024-11-20 16:24:57 +08:00
function onToggleReveal(setValue: Setter<boolean>, event: Event) {
event.stopPropagation();
setValue((x) => !x);
}
2024-10-27 13:43:34 +08:00
/**
* Component for a toot.
*
* If the session involved is not the first session, you must wrap
* this component under a `<DefaultSessionProvier />` with correct
* session.
*
2024-11-23 23:01:35 +08:00
* This component requires be under `<TootEnvProvider />`.
*
* **Handling Clicks**
* There are multiple actions supported in the component. Some handlers
* are passed in, some should be handled as the click event.
*
* For those handler directly passed in, see the props starts with "on".
* We are moving to the new method below.
*
* The following actions are handled by the click event:
* - `[data-action="acct"]`: open the profile page of a account
* - `[data-acct-id]` is the account id for the client
* - `[data-client]` is the client perferred
* - `[href]` is the url of the account
*
* Handling the click event for this component, you should use
* {@link findElementActionable} to find out if the click event has
* additional intent. If the event's target is any from
* the subtree of any "actionable" element, the function returns the element.
*
* You can extract the intent from the attributes of the "actionable" element.
* The action type is the dataset's `action`.
2024-10-27 13:43:34 +08:00
*/
2024-11-23 23:44:34 +08:00
const RegularToot: Component<RegularTootProps> = (oprops) => {
2024-07-14 20:28:44 +08:00
let rootRef: HTMLElement;
2024-11-23 23:44:34 +08:00
const [props, rest] = splitProps(oprops, [
2024-11-23 23:01:35 +08:00
"status",
"lang",
"class",
"actionable",
"evaluated",
"thread",
]);
2024-07-14 20:28:44 +08:00
const now = useTimeSource();
2024-11-23 23:44:34 +08:00
const status = () => props.status;
2024-07-14 20:28:44 +08:00
const toot = () => status().reblog ?? status();
2024-10-27 13:43:34 +08:00
const session = useDefaultSession();
2024-11-20 16:24:57 +08:00
const [reveal, setReveal] = createSignal(false);
2024-07-14 20:28:44 +08:00
return (
<>
<article
2024-07-14 20:28:44 +08:00
classList={{
2024-11-24 17:16:06 +08:00
RegularToot: true,
expanded: props.evaluated,
2024-11-23 23:44:34 +08:00
"thread-top": props.thread === "top",
"thread-mid": props.thread === "middle",
"thread-btm": props.thread === "bottom",
[props.class || ""]: true,
2024-07-14 20:28:44 +08:00
}}
ref={rootRef!}
2024-11-23 23:44:34 +08:00
lang={toot().language || props.lang}
2024-07-14 20:28:44 +08:00
{...rest}
>
<Show when={!!status().reblog}>
2024-11-23 23:44:34 +08:00
<div class="retoot-grp">
<BoostIcon />
<Body2
2024-11-24 17:16:06 +08:00
innerHTML={resolveCustomEmoji(
status().account.displayName,
toot().emojis,
)}
></Body2>
2024-11-06 19:38:53 +08:00
<span>boosts</span>
2024-07-14 20:28:44 +08:00
</div>
</Show>
<TootAuthorGroup
status={toot()}
now={now()}
data-action="acct"
data-client={session() ? makeAcctText(session()!) : undefined}
data-acct-id={toot().account.id}
/>
<TootContent
2024-07-14 20:28:44 +08:00
source={toot().content}
emojis={toot().emojis}
mentions={toot().mentions}
2024-11-20 15:51:14 +08:00
class={cardStyle.cardNoPad}
2024-11-20 16:24:57 +08:00
sensitive={toot().sensitive}
spoilerText={toot().spoilerText}
reveal={reveal()}
onToggleReveal={[onToggleReveal, setReveal]}
2024-07-14 20:28:44 +08:00
/>
<Show
when={
toot().card && (!toot().sensitive || (toot().sensitive && reveal()))
}
>
2024-11-12 19:21:10 +08:00
<PreviewCard src={toot().card!} />
2024-08-17 17:16:18 +08:00
</Show>
2024-07-14 20:28:44 +08:00
<Show when={toot().mediaAttachments.length > 0}>
<MediaAttachmentGrid
attachments={toot().mediaAttachments}
sensitive={toot().sensitive}
/>
2024-07-14 20:28:44 +08:00
</Show>
2024-11-23 20:55:37 +08:00
<Show when={toot().poll}>
2024-11-23 23:01:35 +08:00
<TootPoll value={toot().poll!} status={toot()} />
2024-11-23 20:55:37 +08:00
</Show>
2024-11-23 23:44:34 +08:00
<Show when={props.actionable}>
2024-07-14 20:28:44 +08:00
<Divider
class={cardStyle.cardNoPad}
style={{ "margin-top": "8px" }}
/>
2024-11-23 23:01:35 +08:00
<TootActionGroup value={toot()} class={cardStyle.cardGutSkip} />
2024-07-14 20:28:44 +08:00
</Show>
</article>
2024-07-14 20:28:44 +08:00
</>
);
};
export default RegularToot;