import { useState } from 'react';
import {
useQuery,
useQueryClient,
useSuspenseQuery,
} from '@tanstack/react-query';
import {
Link,
Outlet,
createFileRoute,
redirect,
useNavigate,
useRouter,
} from '@tanstack/react-router';
import type { ErrorComponentProps } from '@tanstack/react-router';
import { LogOut, UserRound, Users } from 'lucide-react';
import { Badge } from '@repo/ui/components/badge';
import { Button } from '@repo/ui/components/button';
import { Spinner } from '@repo/ui/components/spinner';
import { Brand } from '@/components/brand';
import { CallPanel } from '@/components/call-panel';
import { CallProvider } from '@/components/call-provider';
import { ConversationList } from '@/components/conversation-list';
import { IncomingCalls } from '@/components/incoming-calls';
import { NewGroupDialog } from '@/components/new-group-dialog';
import { NewRealmDialog } from '@/components/new-realm-dialog';
import { RealmList } from '@/components/realm-list';
import { UnreadDot } from '@/components/unread-dot';
import { UserAvatar } from '@/components/user-avatar';
import { ApiError } from '@/lib/api';
import { authClient } from '@/lib/auth-client';
import { callsQuery } from '@/lib/calls';
import { friendsQuery } from '@/lib/friends';
import { ownProfileQuery } from '@/lib/profile';
import { useRealtime } from '@/lib/realtime';
import { useUnreadSummary, useUnreadTitle } from '@/lib/unread';
const SETTINGS_PATH = '/app/settings';
export const Route = createFileRoute('/app')({
// Everything under /app is private and personal, so it renders in the
// browser only; the server never needs the visitor's session.
ssr: false,
beforeLoad: async ({ context, location }) => {
let profile;
try {
profile = await context.queryClient.fetchQuery(ownProfileQuery);
} catch (error) {
if (error instanceof ApiError && error.status === 401) {
throw redirect({ to: '/login', search: { redirect: location.href } });
}
throw error;
}
// Friends find each other by username, so nothing else opens without one.
if (profile.username === null && location.pathname !== SETTINGS_PATH) {
throw redirect({ to: SETTINGS_PATH });
}
},
pendingComponent: AppPending,
errorComponent: AppError,
component: AppLayout,
});
const navItems = [
{ to: '/app/friends', label: 'Friends', icon: Users },
{ to: SETTINGS_PATH, label: 'Profile', icon: UserRound },
] as const;
function AppLayout() {
const { data: profile } = useSuspenseQuery(ownProfileQuery);
const { data: friends } = useQuery(friendsQuery);
// Loaded up front: only calls that start after this are offered to join.
useQuery(callsQuery);
const queryClient = useQueryClient();
useRealtime();
useUnreadTitle();
const unread = useUnreadSummary();
const requestCount = friends?.incoming.length ?? 0;
const navigate = useNavigate();
const [isSigningOut, setIsSigningOut] = useState(false);
async function handleSignOut() {
setIsSigningOut(true);
await authClient.signOut();
await navigate({ to: '/' });
// After leaving, so nothing here refetches as a signed-out visitor.
queryClient.clear();
}
return (
{error instanceof ApiError ? error.message : 'The server didn’t answer. Check your connection and try again.'}