-
Notifications
You must be signed in to change notification settings - Fork 6
Core 901 add portal routing handler #2732
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
Open
RoyEJohnson
wants to merge
6
commits into
main
Choose a base branch
from
core-901-add-portal-routing-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e34baa1
Links and menus generally work with portal
RoyEJohnson 6bfb2ee
Portal pages use landing page layout
RoyEJohnson d2a8fc3
Rewrite links for footer and selected page elements
RoyEJohnson 84ddb6f
Tests
RoyEJohnson 8b3162a
A flex page with landing layout is a portal
RoyEJohnson 6122918
footer JS to TSX
RoyEJohnson 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,64 @@ | ||
import React from 'react'; | ||
import usePortalContext from '~/contexts/portal'; | ||
|
||
// Making scripts work, per https://stackoverflow.com/a/47614491/392102 | ||
function activateScripts(el: HTMLElement) { | ||
const scripts: HTMLScriptElement[] = Array.from(el.querySelectorAll('script')); | ||
const processOne = (() => { | ||
const scripts: HTMLScriptElement[] = Array.from( | ||
el.querySelectorAll('script') | ||
); | ||
const processOne = () => { | ||
const s = scripts.shift(); | ||
|
||
if (!s) { | ||
return; | ||
} | ||
const newScript = document.createElement('script'); | ||
const p = (s.src) ? new Promise((resolve) => { | ||
newScript.onload = resolve; | ||
}) : Promise.resolve(); | ||
const p = s.src | ||
? new Promise((resolve) => { | ||
newScript.onload = resolve; | ||
}) | ||
: Promise.resolve(); | ||
|
||
Array.from(s.attributes) | ||
.forEach((a) => newScript.setAttribute(a.name, a.value)); | ||
Array.from(s.attributes).forEach((a) => | ||
newScript.setAttribute(a.name, a.value) | ||
); | ||
if (s.textContent) { | ||
newScript.appendChild(document.createTextNode(s.textContent)); | ||
} | ||
newScript.async = false; | ||
s.parentNode?.replaceChild(newScript, s); | ||
|
||
p.then(processOne); | ||
}); | ||
}; | ||
|
||
processOne(); | ||
} | ||
|
||
type RawHTMLArgs = {Tag?: string, html?: TrustedHTML, embed?: boolean} & React.HTMLAttributes<HTMLDivElement>; | ||
type RawHTMLArgs = { | ||
Tag?: string; | ||
html?: TrustedHTML; | ||
embed?: boolean; | ||
} & React.HTMLAttributes<HTMLDivElement>; | ||
|
||
export default function RawHTML({Tag='div', html, embed=false, ...otherProps}: RawHTMLArgs) { | ||
export default function RawHTML({ | ||
Tag = 'div', | ||
html, | ||
embed = false, | ||
...otherProps | ||
}: RawHTMLArgs) { | ||
const ref = React.useRef<HTMLElement>(); | ||
const {rewriteLinks} = usePortalContext(); | ||
|
||
React.useEffect(() => { | ||
if (embed && ref.current) { | ||
activateScripts(ref.current); | ||
} | ||
}); | ||
return ( | ||
React.createElement(Tag, {ref, dangerouslySetInnerHTML: {__html: html}, ...otherProps}) | ||
); | ||
React.useLayoutEffect(() => rewriteLinks?.(ref.current as HTMLElement), [rewriteLinks]); | ||
|
||
return React.createElement(Tag, { | ||
ref, | ||
dangerouslySetInnerHTML: {__html: html}, | ||
...otherProps | ||
}); | ||
} |
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,8 @@ | ||
.list-of-links { | ||
list-style-type: none; | ||
padding: 0; | ||
margin: 0; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.5rem; | ||
} |
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,10 @@ | ||
import React from 'react'; | ||
import './list-of-links.scss'; | ||
|
||
export default function ListOfLinks({children}: React.PropsWithChildren<object>) { | ||
return ( | ||
<ul className="list-of-links"> | ||
{React.Children.toArray(children).map((c, i) => (<li key={i}>{c}</li>))} | ||
</ul> | ||
); | ||
} |
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,21 @@ | ||
import React from 'react'; | ||
import {RoutesAlsoInPortal} from './router'; | ||
import usePortalContext from '~/contexts/portal'; | ||
import {useParams} from 'react-router-dom'; | ||
|
||
export default function PortalRouter() { | ||
// If we want to validate the portal name, that can be done here | ||
const {portal, '*': rest} = useParams(); | ||
const {setPortal} = usePortalContext(); | ||
|
||
React.useEffect(() => { | ||
// It seems that the path "/press" in particular winds up matching | ||
// the portal-router Route, so we need to ensure it's really a portal | ||
// route by verifying that there's something after the :portal param | ||
if (rest) { | ||
setPortal(portal as string); | ||
} | ||
}, [rest, portal, setPortal]); | ||
|
||
return <RoutesAlsoInPortal />; | ||
} |
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import useRouterContext, {RouterContextProvider} from './router-context'; | |
import loadable from 'react-loadable'; | ||
import LoadingPlaceholder from '~/components/loading-placeholder/loading-placeholder'; | ||
import useLayoutContext, { LayoutContextProvider } from '~/contexts/layout'; | ||
import PortalRouter from './portal-router'; | ||
import './skip-to-content.scss'; | ||
|
||
function useAnalyticsPageView() { | ||
|
@@ -28,6 +29,7 @@ const Fallback = loadable({ | |
loader: () => import('./router-helpers/fallback-to.js'), | ||
loading: () => <h1>...Loading</h1> | ||
}); | ||
|
||
const Error404 = loadable({ | ||
loader: () => import('~/pages/404/404'), | ||
loading: () => <h1>404</h1> | ||
|
@@ -56,14 +58,14 @@ function useLoading(name) { | |
} | ||
|
||
function DefaultLayout({children}) { | ||
const {portal} = useParams(); | ||
const {setLayoutParameters, layoutParameters} = useLayoutContext(); | ||
|
||
React.useEffect( | ||
() => setLayoutParameters(), | ||
[setLayoutParameters] | ||
); | ||
if (portal) { | ||
setLayoutParameters({name: 'landing', data: layoutParameters.data}); | ||
} | ||
|
||
return layoutParameters.name === 'default' ? children : null; | ||
return layoutParameters.name ? children : null; | ||
} | ||
|
||
function usePage(name) { | ||
|
@@ -133,39 +135,50 @@ function MainRoutes() { | |
<Layout> | ||
<Routes> | ||
<Route path="/" element={<ImportedPage name="home" />} /> | ||
{ | ||
FOOTER_PAGES.map( | ||
(path) => <Route path={path} key={path} element={<ImportedPage name="footer-page" />} /> | ||
) | ||
} | ||
<Route path="/errata/" element={<ImportedPage name="errata-summary" />} /> | ||
<Route path="/errata/form/" element={<ImportedPage name="errata-form" />} /> | ||
<Route path="/errata/*" element={<ImportedPage name="errata-detail" />} /> | ||
<Route path="/details/books/:title" element={<ImportedPage name="details" />} /> | ||
<Route path="/details/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/details/" element={<Navigate to="/subjects" replace />} /> | ||
<Route path="/books/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/textbooks/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/subjects/*" element={<ImportedPage name="subjects" />} /> | ||
<Route path="/k12/*" element={<ImportedPage name="k12" />} /> | ||
<Route path="/blog/*" element={<ImportedPage name="blog" />} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm wondering if there are any of these, like k12, blog, general, that we explicitly don't want to work in the portal. i guess probably they're not hurting anything by being in there |
||
<Route path="/webinars/*" element={<ImportedPage name="webinars" />} /> | ||
<Route path="/general/*" element={<ImportedPage name="general" />} /> | ||
<Route path="/confirmation/*" element={<ImportedPage name="confirmation" />} /> | ||
<Route path="/campaign/*" element={<ImportedPage name="campaign" />} /> | ||
<Route path="/press/*" element={<ImportedPage name="press" />} /> | ||
<Route | ||
path="/edtech-partner-program" | ||
element={<ImportedPage name="/openstax-ally-technology-partner-program" />} | ||
/> | ||
<Route path="/:name/" element={<TopLevelPage />} /> | ||
<Route path="/:name/*" element={<Error404 />} /> | ||
</Routes> | ||
<RoutesAlsoInPortal /> | ||
<Routes> | ||
<Route path="/:portal/*" element={<PortalRouter />} /> | ||
<Route element={<h1>Fell through</h1>} /> | ||
</Routes> | ||
</Layout> | ||
); | ||
} | ||
|
||
|
||
export function RoutesAlsoInPortal() { | ||
return ( | ||
<Routes> | ||
{ | ||
FOOTER_PAGES.map( | ||
(path) => <Route path={path} key={path} element={<ImportedPage name="footer-page" />} /> | ||
) | ||
} | ||
<Route path="/errata/" element={<ImportedPage name="errata-summary" />} /> | ||
<Route path="/errata/form/" element={<ImportedPage name="errata-form" />} /> | ||
<Route path="/errata/*" element={<ImportedPage name="errata-detail" />} /> | ||
<Route path="/details/books/:title" element={<ImportedPage name="details" />} /> | ||
<Route path="/details/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/details/" element={<Navigate to="/subjects" replace />} /> | ||
<Route path="/books/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/textbooks/:title" element={<RedirectToCanonicalDetailsPage />} /> | ||
<Route path="/subjects/*" element={<ImportedPage name="subjects" />} /> | ||
<Route path="/k12/*" element={<ImportedPage name="k12" />} /> | ||
<Route path="/blog/*" element={<ImportedPage name="blog" />} /> | ||
<Route path="/webinars/*" element={<ImportedPage name="webinars" />} /> | ||
<Route path="/general/*" element={<ImportedPage name="general" />} /> | ||
<Route path="/confirmation/*" element={<ImportedPage name="confirmation" />} /> | ||
<Route path="/campaign/*" element={<ImportedPage name="campaign" />} /> | ||
<Route path="/press/*" element={<ImportedPage name="press" />} /> | ||
<Route | ||
path="/edtech-partner-program" | ||
element={<ImportedPage name="/openstax-ally-technology-partner-program" />} | ||
/> | ||
<Route path="/:name/" element={<TopLevelPage />} /> | ||
</Routes> | ||
); | ||
} | ||
|
||
function doSkipToContent(event) { | ||
event.preventDefault(); | ||
const mainEl = document.getElementById('main'); | ||
|
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,24 @@ | ||
import React from 'react'; | ||
import buildContext from '~/components/jsx-helpers/build-context'; | ||
|
||
function useContextValue() { | ||
const [portal, setPortal] = React.useState(''); | ||
const portalPrefix = portal ? `/${portal}` : ''; | ||
const rewriteLinks = React.useCallback((container: HTMLElement) => { | ||
if (!portalPrefix) {return;} | ||
const linkNodes = container.querySelectorAll('a[href^="/"]'); | ||
|
||
for (const node of linkNodes) { | ||
const href = node.getAttribute('href'); | ||
|
||
node.setAttribute('href', `${portalPrefix}${href}`); | ||
} | ||
}, | ||
[portalPrefix]); | ||
|
||
return {portalPrefix, setPortal, rewriteLinks}; | ||
} | ||
|
||
const {useContext, ContextProvider} = buildContext({useContextValue}); | ||
|
||
export {useContext as default, ContextProvider as PortalContextProvider}; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unable to see that this did anything.