index.tsx 1.6 KB

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