| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { Head } from "fresh/runtime";
- import { createDefine } from "fresh";
- import type { PageProps } from "fresh";
- import { checkToken } from "utils/server.ts";
- import { find } from "utils/db.ts";
- import HomeBar from "../islands/HomeBar.tsx";
- import PostList from "../islands/PostList.tsx";
- import WelcomeFrame from "../islands/WelcomeFrame.tsx";
- const define = createDefine<Record<never, never>>();
- interface HomeProps {
- name: string;
- list: { id: string; title: string; content: string; shared: boolean }[];
- }
- export const handler = define.handlers<HomeProps>({
- GET(ctx) {
- const tokenUserId = checkToken(ctx.req);
- if (tokenUserId) {
- const user = find(
- "User",
- { id: tokenUserId },
- ["name"],
- );
- if (user.length > 0) {
- const posts = find("Post", { user_id: tokenUserId }, [
- "id",
- "title",
- "content",
- "shared",
- ]);
- return {
- data: {
- name: user[0][0] as string,
- list: posts.map((post) => ({
- id: post[0] as string,
- title: post[1] as string,
- content: post[2] as string,
- shared: post[3] as boolean,
- })),
- },
- };
- }
- }
- return { data: { name: "", list: [] } };
- },
- });
- export default function Home(props: PageProps<HomeProps>) {
- return (
- <>
- <Head>
- <title>Home</title>
- </Head>
- <div className="pd-page">
- {props.data.name
- ? (
- <>
- <HomeBar name={props.data.name} />
- <PostList posts={props.data.list} />
- </>
- )
- : <WelcomeFrame />}
- </div>
- </>
- );
- }
|