tutu/src/timelines/TootList.tsx

236 lines
6.8 KiB
TypeScript
Raw Normal View History

2024-10-18 19:15:35 +08:00
import {
Component,
createSignal,
ErrorBoundary,
type Ref,
2024-10-29 15:22:25 +08:00
createSelector,
2024-11-08 22:12:11 +08:00
Index,
createMemo,
2024-10-18 19:15:35 +08:00
} from "solid-js";
import { type mastodon } from "masto";
import { vibrate } from "../platform/hardware";
import { useDefaultSession } from "../masto/clients";
2024-10-30 13:22:00 +08:00
import { useHeroSource } from "../platform/anim";
2024-10-18 22:35:04 +08:00
import { HERO as BOTTOM_SHEET_HERO } from "../material/BottomSheet";
import { setCache as setTootBottomSheetCache } from "./TootBottomSheet";
import { useNavigate } from "@solidjs/router";
2024-11-08 22:12:11 +08:00
import RegularToot, {
findElementActionable,
findRootToot,
} from "./RegularToot";
import cardStyle from "../material/cards.module.css";
import type { ThreadNode } from "../masto/timelines";
2024-11-08 22:12:11 +08:00
function positionTootInThread(index: number, threadLength: number) {
if (index === 0) {
return "top";
} else if (index === threadLength - 1) {
return "bottom";
}
return "middle";
}
2024-10-18 19:15:35 +08:00
const TootList: Component<{
ref?: Ref<HTMLDivElement>;
2024-11-04 17:10:12 +08:00
id?: string;
2024-10-30 23:25:33 +08:00
threads: readonly string[];
onUnknownThread: (id: string) => ThreadNode[] | undefined;
2024-10-18 19:15:35 +08:00
onChangeToot: (id: string, value: mastodon.v1.Status) => void;
}> = (props) => {
const session = useDefaultSession();
2024-10-29 15:22:25 +08:00
const heroSrc = useHeroSource();
2024-10-18 19:15:35 +08:00
const [expandedThreadId, setExpandedThreadId] = createSignal<string>();
2024-10-18 22:35:04 +08:00
const navigate = useNavigate();
2024-10-18 19:15:35 +08:00
2024-11-08 22:12:11 +08:00
const onBookmark = async (status: mastodon.v1.Status) => {
const client = session()?.client;
if (!client) return;
2024-10-18 19:15:35 +08:00
const result = await (status.bookmarked
? client.v1.statuses.$select(status.id).unbookmark()
: client.v1.statuses.$select(status.id).bookmark());
props.onChangeToot(result.id, result);
};
2024-11-08 22:12:11 +08:00
const toggleBoost = async (status: mastodon.v1.Status) => {
const client = session()?.client;
if (!client) return;
2024-10-18 19:15:35 +08:00
vibrate(50);
const rootStatus = status.reblog ? status.reblog : status;
const reblogged = rootStatus.reblogged;
if (status.reblog) {
2024-11-08 00:13:59 +08:00
props.onChangeToot(status.id, {
...status,
reblog: { ...status.reblog, reblogged: !reblogged },
});
2024-10-18 19:15:35 +08:00
} else {
2024-11-08 00:13:59 +08:00
props.onChangeToot(status.id, {
...status,
reblogged: !reblogged,
});
2024-10-18 19:15:35 +08:00
}
2024-11-08 00:13:59 +08:00
2024-11-09 16:38:41 +08:00
const result = reblogged
2024-10-18 19:15:35 +08:00
? await client.v1.statuses.$select(status.id).unreblog()
: (await client.v1.statuses.$select(status.id).reblog()).reblog!;
2024-11-08 00:13:59 +08:00
if (status.reblog) {
props.onChangeToot(status.id, {
...status,
reblog: result,
});
} else {
props.onChangeToot(status.id, result);
2024-11-09 16:38:41 +08:00
}
2024-11-08 00:13:59 +08:00
};
const toggleFavourite = async (status: mastodon.v1.Status) => {
const client = session()?.client;
if (!client) return;
const ovalue = status.favourited;
props.onChangeToot(status.id, { ...status, favourited: !ovalue });
2024-11-08 00:13:59 +08:00
const result = ovalue
? await client.v1.statuses.$select(status.id).unfavourite()
: await client.v1.statuses.$select(status.id).favourite();
props.onChangeToot(status.id, result);
2024-10-18 19:15:35 +08:00
};
2024-10-18 22:35:04 +08:00
const openFullScreenToot = (
toot: mastodon.v1.Status,
srcElement?: HTMLElement,
reply?: boolean,
) => {
const p = session()?.account;
if (!p) return;
const inf = p.inf;
if (!inf) {
console.warn("no account info?");
return;
}
2024-10-29 15:22:25 +08:00
if (heroSrc) {
heroSrc[1]((x) => ({ ...x, [BOTTOM_SHEET_HERO]: srcElement }));
}
2024-10-18 22:35:04 +08:00
const acct = `${inf.username}@${p.site}`;
setTootBottomSheetCache(acct, toot);
navigate(`/${encodeURIComponent(acct)}/toot/${toot.id}`, {
state: reply
? {
tootReply: true,
}
: undefined,
});
};
const onItemClick = (
status: mastodon.v1.Status,
2024-11-08 22:12:11 +08:00
event: MouseEvent & { target: EventTarget; currentTarget: HTMLElement },
) => {
2024-11-08 22:12:11 +08:00
if (!(event.target instanceof HTMLElement)) {
throw new Error("target is not an element");
}
const actionableElement = findElementActionable(
event.target,
event.currentTarget,
);
if (actionableElement && checkIsExpended(status)) {
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);
}
} else if (
event.target.parentElement &&
event.target.parentElement.tagName === "A"
) {
return;
}
// else if (!actionableElement || !checkIsExpended(status) || <rel is not one of known action>)
if (status.id !== expandedThreadId()) {
setExpandedThreadId((x) => (x ? undefined : status.id));
2024-10-29 15:22:25 +08:00
} else {
openFullScreenToot(status, event.currentTarget as HTMLElement);
2024-10-29 15:22:25 +08:00
}
};
const checkIsExpendedId = createSelector(expandedThreadId);
const checkIsExpended = (status: mastodon.v1.Status) =>
checkIsExpendedId(status.id);
const reply = (
status: mastodon.v1.Status,
event: { currentTarget: HTMLElement },
) => {
const element = findRootToot(event.currentTarget);
openFullScreenToot(status, element, true);
2024-11-08 00:13:59 +08:00
};
2024-10-18 19:15:35 +08:00
return (
<ErrorBoundary
fallback={(err, reset) => {
return <p>Oops: {String(err)}</p>;
}}
>
2024-11-04 17:10:12 +08:00
<div ref={props.ref} id={props.id} class="toot-list">
<Index each={props.threads}>
{(threadId, threadIdx) => {
const thread = createMemo(() =>
props.onUnknownThread(threadId())?.reverse(),
);
const threadLength = () => thread()?.length ?? 0;
2024-10-18 19:15:35 +08:00
return (
<Index each={thread()}>
{(threadNode, index) => {
const status = () => threadNode().value;
return (
<RegularToot
data-status-id={status().id}
data-thread={threadIdx}
data-thread-len={threadLength()}
data-thread-sort={index}
status={status()}
thread={
threadLength() > 1
? positionTootInThread(index, threadLength())
: undefined
}
class={cardStyle.card}
evaluated={checkIsExpended(status())}
actionable={checkIsExpended(status())}
onBookmark={onBookmark}
onRetoot={toggleBoost}
onFavourite={toggleFavourite}
onReply={reply}
onClick={[onItemClick, status()]}
/>
);
}}
2024-11-08 22:12:11 +08:00
</Index>
2024-10-18 19:15:35 +08:00
);
}}
</Index>
2024-10-18 19:15:35 +08:00
</div>
</ErrorBoundary>
);
};
export default TootList;