import { renderToString } from "https://esm.sh/react-dom@18.2.0/server";
import { html } from "https://esm.town/v/stevekrouse/html";
export type PageProps<T = {}> = T & {
route: string;
params: Record<string, string>;
};
type Context = PageProps & {
render: (props) => Response | Promise<Response>;
};
export type Action = (req: Request, ctx: Context) => Response | Promise<Response>;
export function handler(Component, action?: Action) {
return async (c) => {
if (action) {
return action(c.req.raw, {
params: c.req.param(),
route: c.req.routePath,
render(props) {
return html(renderToString(
<Component
{...{
params: c.req.param(),
route: c.req.routePath,
...props,
}}
/>,
));
},
});
}
return html(renderToString(
<Component
{...{
params: c.req.param(),
route: c.req.routePath,
}}
/>,
));
};
}