tutu/src/timelines/TootComposer.tsx

457 lines
12 KiB
TypeScript
Raw Normal View History

2024-09-25 19:30:05 +08:00
import {
createEffect,
createMemo,
createRenderEffect,
2024-09-25 19:30:05 +08:00
createSignal,
Show,
2024-11-09 16:56:18 +08:00
type Accessor,
2024-09-25 19:30:05 +08:00
type Component,
2024-09-28 15:15:06 +08:00
type JSX,
type Ref,
2024-09-25 19:30:05 +08:00
} from "solid-js";
2024-11-22 17:16:56 +08:00
import Scaffold from "~material/Scaffold";
2024-09-25 19:30:05 +08:00
import {
Avatar,
Button,
IconButton,
List,
ListItemButton,
ListItemIcon,
ListItemSecondaryAction,
ListItemText,
Radio,
Switch,
Divider,
2024-09-28 14:39:20 +08:00
CircularProgress,
Toolbar,
MenuItem,
ListItemAvatar,
2024-09-25 19:30:05 +08:00
} from "@suid/material";
import {
ArrowDropDown,
Public as PublicIcon,
Send,
People as PeopleIcon,
ThreeP as ThreePIcon,
ListAlt as ListAltIcon,
2024-09-27 14:53:28 +08:00
Visibility,
Translate,
Close,
MoreVert,
2024-09-25 19:30:05 +08:00
} from "@suid/icons-material";
import type { Account } from "../accounts/stores";
2024-11-09 16:56:18 +08:00
import "./TootComposer.css";
2024-11-22 17:16:56 +08:00
import BottomSheet from "~material/BottomSheet";
import { useLanguage } from "~platform/i18n";
2024-09-27 14:53:28 +08:00
import iso639_1 from "iso-639-1";
import ChooseTootLang from "./ChooseTootLang";
2024-09-28 14:39:20 +08:00
import type { mastodon } from "masto";
2024-11-22 17:16:56 +08:00
import cardStyles from "~material/cards.module.css";
import Menu, { createManagedMenuState } from "~material/Menu";
import { useDefaultSession } from "../masto/clients";
import { resolveCustomEmoji } from "../masto/toot";
2024-11-22 17:16:56 +08:00
import SizedTextarea from "~platform/SizedTextarea";
2024-09-25 19:30:05 +08:00
type TootVisibility = "public" | "unlisted" | "private" | "direct";
const TootVisibilityPickerDialog: Component<{
open?: boolean;
class?: string;
2024-09-25 19:30:05 +08:00
onClose: () => void;
visibility: TootVisibility;
onVisibilityChange: (value: TootVisibility) => void;
}> = (props) => {
type Kind = "public" | "private" | "direct";
const kind = () =>
props.visibility === "public" || props.visibility === "unlisted"
? "public"
: props.visibility;
const setKind = (nv: Kind) => {
if (nv == "public") {
props.onVisibilityChange(discoverable() ? "public" : "unlisted");
} else {
props.onVisibilityChange(nv);
}
};
const discoverable = () => {
return props.visibility === "public";
};
2024-09-25 19:30:05 +08:00
const setDiscoverable = (setter: (v: boolean) => boolean) => {
const nval = setter(discoverable());
2024-09-25 19:30:05 +08:00
props.onVisibilityChange(nval ? "public" : "unlisted"); // trigger change
};
return (
<BottomSheet
open={props.open}
onClose={props.onClose}
bottomUp
class={props.class}
>
2024-09-25 19:30:05 +08:00
<Scaffold
bottom={
<div
style={{
"border-top": "1px solid #ddd",
background: "var(--tutu-color-surface)",
2024-11-19 17:55:36 +08:00
padding: "8px 16px calc(8px + var(--safe-area-inset-bottom, 0px))",
2024-09-25 19:30:05 +08:00
width: "100%",
"text-align": "end",
}}
>
<Button onClick={props.onClose}>Confirm</Button>
</div>
}
>
<List dense>
2024-09-25 19:30:05 +08:00
<ListItemButton onClick={[setKind, "public"]}>
<ListItemIcon>
<PublicIcon />
</ListItemIcon>
<ListItemText
primary="Public"
secondary="Everyone can see this toot"
></ListItemText>
<ListItemSecondaryAction>
<Radio checked={kind() == "public"}></Radio>
</ListItemSecondaryAction>
</ListItemButton>
<ListItemButton
sx={{ paddingLeft: "40px" }}
disabled={kind() !== "public"}
onClick={() => setDiscoverable((x) => !x)}
>
<ListItemIcon>
<ListAltIcon />
</ListItemIcon>
<ListItemText
primary="Discoverable"
secondary="The others can discover it on the exploration."
></ListItemText>
<ListItemSecondaryAction>
<Switch
checked={discoverable()}
disabled={kind() !== "public"}
></Switch>
</ListItemSecondaryAction>
</ListItemButton>
<Divider />
<ListItemButton onClick={[setKind, "private"]}>
<ListItemIcon>
<PeopleIcon />
</ListItemIcon>
<ListItemText
primary="Only Followers"
secondary="Visibile for followers only"
></ListItemText>
<ListItemSecondaryAction>
<Radio checked={kind() == "private"}></Radio>
</ListItemSecondaryAction>
</ListItemButton>
<Divider />
<ListItemButton onClick={[setKind, "direct"]}>
<ListItemIcon>
<ThreePIcon />
</ListItemIcon>
<ListItemText
primary="Only Mentions"
secondary="Visible for mentioned users only"
></ListItemText>
<ListItemSecondaryAction>
<Radio checked={kind() == "direct"}></Radio>
</ListItemSecondaryAction>
</ListItemButton>
</List>
</Scaffold>
</BottomSheet>
);
};
const TootLanguagePickerDialog: Component<{
open?: boolean;
class?: string;
onClose: () => void;
code: string;
onCodeChange: (nval: string) => void;
}> = (props) => {
return (
<BottomSheet open={props.open} onClose={props.onClose} class={props.class}>
<Show when={props.open}>
<ChooseTootLang
code={props.code}
onCodeChange={props.onCodeChange}
onClose={props.onClose}
/>
</Show>
</BottomSheet>
);
};
2024-09-28 15:15:06 +08:00
function randomChoose<T extends any[]>(
rn: number,
K: T,
): T extends Array<infer E> ? E : never {
2024-09-28 22:46:28 +08:00
const idx = Math.floor(rn * K.length);
2024-09-28 15:15:06 +08:00
return K[idx];
}
2024-11-09 16:56:18 +08:00
function useRandomChoice<T>(choices: () => T[]): Accessor<T> {
return createMemo(() => randomChoose(Math.random(), choices()));
}
function cancelEvent(event: Event) {
event.stopPropagation();
}
2024-09-28 15:15:06 +08:00
const TootComposer: Component<{
ref?: Ref<HTMLDivElement>;
style?: JSX.CSSProperties;
profile?: Account;
replyToDisplayName?: string;
2024-09-28 14:39:20 +08:00
mentions?: readonly string[];
client?: mastodon.rest.Client;
inReplyToId?: string;
onSent?: (status: mastodon.v1.Status) => void;
2024-09-25 19:30:05 +08:00
}> = (props) => {
let inputRef: HTMLTextAreaElement;
const session = useDefaultSession();
const [active, setActive] = createSignal(false);
2024-09-28 14:39:20 +08:00
const [sending, setSending] = createSignal(false);
2024-09-25 19:30:05 +08:00
const [visibility, setVisibility] = createSignal<TootVisibility>("public");
const [permPicker, setPermPicker] = createSignal(false);
const [language, setLanguage] = createSignal("en");
2024-09-27 14:53:28 +08:00
const [langPickerOpen, setLangPickerOpen] = createSignal(false);
const appLanguage = useLanguage();
const [openMenu, menuState] = createManagedMenuState();
2024-11-09 16:56:18 +08:00
const randomPlaceholder = useRandomChoice(() => [
"What's happening?",
"What do you think?",
]);
createEffect(() => {
const lang = appLanguage().split("-")[0];
setLanguage(lang);
});
2024-09-25 19:30:05 +08:00
2024-09-28 15:29:21 +08:00
createEffect(() => {
if (active()) {
2024-09-28 15:29:21 +08:00
setTimeout(() => inputRef.focus(), 0);
}
});
2024-09-28 14:39:20 +08:00
createEffect(() => {
if (inputRef.value !== "") return;
if (props.mentions) {
const prepText = props.mentions.join(" ") + " ";
inputRef.value = prepText;
}
});
2024-09-25 19:30:05 +08:00
const containerStyle = () =>
active() || permPicker()
2024-09-25 19:30:05 +08:00
? {
position: "sticky" as const,
top: "var(--scaffold-topbar-height, 0)",
bottom: "var(--safe-area-inset-bottom, 0)",
"z-index": 2,
2024-09-28 15:15:06 +08:00
...props.style,
2024-09-25 19:30:05 +08:00
}
: undefined;
const visibilityText = () => {
switch (visibility()) {
case "public":
return "Discoverable";
case "unlisted":
return "Public";
case "private":
return "Only Followers";
case "direct":
return "Only Mentions";
}
};
const idempotencyKey = createMemo(() => window.crypto.randomUUID());
2024-09-28 14:39:20 +08:00
const send = async () => {
const client = session()?.client;
if (!client) return;
2024-09-28 14:39:20 +08:00
setSending(true);
try {
const status = await client.v1.statuses.create(
2024-09-28 14:39:20 +08:00
{
status: inputRef.value,
language: language(),
visibility: visibility(),
inReplyToId: props.inReplyToId,
},
{
requestInit: {
headers: {
["Idempotency-Key"]: idempotencyKey(),
2024-09-28 14:39:20 +08:00
},
},
},
);
props.onSent?.(status);
inputRef.value = "";
} finally {
setSending(false);
}
};
2024-09-25 19:30:05 +08:00
return (
<div
2024-09-28 15:15:06 +08:00
ref={props.ref}
2024-11-09 16:56:18 +08:00
class={/* @once */ `TootComposer ${cardStyles.card}`}
2024-09-25 19:30:05 +08:00
style={containerStyle()}
on:touchend={
cancelEvent
/* on: is required to register the event handler on the exact element */
}
on:touchmove={cancelEvent}
on:wheel={cancelEvent}
2024-09-25 19:30:05 +08:00
>
<Show when={active()}>
<Toolbar class={cardStyles.cardNoPad}>
<IconButton
onClick={[setActive, false]}
aria-label="Close the composer"
>
<Close />
</IconButton>
<IconButton
onClick={(e) => openMenu(e.currentTarget.getBoundingClientRect())}
>
<MoreVert />
</IconButton>
</Toolbar>
<div class={cardStyles.cardNoPad}>
<Menu {...menuState}>
<MenuItem>
<ListItemAvatar>
<Avatar src={session()?.account.inf?.avatar}></Avatar>
</ListItemAvatar>
<ListItemText secondary={"Default account"}>
<span
ref={(e) => {
createRenderEffect(() => {
const inf = session()?.account.inf;
return (e.innerHTML = resolveCustomEmoji(
inf?.displayName || "",
inf?.emojis ?? [],
));
});
}}
></span>
</ListItemText>
</MenuItem>
</Menu>
</div>
</Show>
2024-11-09 16:56:18 +08:00
<div class="reply-input">
2024-09-28 15:15:06 +08:00
<Show when={props.profile}>
<Avatar
src={props.profile!.inf?.avatar}
sx={{ marginLeft: "-0.25em" }}
/>
</Show>
<SizedTextarea
2024-09-25 19:30:05 +08:00
ref={inputRef!}
2024-09-28 15:15:06 +08:00
placeholder={
props.replyToDisplayName
? `Reply to ${props.replyToDisplayName}...`
: randomPlaceholder()
2024-09-28 15:15:06 +08:00
}
onFocus={[setActive, true]}
2024-09-25 19:30:05 +08:00
style={{ width: "100%", border: "none" }}
2024-09-28 14:39:20 +08:00
disabled={sending()}
autocomplete="off"
></SizedTextarea>
2024-09-28 14:39:20 +08:00
<Show when={props.client}>
<Show
when={!sending()}
fallback={
<div style={{ padding: "8px" }}>
<CircularProgress
sx={{
marginRight: "-0.5em",
width: "1.5rem",
height: "1.5rem",
}}
/>
</div>
}
>
<IconButton
sx={{ marginRight: "-0.5em" }}
onClick={send}
aria-label="Send"
>
2024-09-28 14:39:20 +08:00
<Send />
</IconButton>
</Show>
</Show>
2024-09-25 19:30:05 +08:00
</div>
<Show when={active()}>
<div class="options">
<Button
startIcon={<Translate />}
endIcon={<ArrowDropDown />}
onClick={[setLangPickerOpen, true]}
disabled={sending()}
>
<span style={{ "vertical-align": "bottom" }}>
{iso639_1.getNativeName(language())}
</span>
</Button>
<Button
startIcon={<Visibility />}
endIcon={<ArrowDropDown />}
onClick={[setPermPicker, true]}
disabled={sending()}
>
<span style={{ "vertical-align": "bottom" }}>
{visibilityText()}
</span>
</Button>
</div>
2024-09-25 19:30:05 +08:00
<TootVisibilityPickerDialog
class={cardStyles.cardNoPad}
open={permPicker()}
onClose={() => setPermPicker(false)}
visibility={visibility()}
onVisibilityChange={setVisibility}
/>
2024-09-27 14:53:28 +08:00
<TootLanguagePickerDialog
class={cardStyles.cardNoPad}
open={langPickerOpen()}
onClose={() => setLangPickerOpen(false)}
code={language()}
onCodeChange={setLanguage}
/>
</Show>
2024-09-25 19:30:05 +08:00
</div>
);
};
2024-09-28 15:15:06 +08:00
export default TootComposer;