RegularToot: refactor, add env context

This commit is contained in:
thislight 2024-11-23 22:21:14 +08:00
parent cbdf5e667d
commit ad7db8e865
No known key found for this signature in database
GPG key ID: FCFE5192241CCD4E
5 changed files with 164 additions and 123 deletions

View file

@ -7,6 +7,8 @@ import {
createRenderEffect, createRenderEffect,
createSignal, createSignal,
type Setter, type Setter,
createContext,
useContext,
} from "solid-js"; } from "solid-js";
import tootStyle from "./toot.module.css"; import tootStyle from "./toot.module.css";
import { formatRelative, parseISO } from "date-fns"; import { formatRelative, parseISO } from "date-fns";
@ -38,27 +40,38 @@ import BoostIcon from "./toots/BoostIcon";
import PreviewCard from "./toots/PreviewCard"; import PreviewCard from "./toots/PreviewCard";
import TootPoll from "./toots/TootPoll"; import TootPoll from "./toots/TootPoll";
type TootActionGroupProps<T extends mastodon.v1.Status> = { export type TootEnv = {
onRetoot?: (value: T) => void; boost: (value: mastodon.v1.Status) => void;
onFavourite?: (value: T) => void; favourite: (value: mastodon.v1.Status) => void;
onBookmark?: (value: T) => void; bookmark: (value: mastodon.v1.Status) => void;
onReply?: ( reply?: (
value: T, value: mastodon.v1.Status,
event: MouseEvent & { currentTarget: HTMLButtonElement }, event: MouseEvent & { currentTarget: HTMLButtonElement },
) => void; ) => void;
vote: (
status: mastodon.v1.Status,
votes: readonly number[],
) => void | Promise<void>;
}; };
const TootEnvContext = /* @__PURE__ */ createContext<TootEnv>();
export const TootEnvProvider = TootEnvContext.Provider;
export function useTootEnv() {
const env = useContext(TootEnvContext);
if (!env) {
throw new TypeError("environment not found, use TootEnvProvider to provide")
}
return env
}
type RegularTootProps = { type RegularTootProps = {
status: mastodon.v1.Status; status: mastodon.v1.Status;
actionable?: boolean; actionable?: boolean;
evaluated?: boolean; evaluated?: boolean;
thread?: "top" | "bottom" | "middle"; thread?: "top" | "bottom" | "middle";
} &
onVote?: (value: {
status: mastodon.v1.Status;
votes: readonly number[];
}) => void | Promise<void>;
} & TootActionGroupProps<mastodon.v1.Status> &
JSX.HTMLElementTags["article"]; JSX.HTMLElementTags["article"];
function isolatedCallback(e: MouseEvent) { function isolatedCallback(e: MouseEvent) {
@ -79,8 +92,9 @@ export function findRootToot(element: HTMLElement) {
} }
function TootActionGroup<T extends mastodon.v1.Status>( function TootActionGroup<T extends mastodon.v1.Status>(
props: TootActionGroupProps<T> & { value: T }, props: { value: T },
) { ) {
const {reply, boost, favourite, bookmark} = useTootEnv()
let actGrpElement: HTMLDivElement; let actGrpElement: HTMLDivElement;
const toot = () => props.value; const toot = () => props.value;
return ( return (
@ -89,10 +103,10 @@ function TootActionGroup<T extends mastodon.v1.Status>(
class={tootStyle.tootBottomActionGrp} class={tootStyle.tootBottomActionGrp}
onClick={isolatedCallback} onClick={isolatedCallback}
> >
<Show when={props.onReply}> <Show when={reply}>
<Button <Button
class={tootStyle.tootActionWithCount} class={tootStyle.tootActionWithCount}
onClick={[props.onReply!, props.value]} onClick={[reply!, props.value]}
> >
<ReplyAll /> <ReplyAll />
<span>{toot().repliesCount}</span> <span>{toot().repliesCount}</span>
@ -104,7 +118,7 @@ function TootActionGroup<T extends mastodon.v1.Status>(
style={{ style={{
color: toot().reblogged ? "var(--tutu-color-primary)" : undefined, color: toot().reblogged ? "var(--tutu-color-primary)" : undefined,
}} }}
onClick={() => props.onRetoot?.(toot())} onClick={[boost, props.value]}
> >
<Repeat /> <Repeat />
<span>{toot().reblogsCount}</span> <span>{toot().reblogsCount}</span>
@ -114,7 +128,7 @@ function TootActionGroup<T extends mastodon.v1.Status>(
style={{ style={{
color: toot().favourited ? "var(--tutu-color-primary)" : undefined, color: toot().favourited ? "var(--tutu-color-primary)" : undefined,
}} }}
onClick={() => props.onFavourite?.(toot())} onClick={[favourite, props.value]}
> >
{toot().favourited ? <Star /> : <StarOutline />} {toot().favourited ? <Star /> : <StarOutline />}
<span>{toot().favouritesCount}</span> <span>{toot().favouritesCount}</span>
@ -124,7 +138,7 @@ function TootActionGroup<T extends mastodon.v1.Status>(
style={{ style={{
color: toot().bookmarked ? "var(--tutu-color-primary)" : undefined, color: toot().bookmarked ? "var(--tutu-color-primary)" : undefined,
}} }}
onClick={() => props.onBookmark?.(toot())} onClick={[bookmark, props.value]}
> >
{toot().bookmarked ? <Bookmark /> : <BookmarkAddOutlined />} {toot().bookmarked ? <Bookmark /> : <BookmarkAddOutlined />}
</Button> </Button>
@ -243,11 +257,10 @@ function onToggleReveal(setValue: Setter<boolean>, event: Event) {
*/ */
const RegularToot: Component<RegularTootProps> = (props) => { const RegularToot: Component<RegularTootProps> = (props) => {
let rootRef: HTMLElement; let rootRef: HTMLElement;
const [managed, managedActionGroup, pollProps, rest] = splitProps( const {vote} = useTootEnv()
const [managed, rest] = splitProps(
props, props,
["status", "lang", "class", "actionable", "evaluated", "thread"], ["status", "lang", "class", "actionable", "evaluated", "thread"],
["onRetoot", "onFavourite", "onBookmark", "onReply"],
["onVote"],
); );
const now = useTimeSource(); const now = useTimeSource();
const status = () => managed.status; const status = () => managed.status;
@ -361,18 +374,8 @@ const RegularToot: Component<RegularTootProps> = (props) => {
</Show> </Show>
<Show when={toot().poll}> <Show when={toot().poll}>
<TootPoll <TootPoll
options={toot().poll!.options} value={toot().poll!}
multiple={toot().poll!.multiple} status={toot()}
votesCount={toot().poll!.votesCount}
expired={toot().poll!.expired}
expiredAt={
toot().poll!.expiresAt
? parseISO(toot().poll!.expiresAt!)
: undefined
}
voted={toot().poll!.voted}
ownVotes={toot().poll!.ownVotes || undefined}
onVote={(votes) => pollProps.onVote?.({ status: status(), votes })}
/> />
</Show> </Show>
<Show when={managed.actionable}> <Show when={managed.actionable}>
@ -380,7 +383,7 @@ const RegularToot: Component<RegularTootProps> = (props) => {
class={cardStyle.cardNoPad} class={cardStyle.cardNoPad}
style={{ "margin-top": "8px" }} style={{ "margin-top": "8px" }}
/> />
<TootActionGroup value={toot()} {...managedActionGroup} /> <TootActionGroup value={toot()} />
</Show> </Show>
</article> </article>
</> </>

View file

@ -13,7 +13,10 @@ import { Title } from "~material/typography";
import { Close as CloseIcon } from "@suid/icons-material"; import { Close as CloseIcon } from "@suid/icons-material";
import { useSessionForAcctStr } from "../masto/clients"; import { useSessionForAcctStr } from "../masto/clients";
import { resolveCustomEmoji } from "../masto/toot"; import { resolveCustomEmoji } from "../masto/toot";
import RegularToot, { findElementActionable } from "./RegularToot"; import RegularToot, {
findElementActionable,
TootEnvProvider,
} from "./RegularToot";
import type { mastodon } from "masto"; import type { mastodon } from "masto";
import cards from "~material/cards.module.css"; import cards from "~material/cards.module.css";
import { css } from "solid-styled"; import { css } from "solid-styled";
@ -169,6 +172,33 @@ const TootBottomSheet: Component = (props) => {
return Array.from(new Set(values).keys()); return Array.from(new Set(values).keys());
}; };
const vote = async (status: mastodon.v1.Status, votes: readonly number[]) => {
const client = session()?.client;
if (!client) return;
const toot = status.reblog ?? status;
if (!toot.poll) return;
const npoll = await client.v1.polls.$select(toot.poll.id).votes.create({
choices: votes,
});
if (status.reblog) {
setRemoteToot({
...status,
reblog: {
...status.reblog,
poll: npoll,
},
});
} else {
setRemoteToot({
...status,
poll: npoll,
});
}
};
const handleMainTootClick = ( const handleMainTootClick = (
event: MouseEvent & { currentTarget: HTMLElement }, event: MouseEvent & { currentTarget: HTMLElement },
) => { ) => {
@ -255,23 +285,29 @@ const TootBottomSheet: Component = (props) => {
<article> <article>
<Show when={toot()}> <Show when={toot()}>
<TootEnvProvider
value={{
bookmark: onBookmark,
boost: onBoost,
favourite: onFav,
vote,
}}
>
<RegularToot <RegularToot
id={`toot-${toot()!.id}`} id={`toot-${toot()!.id}`}
class={cards.card} class={cards.card}
style={{ style={{
"scroll-margin-top": "scroll-margin-top":
"calc(var(--scaffold-topbar-height) + 20px)", "calc(var(--scaffold-topbar-height) + 20px)",
"cursor": "auto", cursor: "auto",
"user-select": "auto", "user-select": "auto",
}} }}
status={toot()!} status={toot()!}
actionable={!!actSession()} actionable={!!actSession()}
evaluated={true} evaluated={true}
onBookmark={onBookmark}
onRetoot={onBoost}
onFavourite={onFav}
onClick={handleMainTootClick} onClick={handleMainTootClick}
></RegularToot> ></RegularToot>
</TootEnvProvider>
</Show> </Show>
</article> </article>

View file

@ -14,6 +14,7 @@ import { setCache as setTootBottomSheetCache } from "./TootBottomSheet";
import RegularToot, { import RegularToot, {
findElementActionable, findElementActionable,
findRootToot, findRootToot,
TootEnvProvider,
} from "./RegularToot"; } from "./RegularToot";
import cardStyle from "~material/cards.module.css"; import cardStyle from "~material/cards.module.css";
import type { ThreadNode } from "../masto/timelines"; import type { ThreadNode } from "../masto/timelines";
@ -232,13 +233,10 @@ const TootList: Component<{
openFullScreenToot(status, element, true); openFullScreenToot(status, element, true);
}; };
const vote = async ({ const vote = async (
status, status: mastodon.v1.Status,
votes, votes: readonly number[]
}: { ) => {
status: mastodon.v1.Status;
votes: readonly number[];
}) => {
const client = session()?.client; const client = session()?.client;
if (!client) return; if (!client) return;
@ -263,7 +261,6 @@ const TootList: Component<{
poll: npoll, poll: npoll,
}); });
} }
}; };
return ( return (
@ -273,6 +270,13 @@ const TootList: Component<{
return <p>Oops: {String(err)}</p>; return <p>Oops: {String(err)}</p>;
}} }}
> >
<TootEnvProvider value={{
boost: toggleBoost,
bookmark: onBookmark,
favourite: toggleFavourite,
reply: reply,
vote: vote
}}>
<div ref={props.ref} id={props.id} class="toot-list"> <div ref={props.ref} id={props.id} class="toot-list">
<Index each={props.threads}> <Index each={props.threads}>
{(threadId, threadIdx) => { {(threadId, threadIdx) => {
@ -290,8 +294,6 @@ const TootList: Component<{
return ( return (
<RegularToot <RegularToot
data-status-id={status().id} data-status-id={status().id}
data-thread={threadIdx}
data-thread-len={threadLength()}
data-thread-sort={index} data-thread-sort={index}
status={status()} status={status()}
thread={ thread={
@ -302,11 +304,6 @@ const TootList: Component<{
class={cardStyle.card} class={cardStyle.card}
evaluated={checkIsExpended(status())} evaluated={checkIsExpended(status())}
actionable={checkIsExpended(status())} actionable={checkIsExpended(status())}
onBookmark={onBookmark}
onRetoot={toggleBoost}
onFavourite={toggleFavourite}
onReply={reply}
onVote={vote}
onClick={[onItemClick, status()]} onClick={[onItemClick, status()]}
/> />
); );
@ -316,6 +313,7 @@ const TootList: Component<{
}} }}
</Index> </Index>
</div> </div>
</TootEnvProvider>
</ErrorBoundary> </ErrorBoundary>
); );
}; };

View file

@ -28,21 +28,17 @@ import { useTimeSource } from "~platform/timesrc";
import { useDateFnLocale } from "~platform/i18n"; import { useDateFnLocale } from "~platform/i18n";
import TootPollDialog from "./TootPollDialog"; import TootPollDialog from "./TootPollDialog";
import { ANIM_CURVE_STD } from "~material/theme"; import { ANIM_CURVE_STD } from "~material/theme";
import { useTootEnv } from "../RegularToot";
type TootPollProps = { type TootPollProps = {
options: Readonly<mastodon.v1.Poll["options"]>; value: mastodon.v1.Poll
multiple?: boolean; status: mastodon.v1.Status
votesCount: number;
expired?: boolean;
expiredAt?: Date;
voted?: boolean;
ownVotes?: readonly number[];
onVote(votes: readonly number[]): void | Promise<void>;
}; };
const TootPoll: Component<TootPollProps> = (props) => { const TootPoll: Component<TootPollProps> = (props) => {
let list: HTMLUListElement; let list: HTMLUListElement;
const {vote}= useTootEnv()
const now = useTimeSource(); const now = useTimeSource();
const dateFnLocale = useDateFnLocale(); const dateFnLocale = useDateFnLocale();
const [mustShowResult, setMustShowResult] = createSignal<boolean>(); const [mustShowResult, setMustShowResult] = createSignal<boolean>();
@ -50,24 +46,26 @@ const TootPoll: Component<TootPollProps> = (props) => {
const [initialVote, setInitialVote] = createSignal(0); const [initialVote, setInitialVote] = createSignal(0);
const poll = () => props.value
const isShowResult = () => { const isShowResult = () => {
const n = mustShowResult(); const n = mustShowResult();
if (typeof n !== "undefined") { if (typeof n !== "undefined") {
return n; return n;
} }
return props.expired || props.voted; return poll().expired || poll().voted;
}; };
const isOwnVote = createSelector( const isOwnVote = createSelector(
() => props.ownVotes, () => poll().ownVotes,
(idx: number, votes) => votes?.includes(idx) || false, (idx: number, votes) => votes?.includes(idx) || false,
); );
const openVote = (i: number, event: Event) => { const openVote = (i: number, event: Event) => {
event.stopPropagation(); event.stopPropagation();
if (props.expired || props.voted) { if (poll().expired || poll().voted) {
return; return;
} }
@ -100,13 +98,13 @@ const TootPoll: Component<TootPollProps> = (props) => {
return ( return (
<section class="TootPoll"> <section class="TootPoll">
<div class="hints"> <div class="hints">
<span>{props.votesCount} votes in total</span> <span>{poll().votesCount} votes in total</span>
<Show when={props.expired}> <Show when={poll().expired}>
<span>Poll is ended</span> <span>Poll is ended</span>
</Show> </Show>
</div> </div>
<List ref={list!} disablePadding class="option-list"> <List ref={list!} disablePadding class="option-list">
<Index each={props.options}> <Index each={poll().options}>
{(option, index) => { {(option, index) => {
return ( return (
<> <>
@ -140,7 +138,7 @@ const TootPoll: Component<TootPollProps> = (props) => {
</Show> </Show>
<Show <Show
when={props.multiple} when={poll().multiple}
fallback={ fallback={
<Radio <Radio
checked={isOwnVote(index)} checked={isOwnVote(index)}
@ -164,14 +162,14 @@ const TootPoll: Component<TootPollProps> = (props) => {
<Button onClick={animateAndSetMustShow}> <Button onClick={animateAndSetMustShow}>
{isShowResult() ? "Hide result" : "Reveal result"} {isShowResult() ? "Hide result" : "Reveal result"}
</Button> </Button>
<Show when={props.expiredAt}> <Show when={poll().expiresAt}>
<span> <span>
<span style={{ "margin-inline-end": "0.5ch" }}> <span style={{ "margin-inline-end": "0.5ch" }}>
{isBefore(now(), props.expiredAt!) ? "Expire in" : "Expired"} {isBefore(now(), poll().expiresAt!) ? "Expire in" : "Expired"}
</span> </span>
<time dateTime={props.expiredAt!.toISOString()}> <time dateTime={poll().expiresAt!}>
{formatDistance(now(), props.expiredAt!, { {formatDistance(now(), poll().expiresAt!, {
locale: dateFnLocale(), locale: dateFnLocale(),
})} })}
</time> </time>
@ -181,8 +179,8 @@ const TootPoll: Component<TootPollProps> = (props) => {
<TootPollDialog <TootPollDialog
open={showVoteDialog()} open={showVoteDialog()}
options={props.options} options={poll().options}
onVote={(votes) => console.debug(votes)} onVote={[vote, props.status]}
onClose={() => setShowVoteDialog(false)} onClose={() => setShowVoteDialog(false)}
initialVotes={[initialVote()]} initialVotes={[initialVote()]}
/> />

View file

@ -26,7 +26,13 @@ export type TootPollDialogPoll = {
initialVotes?: readonly number[]; initialVotes?: readonly number[];
multiple?: boolean; multiple?: boolean;
onVote(votes: readonly number[]): void | Promise<void>; onVote: [
(
status: mastodon.v1.Status,
votes: readonly number[],
) => void | Promise<void>,
mastodon.v1.Status,
];
onClose?: BottomSheetProps["onClose"] & onClose?: BottomSheetProps["onClose"] &
((reason: "cancel" | "success") => void); ((reason: "cancel" | "success") => void);
}; };
@ -50,7 +56,7 @@ const TootPollDialog: Component<TootPollDialogPoll> = (props) => {
const sendVote = async () => { const sendVote = async () => {
setInProgress(true); setInProgress(true);
try { try {
await props.onVote(votes()); await props.onVote[0](props.onVote[1], votes());
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
props.onClose?.("cancel"); props.onClose?.("cancel");