Links neben dem Avatar stehen nur noch Anzeigename und @handle; das Datum (samt Bump-Cooldown) rückt nach rechts, direkt links neben Melden. So bleibt der Name ruhig und die Zeit steht immer an derselben Stelle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import { Link } from "react-router";
|
|
|
|
// avatarUrl liefert den Bildpfad oder das Platzhalterbild bei leerem Avatar.
|
|
export function avatarUrl(path: string): string {
|
|
return path ? `/${path}` : "/static/assets/no_profile_pic.jpg";
|
|
}
|
|
|
|
// Wer schreibt: Handle (@name, eindeutig) und frei gewählter Anzeigename,
|
|
// '' = keiner.
|
|
export type Author = { handle: string; display_name: string };
|
|
|
|
// AuthorName steht in der Byline neben dem Avatar, zweizeilig: oben der
|
|
// Anzeigename (ohne einen der Handle), darunter "@handle" und meta, etwa die
|
|
// Zahl ungelesener Nachrichten. Beide Zeilen verlinken auf to.
|
|
export function AuthorName({ u, to, meta }: { u: Author; to: string; meta?: ReactNode }) {
|
|
return (
|
|
<span className="author">
|
|
<Link to={to}>{u.display_name || u.handle}</Link>
|
|
<span>
|
|
<Link to={to}>@{u.handle}</Link>
|
|
{meta}
|
|
</span>
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export function formatTime(unix: number): string {
|
|
return new Date(unix * 1000).toLocaleString("de-DE");
|
|
}
|
|
|
|
const urlPattern = /https?:\/\/[^\s<]+/g;
|
|
|
|
// Hashtag wie in der Suche (internal/httpapi/search.go): Buchstaben, Ziffern,
|
|
// _, davor keines davon -- "a#b" ist kein Tag.
|
|
const tagPattern = /(^|[^\p{L}\p{N}_])#([\p{L}\p{N}_]+)/gu;
|
|
|
|
// tagify verlinkt die Hashtags eines Textstücks auf die Suche nach genau
|
|
// diesem Tag.
|
|
function tagify(text: string, key: string): ReactNode[] {
|
|
const out: ReactNode[] = [];
|
|
let last = 0;
|
|
for (const m of text.matchAll(tagPattern)) {
|
|
const start = m.index + m[1].length;
|
|
if (start > last) out.push(text.slice(last, start));
|
|
out.push(
|
|
<Link key={`${key}-t${start}`} to={`/search?tag=${encodeURIComponent(m[2])}`}>
|
|
#{m[2]}
|
|
</Link>,
|
|
);
|
|
last = m.index + m[0].length;
|
|
}
|
|
if (last < text.length) out.push(text.slice(last));
|
|
return out;
|
|
}
|
|
|
|
// linkify macht aus URLs im Text Links, aus Hashtags Links auf die Suche und
|
|
// aus Zeilenumbrüchen <br>. Den Text escaped React selbst.
|
|
export function linkify(text: string): ReactNode[] {
|
|
const out: ReactNode[] = [];
|
|
text.split("\n").forEach((line, i) => {
|
|
if (i > 0) out.push(<br key={`br${i}`} />);
|
|
let last = 0;
|
|
for (const m of line.matchAll(urlPattern)) {
|
|
const start = m.index;
|
|
if (start > last) out.push(...tagify(line.slice(last, start), `${i}-${last}`));
|
|
out.push(
|
|
<a key={`a${i}-${start}`} href={m[0]} target="_blank" rel="noopener">
|
|
{m[0]}
|
|
</a>,
|
|
);
|
|
last = start + m[0].length;
|
|
}
|
|
if (last < line.length) out.push(...tagify(line.slice(last), `${i}-${last}`));
|
|
});
|
|
return out;
|
|
}
|