index.tsx 1.6 KB

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