import { cx } from "@twind/core"; import React, { ReactNode } from "react"; import { NavLink } from "react-router-dom"; import useSWR from "swr"; import { fetchApi, handleResponse } from "./apiClient.tsx"; function NavTab(props: { to: string; children: ReactNode }) { return ( cx("tab", isActive && "tab-active")} to={props.to} > {props.children} ); } export function AppHeader( props: { className?: string; sessionId?: string; onLogOut: () => void }, ) { const { className, sessionId, onLogOut } = props; const session = useSWR( sessionId ? ["sessions/{sessionId}", "GET", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), { onError: () => onLogOut() }, ); const user = useSWR( session.data?.userId ? ["users/{userId}", "GET", { params: { userId: String(session.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const bot = useSWR( ["bot", "GET", {}] as const, (args) => fetchApi(...args).then(handleResponse), ); const userPhoto = useSWR( session.data?.userId ? ["users/{userId}/photo", "GET", { params: { userId: String(session.data.userId) }, }] as const : null, (args) => fetchApi(...args).then(handleResponse).then((blob) => URL.createObjectURL(blob)), ); return (
{/* logo */} logo {/* tabs */} {/* loading indicator */} {session.isLoading || user.isLoading ?
: null} {/* user avatar */} {user.data ? userPhoto.data ? ( avatar ) : (
{user.data.first_name.at(0)?.toUpperCase()}
) : null} {/* login/logout button */} {!session.isLoading && !user.isLoading && bot.data && sessionId ? ( user.data ? ( ) : ( Login ) ) : null}
); }