fe04f67 (parent 5c6496d)5/9/2026, 8:25:56 AM
.ts
TypeScript
(application/typescript)
// 3rd-party
import type { ViewContext } from "@ethicdevs/fastify-stream-react-views";
import type { FastifyReply, FastifyRequest } from "fastify";
// app root
import pkgJson from "../../../package.json";
// app
import type { CommonViewProps } from "../../types";
import { Const } from "../../const";

export const makeRequestHandler = {
  getter: () => {
    return (request: FastifyRequest, reply: FastifyReply) => {
      return <T extends Record<string, unknown>>(
        viewName: string,
        props?: T & CommonViewProps,
        viewCtx?: ViewContext,
      ) => {
        const {
          authenticated,
          curr_user_uid,
          curr_user_avatar_uri,
          curr_user_role,
          curr_user_username,
          flash_message,
        } = request.session.data;

        const title = props != null ? props.title : viewName;

        const themeSchemeFromCookies =
          "theme_scheme" in request.cookies
            ? request.cookies["theme_scheme"].split(".")[0]
            : null;

        const themeScheme =
          (themeSchemeFromCookies || Const.DEFAULT_THEME_SCHEME) === "light"
            ? "light"
            : "dark";

        const viewProps: T & { commonProps: CommonViewProps } = {
          ...props,
          commonProps: {
            appVersion: pkgJson.version,
            authenticated,
            currentUserAvatarUri: curr_user_avatar_uri,
            currentUserId: curr_user_uid,
            currentUserRole: curr_user_role,
            currentUserUsername: curr_user_username,
            flashMessage: flash_message,
            gitStamp: request.gitStamp,
            themeScheme,
            title,
          },
        } as T & { commonProps: CommonViewProps };

        return reply.streamReactView(viewName, viewProps as any, viewCtx);
      };
    };
  },
};