import { Handlers, PageProps } from "$fresh/server.ts"; import { getTeam, getTeamUsers, teamUserStatus } from "@/db/mod.ts"; import { type Team, type TeamUserStatus, type User } from "@/types.ts"; import { type ContextState } from "@/types.ts"; interface TeamIndexProps { status: TeamUserStatus; team: Team; users: User[]; } interface TeamStatusProps { status: TeamUserStatus; } type TeamPageProps = TeamIndexProps | TeamStatusProps; export const handler: Handlers = { async GET(request, context) { if (!context.state.user?.id) { // unauthenticated requests may not view teams return await context.renderNotFound(); } // TODO: implement this with row-level security? // TODO: do I just use supabase at this point? // TODO: only allow logged-in users to view teams (and most resources!) // TODO: only allow users that are a member of a team to view them // NOTE: maybe teams can be public...? const { id } = context.params; console.debug({ request, context }); try { const status = await teamUserStatus(context.state.user?.id, id); console.log("Status of this user on team:", status, id); if (!status) { return await context.renderNotFound(); } else if (["accepted", "manager", "owner"].includes(status)) { // users that are not a member of a team may not view it const team = await getTeam({ id }); const users = await getTeamUsers(team) || []; return await context.render({ team, users, status }); } else if (["invited", "left", "removed"].includes(status)) { return await context.render({ status }); } return await context.renderNotFound(); } catch (e) { console.error(`Error handling team page for ID '${id}'`, e); return await context.renderNotFound(); } }, }; export default function TeamPage({ data }: PageProps) { if ("users" in data) { return ; } else { return ; } } function TeamStatus({ status }: TeamStatusProps) { return <>Team status: {status}; } function TeamIndex( { team: { id, displayName, createdAt }, users, status }: TeamIndexProps, ) { return ( <> Back to dashboard

{displayName} - created {createdAt.toLocaleString()}

{/*

Administrate

*/} {["owner", "manager"].includes(status) && }

Team Members

); } function Manage({ teamId }: { teamId: string }) { // TODO: invite by email WITHOUT turning into a spambot? return (
Manage
Invite User to Team
); }