-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import '@/globals'; | ||
|
||
log('PartBot is starting up...'); | ||
|
||
import '@/ps'; | ||
import '@/discord'; | ||
import '@/web'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import type { Request, Response } from 'express'; | ||
import type { ReactElement } from 'react'; | ||
|
||
export type RouteHandler = (req: Request, res: Response) => void; | ||
export type RouteHandler = (req: Request, res: Response & { render: Render }) => void; | ||
|
||
export type Route = { | ||
export type APIRoute = { | ||
handler: RouteHandler; | ||
verb?: 'get' | 'post'; | ||
}; | ||
|
||
export type UIRoute = { | ||
handler: RouteHandler; | ||
}; | ||
|
||
export type Render = (jsx: ReactElement, title: string, hydrate: boolean) => Promise<Response>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { renderToString } from 'react-dom/server'; | ||
import { jsxToHTML } from '@/utils/jsx-to-html'; | ||
import { renderTemplate } from '@/web/loaders/util'; | ||
|
||
import type { Request, Response, NextFunction } from 'express'; | ||
import type { Render } from '@/types/web'; | ||
|
||
export function renderReact(req: Request, res: Response, next: NextFunction): void { | ||
const render: Render = async (jsx, title, hydrate) => { | ||
if (!hydrate) { | ||
const content = jsxToHTML(jsx); | ||
const page = await renderTemplate('static-react.html', { title, content }); | ||
return res.send(page); | ||
} | ||
const preHydrated = renderToString(jsx); | ||
const page = await renderTemplate('react.html', { title, preHydrated }); | ||
return res.send(page); | ||
}; | ||
Object.assign(res, { render }); | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Router, Application } from 'express'; | ||
import { readFileStructure } from '@/web/loaders/util'; | ||
|
||
import { renderReact } from '@/web/loaders/middleware'; | ||
|
||
import type { UIRoute } from '@/types/web'; | ||
|
||
export default async function init(app: Application): Promise<void> { | ||
const router = Router(); | ||
|
||
router.use(renderReact); | ||
|
||
const routes = await readFileStructure(fsPath('web', 'ui')); | ||
await Promise.all( | ||
Object.entries(routes).map(async ([urlPath, filepath]) => { | ||
const route: UIRoute = await import(filepath); | ||
router.get(urlPath, route.handler); | ||
}) | ||
); | ||
|
||
app.use(router); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0" /> | ||
<title>{{title}}</title> | ||
</head> | ||
|
||
<body id="react-root"> | ||
{{preHydrated}} | ||
</body> | ||
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script> | ||
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script> | ||
<script> | ||
ReactDOM.hydrateRoot({{content}}); // Okay crap this won't work; TODO | ||
</script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1.0" /> | ||
<title>{{title}}</title> | ||
</head> | ||
|
||
<body id="react-root"> | ||
{{content}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const Div = () => <div>Test content here; fully static</div>; | ||
|
||
export const handler: RouteHandler = (req, res) => { | ||
return res.render(<Div />, 'Test Title', false); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters