-
Notifications
You must be signed in to change notification settings - Fork 17
Add JSX view engine #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add JSX view engine #132
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
437cdb6
feat: Add JSX renderer
mrjvs f662fee
feat: port messages.ejs to JSX
mrjvs c4a5918
feat: add util to easily build render context for JSX
mrjvs e873262
feat: Implement new JSX view to messages list page
mrjvs 10ef12a
fix: add proper build steps
mrjvs 99ebbea
Fix JSX bugs
mrjvs 13fb320
chore: add jsx files to nodemon reloader
mrjvs 8940478
fix: Add html doctype to JSX rendered pages
mrjvs e91e43a
feat: add support for directory based rendering of JSX
mrjvs b464e7e
feat: Add JSX view for portal
mrjvs 12e07ed
feat: Add support for omitting doctype on some rendered jsx pages
mrjvs 7f79fac
feat: Add messages view to CTR
mrjvs c8f7cbe
fix: Fix express.d.ts types
mrjvs 91e5f33
fix: Fix bugs when rendering portal version of jsx
mrjvs 40f86d8
Add SVG todos
mrjvs 92cc451
Add typing to conversations in tsx view
mrjvs 74e6c30
chore: update eslint config
mrjvs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| import { renderToStaticMarkup } from 'react-dom/server'; | ||
| import type { RequestHandler } from 'express'; | ||
|
|
||
| const htmlDoctype = '<!DOCTYPE html>'; | ||
|
|
||
| /** | ||
| * Render JSX as static markup. Only static! No state or event handlers are supported. | ||
| */ | ||
| export const jsxRenderer: RequestHandler = (request, response, next) => { | ||
| response.jsx = (el, addDoctype): typeof response => { | ||
| const prefix = addDoctype ? htmlDoctype + '\n' : ''; | ||
| response.send(prefix + renderToStaticMarkup(el)); | ||
| return response; | ||
| }; | ||
|
|
||
| response.jsxForDirectory = (opt): typeof response => { | ||
| const disabledFor = opt.disableDoctypeFor ?? []; | ||
| const directory = request.directory; | ||
| if (directory === 'ctr' && opt.ctr) { | ||
| response.jsx(opt.ctr, !disabledFor.includes('ctr')); | ||
| return response; | ||
| } | ||
|
|
||
| if (directory === 'portal' && opt.portal) { | ||
| response.jsx(opt.portal, !disabledFor.includes('portal')); | ||
| return response; | ||
| } | ||
|
|
||
| if (directory === 'web' && opt.web) { | ||
| response.jsx(opt.web, !disabledFor.includes('web')); | ||
| return response; | ||
| } | ||
|
|
||
| throw new Error('Invalid directory to render JSX for'); | ||
| }; | ||
| next(); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export function InlineScript(props: { src: string }): ReactNode { | ||
| return <script dangerouslySetInnerHTML={{ __html: props.src }} />; | ||
| } | ||
|
|
||
| export function InlineStyle(props: { src: string }): ReactNode { | ||
| return <style dangerouslySetInnerHTML={{ __html: props.src }} />; | ||
| } |
22 changes: 22 additions & 0 deletions
22
apps/juxtaposition-ui/src/services/juxt-web/views/context.ts
This file contains hidden or 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 { getUserHash } from '@/util'; | ||
| import type { Response } from 'express'; | ||
| import type HashMap from 'hashmap'; | ||
|
|
||
| export type RenderContext = { | ||
| lang: Record<string, any>; | ||
| cdnUrl: string; | ||
| moderator: boolean; | ||
| pid: number; | ||
| usersMap: HashMap<number, string>; // map of PID -> screen name | ||
| }; | ||
|
|
||
| export function buildContext(res: Response): RenderContext { | ||
| const locals = res.locals; | ||
| return { | ||
| usersMap: getUserHash(), | ||
| lang: locals.lang, | ||
| cdnUrl: locals.cdnURL, | ||
| moderator: locals.moderator, | ||
| pid: locals.pid | ||
| }; | ||
| } |
65 changes: 65 additions & 0 deletions
65
apps/juxtaposition-ui/src/services/juxt-web/views/ctr/messages.tsx
This file contains hidden or 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,65 @@ | ||
| import type { ReactNode } from "react"; | ||
| import type { MessagesViewProps } from "@/services/juxt-web/views/web/messages"; | ||
| import moment from "moment"; | ||
| import cx from "classnames"; | ||
|
|
||
| export function CtrMessagesView(props: MessagesViewProps): ReactNode { | ||
| return ( | ||
| <ul | ||
| className="list-content-with-icon-column arrow-list" | ||
| id="news-list-content" | ||
| > | ||
| {props.conversations.length === 0 ? ( | ||
| <p className="no-posts-text">{props.ctx.lang.messages.coming_soon}</p> | ||
| ) : ( | ||
| props.conversations.map((convo) => { | ||
| // get the PID of the opposite user | ||
| let userObj, me; | ||
| if (convo.users[0].pid === props.ctx.pid) { | ||
| userObj = convo.users[1]; | ||
| me = convo.users[0]; | ||
| } else if (convo.users[1].pid === props.ctx.pid) { | ||
| userObj = convo.users[0]; | ||
| me = convo.users[1]; | ||
| } | ||
| return ( | ||
| <li> | ||
| <a | ||
| href={`/users/${userObj.pid}`} | ||
| data-pjax="#body" | ||
| className={cx("icon-container", { | ||
| verified: userObj.official, | ||
| })} | ||
| > | ||
| <img | ||
| src={`${props.ctx.cdnUrl}/mii/${userObj.pid}/normal_face.png`} | ||
| className="icon" | ||
| /> | ||
| </a> | ||
| <a | ||
| href={`/friend_messages/${convo.id}`} | ||
| data-pjax="#body" | ||
| className="arrow-button" | ||
| ></a> | ||
| <div className="body message"> | ||
| <p> | ||
| <span className="nick-name"> | ||
| {props.ctx.usersMap.get(userObj.pid)} | ||
| </span> | ||
| <span className="id-name"> | ||
| @{props.ctx.usersMap.get(userObj.pid)} | ||
| </span> | ||
| <span> {convo.message_preview}</span> | ||
| <span className="timestamp"> | ||
| {" "} | ||
| {moment(convo.last_updated).fromNow()} | ||
| </span> | ||
| </p> | ||
| </div> | ||
| </li> | ||
| ); | ||
| }) | ||
| )} | ||
| </ul> | ||
| ); | ||
| } |
77 changes: 77 additions & 0 deletions
77
apps/juxtaposition-ui/src/services/juxt-web/views/portal/messages.tsx
This file contains hidden or 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,77 @@ | ||
| import cx from "classnames"; | ||
| import { PortalNavBar } from "@/services/juxt-web/views/portal/navbar"; | ||
| import { | ||
| PortalPageBody, | ||
| PortalRoot, | ||
| } from "@/services/juxt-web/views/portal/root"; | ||
| import moment from "moment"; | ||
| import type { ReactNode } from "react"; | ||
| import type { MessagesViewProps } from "@/services/juxt-web/views/web/messages"; | ||
|
|
||
| export function PortalMessagesView(props: MessagesViewProps): ReactNode { | ||
| return ( | ||
| <PortalRoot title={props.ctx.lang.global.messages}> | ||
| <PortalNavBar ctx={props.ctx} selection={3} /> | ||
| <PortalPageBody> | ||
mrjvs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <header id="header"> | ||
| <h1 id="page-title">{props.ctx.lang.global.messages}</h1> | ||
| </header> | ||
| <div className="body-content" id="messages-list"> | ||
| <ul className="list-content-with-icon-and-text arrow-list"> | ||
| {props.conversations.length === 0 ? ( | ||
| <p className="no-posts-text"> | ||
| {props.ctx.lang.messages.coming_soon} | ||
| </p> | ||
| ) : ( | ||
| props.conversations.map((convo) => { | ||
| let userObj, me; | ||
| if (convo.users[0].pid === props.ctx.pid) { | ||
| userObj = convo.users[1]; | ||
| me = convo.users[0]; | ||
| } else if (convo.users[1].pid === props.ctx.pid) { | ||
| userObj = convo.users[0]; | ||
| me = convo.users[1]; | ||
| } | ||
| return ( | ||
| <li> | ||
| <a | ||
| href={`/users/show?pid=${userObj.pid}`} | ||
| data-pjax="#body" | ||
| className="icon-container trigger" | ||
| > | ||
| <img | ||
| src={`${props.ctx.cdnUrl}/mii/${userObj.pid}/normal_face.png`} | ||
| className={cx("icon", { | ||
| verified: userObj.official, | ||
| })} | ||
| /> | ||
mrjvs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </a> | ||
| <a | ||
| href={`/friend_messages/${convo.id}`} | ||
| data-pjax="#body" | ||
| className="arrow-button" | ||
| ></a> | ||
| <div className="body"> | ||
| <p className="title"> | ||
| <span className="nick-name"> | ||
| {props.ctx.usersMap.get(userObj.pid)} | ||
| </span> | ||
| <span className="id-name"> | ||
| @{props.ctx.usersMap.get(userObj.pid)} | ||
| </span> | ||
| </p> | ||
| <span className="timestamp"> | ||
| {moment(convo.last_updated).fromNow()} | ||
| </span> | ||
| <p className="text">{convo.message_preview}</p> | ||
| </div> | ||
| </li> | ||
| ); | ||
| }) | ||
| )} | ||
| </ul> | ||
| </div> | ||
| </PortalPageBody> | ||
| </PortalRoot> | ||
| ); | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
apps/juxtaposition-ui/src/services/juxt-web/views/portal/navbar.tsx
This file contains hidden or 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,72 @@ | ||
| import type { RenderContext } from "@/services/juxt-web/views/context"; | ||
| import type { ReactNode } from "react"; | ||
|
|
||
| export type NavBarProps = { | ||
| ctx: RenderContext; | ||
| selection: number; | ||
| }; | ||
|
|
||
| export function PortalNavBar(props: NavBarProps): ReactNode { | ||
| const selectedClasses = (id: number) => | ||
| id === props.selection ? "selected" : ""; | ||
|
|
||
| return ( | ||
| <menu id="nav-menu"> | ||
| <li id="nav-menu-me" data-tab="me" className={selectedClasses(0)}> | ||
| <a href="/users/me" data-pjax="#body" data-sound="SE_WAVE_MENU"> | ||
| <span className="mii-icon"> | ||
| <img | ||
| src={`${props.ctx.cdnUrl}/mii/${props.ctx.pid}/normal_face.png`} | ||
| alt="User Page" | ||
| /> | ||
| </span> | ||
| <span>{props.ctx.lang.global.user_page}</span> | ||
| </a> | ||
| </li> | ||
| <li id="nav-menu-feed" data-tab="feed" className={selectedClasses(1)}> | ||
| <a href="/feed" data-pjax="#body" data-sound="SE_WAVE_MENU"> | ||
| {props.ctx.lang.global.activity_feed} | ||
| </a> | ||
| </li> | ||
| <li | ||
| id="nav-menu-community" | ||
| data-tab="titles" | ||
| className={selectedClasses(2)} | ||
| > | ||
| <a href="/titles" data-pjax="#body" data-sound="SE_WAVE_MENU"> | ||
| {props.ctx.lang.global.communities} | ||
| </a> | ||
| </li> | ||
| <li | ||
| id="nav-menu-message" | ||
| data-tab="message" | ||
| className={selectedClasses(3)} | ||
| > | ||
| <a href="/friend_messages" data-pjax="#body" data-sound="SE_WAVE_MENU"> | ||
| {props.ctx.lang.global.messages} | ||
| <span id="message-badge" className="badge"> | ||
| 0 | ||
| </span> | ||
| </a> | ||
| </li> | ||
| <li id="nav-menu-news" data-tab="news" className={selectedClasses(4)}> | ||
| <a href="/news/my_news" data-pjax="#body" data-sound="SE_WAVE_MENU"> | ||
| {props.ctx.lang.global.notifications} | ||
| <span id="news-badge" className="badge"> | ||
| 0 | ||
| </span> | ||
| </a> | ||
| </li> | ||
| <li id="nav-menu-exit" onclick="exit()"> | ||
| <a role="button" data-sound="SE_WAVE_EXIT"> | ||
| {props.ctx.lang.global.close} | ||
| </a> | ||
| </li> | ||
| <li id="nav-menu-back" className="none" onclick="back()"> | ||
| <a role="button" className="accesskey-B" data-sound="SE_WAVE_BACK"> | ||
| {props.ctx.lang.global.go_back} | ||
| </a> | ||
| </li> | ||
| </menu> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.