ls-deno/routes/dashboard.tsx

53 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-10-21 03:06:37 -05:00
import { getUserTeams } from "@/db/mod.ts";
2022-10-11 16:17:21 -05:00
import { Handlers, PageProps } from "$fresh/server.ts";
2022-10-21 03:06:37 -05:00
import { type ContextState, type PublicUser, type Team } from "@/types.ts";
interface DashboardProps {
user: PublicUser;
teams: Team[];
}
2022-10-08 02:01:48 -05:00
2022-10-08 02:53:13 -05:00
export const handler: Handlers<unknown, ContextState> = {
async GET(_request: Request, context) {
2022-10-21 03:06:37 -05:00
if (context.state.user?.id) {
const teams = await getUserTeams(context.state.user) || [];
return await context.render({ user: context.state.user, teams });
}
return await context.render();
2022-10-08 02:01:48 -05:00
},
};
2022-10-21 03:06:37 -05:00
export default function Page(
{ data }: PageProps<DashboardProps | undefined>,
) {
2022-10-08 02:01:48 -05:00
if (data) {
2022-10-21 03:06:37 -05:00
return Dashboard(data);
2022-10-08 02:01:48 -05:00
} else {
return LoginRequired();
}
}
2022-10-21 03:06:37 -05:00
function Dashboard({ teams, user }: DashboardProps) {
2022-10-08 02:01:48 -05:00
return (
2022-10-21 03:06:37 -05:00
<>
<h2 class="text-4xl mb-2">
Hello, {(user.displayName || user.username).trim()}!
</h2>
<h3 class="text-lg">
Which team are we working with today?
</h3>
<ul>
{teams.map((team) => (
<li key={team.id}>
<a href={`/team/${team.id}`}>{team.displayName}</a>
</li>
))}
</ul>
</>
2022-10-08 02:01:48 -05:00
);
}
function LoginRequired() {
2022-10-11 23:49:36 -05:00
return <a href="/login">You need to login first!</a>;
2022-10-08 02:01:48 -05:00
}