2024-10-18 19:15:35 +08:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
For,
|
|
|
|
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";
|
2024-10-30 20:58:36 +08:00
|
|
|
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";
|
|
|
|
|
|
|
|
function positionTootInThread(index: number, threadLength: number) {
|
|
|
|
if (index === 0) {
|
|
|
|
return "top";
|
|
|
|
} else if (index === threadLength - 1) {
|
|
|
|
return "bottom";
|
|
|
|
}
|
|
|
|
return "middle";
|
|
|
|
}
|
2024-10-30 19:25:58 +08:00
|
|
|
|
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[];
|
2024-10-18 19:15:35 +08:00
|
|
|
onUnknownThread: (id: string) => { value: mastodon.v1.Status }[] | undefined;
|
|
|
|
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-10-18 19:15:35 +08:00
|
|
|
const result = reblogged
|
|
|
|
? 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleFavourite = async (status: mastodon.v1.Status) => {
|
|
|
|
const client = session()?.client;
|
|
|
|
if (!client) return;
|
|
|
|
const ovalue = status.favourited;
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-10-30 19:25:58 +08:00
|
|
|
const onItemClick = (
|
|
|
|
status: mastodon.v1.Status,
|
2024-11-08 22:12:11 +08:00
|
|
|
event: MouseEvent & { target: EventTarget; currentTarget: HTMLElement },
|
2024-10-30 19:25:58 +08:00
|
|
|
) => {
|
2024-11-08 22:12:11 +08:00
|
|
|
if (!(event.target instanceof HTMLElement)) {
|
|
|
|
throw new Error("target is not an element");
|
|
|
|
}
|
2024-10-30 19:25:58 +08:00
|
|
|
const actionableElement = findElementActionable(
|
|
|
|
event.target,
|
|
|
|
event.currentTarget,
|
|
|
|
);
|
|
|
|
|
2024-10-30 20:58:36 +08:00
|
|
|
if (actionableElement && checkIsExpended(status)) {
|
|
|
|
if (actionableElement.dataset.action === "acct") {
|
2024-10-30 19:25:58 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
const target = actionableElement as HTMLAnchorElement;
|
|
|
|
|
2024-11-01 16:16:44 +08:00
|
|
|
const acct = encodeURIComponent(
|
|
|
|
target.dataset.client || `@${new URL(target.href).origin}`,
|
|
|
|
);
|
2024-10-30 19:25:58 +08:00
|
|
|
|
|
|
|
navigate(`/${acct}/profile/${target.dataset.acctId}`);
|
2024-10-30 20:58:36 +08:00
|
|
|
|
|
|
|
return;
|
2024-10-30 19:25:58 +08:00
|
|
|
} else {
|
|
|
|
console.warn("unknown action", actionableElement.dataset.rel);
|
|
|
|
}
|
2024-11-01 16:16:44 +08:00
|
|
|
} else if (
|
|
|
|
event.target.parentElement &&
|
|
|
|
event.target.parentElement.tagName === "A"
|
|
|
|
) {
|
|
|
|
return;
|
2024-10-30 20:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-10-30 20:58:36 +08:00
|
|
|
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);
|
|
|
|
|
2024-11-08 00:13:59 +08:00
|
|
|
const getPath = (itemId: string) => {
|
|
|
|
return props
|
|
|
|
.onUnknownThread(itemId)!
|
|
|
|
.reverse()
|
|
|
|
.map((x) => x.value);
|
|
|
|
};
|
|
|
|
|
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">
|
2024-10-18 19:15:35 +08:00
|
|
|
<For each={props.threads}>
|
2024-11-08 00:13:59 +08:00
|
|
|
{(itemId) => {
|
2024-11-08 22:12:11 +08:00
|
|
|
const toots = createMemo(() => getPath(itemId));
|
2024-10-18 19:15:35 +08:00
|
|
|
return (
|
2024-11-08 22:12:11 +08:00
|
|
|
<Index each={toots()}>
|
|
|
|
{(status, index) => (
|
|
|
|
<RegularToot
|
|
|
|
data-status-id={status().id}
|
|
|
|
data-thread-sort={index}
|
|
|
|
status={status()}
|
|
|
|
thread={
|
|
|
|
toots().length > 1
|
|
|
|
? positionTootInThread(index, toots().length)
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
class={cardStyle.card}
|
|
|
|
evaluated={checkIsExpended(status())}
|
|
|
|
actionable={checkIsExpended(status())}
|
|
|
|
onBookmark={onBookmark}
|
|
|
|
onRetoot={toggleBoost}
|
|
|
|
onFavourite={toggleFavourite}
|
|
|
|
onReply={(status, event) => {
|
|
|
|
const element = findRootToot(event.currentTarget);
|
|
|
|
openFullScreenToot(status, element, true);
|
|
|
|
}}
|
|
|
|
onClick={[onItemClick, status()]}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Index>
|
2024-10-18 19:15:35 +08:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
</For>
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TootList;
|