import type { ReactNode } from "react";
import { NavLink } from "react-router";
import { Bell, ChartColumn, House, Info, LogOut, MessageSquare, Search, Settings, User } from "lucide-react";
import { api } from "./api";
import { useAuthChanged, useMe } from "./auth";
import { useUnread } from "./chats";
import { lockKeyring } from "./keyring";
type Target = { title: string; icon?: ReactNode; to: string; end?: boolean; badge?: number };
// Bereiche, die es noch nicht gibt: Route und Platzhalter-Seite bestehen,
// in der Navigation sind sie bis dahin ausgeblendet.
const unfinished = new Set(["/search", "/notifications"]);
export const isReady = (to: string) => !unfinished.has(to);
// useMainTargets liefert die fünf wichtigsten Ziele (TabBar, Kopf der
// Seitenleiste). Das Profil zeigt angemeldet aufs eigene Profil, sonst auf
// /me mit dem Login.
function useMainTargets(): Target[] {
const me = useMe().data;
const profile = me ? `/u/${encodeURIComponent(me.username)}` : "/me";
const unread = useUnread();
const targets: Target[] = [
{ title: "Feed", icon: , to: "/", end: true },
{ title: "Suche", icon: , to: "/search" },
{ title: "Benachrichtigungen", icon: , to: "/notifications" },
{ title: "Chats", icon: , to: "/chats", badge: unread },
{ title: "Profil", icon: , to: profile },
];
return targets.filter((t) => isReady(t.to));
}
const moreTargets: Target[] = [
{ title: "Einstellungen", icon: , to: "/settings" },
{ title: "Statistik", icon: , to: "/stats" },
{ title: "Info", icon: , to: "/info" },
];
// Die Rechtstexte stehen ohne Icon in der Seitenleiste.
const docs: Target[] = [
{ title: "Datenschutz", to: "/datenschutz" },
{ title: "Beitragsordnung", to: "/beitragsordnung" },
{ title: "Impressum", to: "/impressum" },
];
// Sidebar: alle Ziele mit Icon und Titel. Auf dem Desktop immer sichtbar,
// mobil die Schublade hinter dem Menü (open).
export function Sidebar({ open, onNavigate }: { open: boolean; onNavigate: () => void }) {
const main = useMainTargets();
return (
);
}
function TargetList({ targets, onNavigate }: { targets: Target[]; onNavigate: () => void }) {
return (
{targets.map((t) => (
{t.icon}{t.title}
))}
);
}
// Logout steht nur angemeldet unten in der Seitenleiste. POST statt GET: als
// GET wäre Logout per fremdem Link auslösbar (CSRF).
function Logout({ onDone }: { onDone: () => void }) {
const me = useMe().data;
const authChanged = useAuthChanged();
if (!me) return null;
const logout = async () => {
await api.post("/auth/logout").catch(() => undefined);
lockKeyring();
await authChanged();
onDone();
};
return (
);
}
// Badge: schlichter Zähler hinter dem Symbol, etwa ungelesene Nachrichten.
function Badge({ n }: { n?: number }) {
if (!n) return null;
return {n > 99 ? "99+" : n};
}
const label = (t: Target) => (t.badge ? `${t.title} (${t.badge} ungelesen)` : t.title);
// TabBar: mobil unten die (bis zu) fünf wichtigsten Ziele, nur Icons.
export function TabBar() {
return (
);
}