index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Head } from "fresh/runtime";
  2. import { page, type PageProps } from "fresh";
  3. import { checkToken } from "utils/server.ts";
  4. import { find } from "utils/db.ts";
  5. import { define } from "utils/state.ts";
  6. import HomeBar from "../islands/HomeBar.tsx";
  7. import PostList from "../islands/PostList.tsx";
  8. import WelcomeFrame from "../islands/WelcomeFrame.tsx";
  9. import PageContainer from "../components/layout/PageContainer.tsx";
  10. interface HomeProps {
  11. name: string;
  12. list: { id: string; title: string; content: string; shared: boolean }[];
  13. }
  14. export const handler = define.handlers({
  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 page({
  31. name: user[0]["name"] as string,
  32. list: posts.map((post) => ({
  33. id: post["id"] as string,
  34. title: post["title"] as string,
  35. content: post["content"] as string,
  36. shared: post["shared"] === 1,
  37. })),
  38. });
  39. }
  40. }
  41. return page({ name: "", list: [] });
  42. },
  43. });
  44. export default define.page((props: PageProps<HomeProps>) => {
  45. return (
  46. <>
  47. <Head>
  48. <title>Home</title>
  49. </Head>
  50. <PageContainer>
  51. {props.data.name
  52. ? (
  53. <>
  54. <HomeBar name={props.data.name} />
  55. <PostList posts={props.data.list} />
  56. </>
  57. )
  58. : <WelcomeFrame />}
  59. </PageContainer>
  60. </>
  61. );
  62. });