index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Head } from "fresh/runtime";
  2. import { createDefine } from "fresh";
  3. import type { PageProps } from "fresh";
  4. import { checkToken } from "utils/server.ts";
  5. import { find } from "utils/db.ts";
  6. import HomeBar from "../islands/HomeBar.tsx";
  7. import PostList from "../islands/PostList.tsx";
  8. import WelcomeFrame from "../islands/WelcomeFrame.tsx";
  9. const define = createDefine<Record<never, never>>();
  10. interface HomeProps {
  11. name: string;
  12. list: { id: string; title: string; content: string; shared: boolean }[];
  13. }
  14. export const handler = define.handlers<HomeProps>({
  15. GET(ctx) {
  16. const tokenUserId = checkToken(ctx.req);
  17. if (tokenUserId) {
  18. const user = find(
  19. "User",
  20. { id: tokenUserId },
  21. ["name"],
  22. );
  23. if (user.length > 0) {
  24. const posts = find("Post", { user_id: tokenUserId }, [
  25. "id",
  26. "title",
  27. "content",
  28. "shared",
  29. ]);
  30. return {
  31. data: {
  32. name: user[0][0] as string,
  33. list: posts.map((post) => ({
  34. id: post[0] as string,
  35. title: post[1] as string,
  36. content: post[2] as string,
  37. shared: post[3] as boolean,
  38. })),
  39. },
  40. };
  41. }
  42. }
  43. return { data: { name: "", list: [] } };
  44. },
  45. });
  46. export default function Home(props: PageProps<HomeProps>) {
  47. return (
  48. <>
  49. <Head>
  50. <title>Home</title>
  51. </Head>
  52. <div className="pd-page">
  53. {props.data.name
  54. ? (
  55. <>
  56. <HomeBar name={props.data.name} />
  57. <PostList posts={props.data.list} />
  58. </>
  59. )
  60. : <WelcomeFrame />}
  61. </div>
  62. </>
  63. );
  64. }