Files
Kontrollverlust/frontend/src/format.tsx
T
irrlichtandClaude Opus 5.5 a4a1860342 Darstellung: gemeinsame Byline für Beiträge und Chats
Die Chat-Nachrichten bauten ihren Kopf selbst und hatten die letzten
Änderungen (zweizeiliger Name, Datum rechts) nicht mitbekommen. Jetzt nutzen
Beitragskarten, Chat-Liste und Chat-Nachrichten dieselbe Komponente Byline;
Zusätze wie Repost-Countdown, "N neu", "Signatur nicht prüfbar" oder Melden
kommen über Slots hinein. So muss der Kopf nur an einer Stelle gepflegt
werden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:05:47 +01:00

70 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";
}
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,
// _; Erwähnung wie im Backend (internal/httpapi/mentions.go): @ und Handle.
// Davor jeweils keines dieser Zeichen -- "a#b" ist kein Tag, "mail@bob.org"
// keine Erwähnung.
const tagPattern = /(^|[^\p{L}\p{N}_])(?:#([\p{L}\p{N}_]+)|@([A-Za-z0-9_]+))/gu;
// Mentions: Handle, wie er im Text steht (klein) -> heutiger Handle derselben
// Person (Entry.mentions).
export type Mentions = Record<string, string>;
// tagify verlinkt die Hashtags eines Textstücks auf die Suche nach genau
// diesem Tag und die Erwähnungen aus mentions aufs Profil; unbekannte @…
// bleiben Text.
function tagify(text: string, key: string, mentions: Mentions): ReactNode[] {
const out: ReactNode[] = [];
let last = 0;
for (const m of text.matchAll(tagPattern)) {
const start = m.index + m[1].length;
const [tag, handle] = [m[2], m[3]];
const to = tag ? `/search?tag=${encodeURIComponent(tag)}` : mentions[handle.toLowerCase()];
if (!to) continue;
if (start > last) out.push(text.slice(last, start));
out.push(
<Link key={`${key}-t${start}`} to={tag ? to : `/u/${encodeURIComponent(to)}`}>
{tag ? `#${tag}` : `@${handle}`}
</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, aus
// bekannten Erwähnungen Links aufs Profil und aus Zeilenumbrüchen <br>. Den
// Text escaped React selbst.
export function linkify(text: string, mentions: Mentions = {}): 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}`, mentions));
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}`, mentions));
});
return out;
}