TootBottomSheet: set document title

This commit is contained in:
thislight 2024-10-14 20:05:08 +08:00
parent 28ddc6b3db
commit 3ea7072437
2 changed files with 18 additions and 6 deletions

View file

@ -24,6 +24,7 @@ import { css } from "solid-styled";
import { vibrate } from "../platform/hardware"; import { vibrate } from "../platform/hardware";
import { createTimeSource, TimeSourceProvider } from "../platform/timesrc"; import { createTimeSource, TimeSourceProvider } from "../platform/timesrc";
import TootComposer from "./TootComposer"; import TootComposer from "./TootComposer";
import { useDocumentTitle } from "../utils";
let cachedEntry: [string, mastodon.v1.Status] | undefined; let cachedEntry: [string, mastodon.v1.Status] | undefined;
@ -106,8 +107,14 @@ const TootBottomSheet: Component = (props) => {
} }
}); });
useDocumentTitle(() => {
const t = toot()?.reblog ?? toot()
const name = t?.account.displayName ?? "Someone"
return `${name}'s toot`
})
const tootDisplayName = () => { const tootDisplayName = () => {
const t = toot(); const t = toot()?.reblog ?? toot();
if (t) { if (t) {
return resolveCustomEmoji(t.account.displayName, t.account.emojis); return resolveCustomEmoji(t.account.displayName, t.account.emojis);
} }

View file

@ -1,18 +1,23 @@
import { createRenderEffect, createSignal, onCleanup } from "solid-js"; import {
createRenderEffect,
onCleanup,
type Accessor,
} from "solid-js";
export function useDocumentTitle(newTitle?: string) { export function useDocumentTitle(newTitle?: string | Accessor<string>) {
const capturedTitle = document.title; const capturedTitle = document.title;
const [title, setTitle] = createSignal(newTitle ?? capturedTitle);
createRenderEffect(() => { createRenderEffect(() => {
document.title = title(); if (newTitle)
document.title = typeof newTitle === "string" ? newTitle : newTitle();
}); });
onCleanup(() => { onCleanup(() => {
document.title = capturedTitle; document.title = capturedTitle;
}); });
return setTitle; return (x: ((x: string) => string) | string) =>
(document.title = typeof x === "string" ? x : x(document.title));
} }
export function mergeClass(c1: string | undefined, c2: string | undefined) { export function mergeClass(c1: string | undefined, c2: string | undefined) {