index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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: {
  13. id: string;
  14. title: string;
  15. content: string;
  16. shared: boolean;
  17. hasPassword: boolean;
  18. }[];
  19. }
  20. export const handler = define.handlers({
  21. GET(ctx) {
  22. const tokenUserId = checkToken(ctx.req);
  23. if (tokenUserId) {
  24. const user = find(
  25. "User",
  26. { id: tokenUserId },
  27. ["name"],
  28. );
  29. if (user.length > 0) {
  30. const posts = find("Post", { user_id: tokenUserId }, [
  31. "id",
  32. "title",
  33. "content",
  34. "shared",
  35. "share_password",
  36. ]);
  37. return page({
  38. name: user[0]["name"] as string,
  39. list: posts.map((post) => ({
  40. id: post["id"] as string,
  41. title: post["title"] as string,
  42. content: post["content"] as string,
  43. shared: post["shared"] === 1,
  44. hasPassword: Boolean(post["share_password"]),
  45. })),
  46. });
  47. }
  48. }
  49. return page({ name: "", list: [] });
  50. },
  51. });
  52. export default define.page((props: PageProps<HomeProps>) => {
  53. return (
  54. <>
  55. <Head>
  56. <title>Home</title>
  57. </Head>
  58. <PageContainer>
  59. {props.data.name
  60. ? (
  61. <>
  62. <HomeBar name={props.data.name} />
  63. <PostList posts={props.data.list} />
  64. </>
  65. )
  66. : <WelcomeFrame />}
  67. </PageContainer>
  68. </>
  69. );
  70. });