-
-
Notifications
You must be signed in to change notification settings - Fork 448
ui-next: page registration #1159
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
renbaoshuo
wants to merge
1
commit into
hydro-dev:master
Choose a base branch
from
renbaoshuo:ui-next
base: master
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,37 @@ | ||
| import { Link } from './components/link'; | ||
| import { Suspense, useMemo, useSyncExternalStore } from 'react'; | ||
| import { usePageData } from './context/page-data'; | ||
| import { defineSlot } from './registry'; | ||
| import { defineSlot, SlotName } from './registry'; | ||
| import { SlotErrorBoundary } from './registry/error-boundary'; | ||
| import { store } from './registry/store'; | ||
|
|
||
| const AppInner = defineSlot('page:app', () => { | ||
| const data = usePageData(); | ||
| const App = defineSlot('app:root', () => { | ||
| const { name } = usePageData(); | ||
| const slotName: SlotName = `page:${name}`; | ||
|
|
||
| return ( | ||
| <> | ||
| <div>app, page:{data.name}</div> | ||
| const [subscribe, getSnapshot] = useMemo(() => [ | ||
| (cb: () => void) => store.subscribe(slotName, cb), | ||
| () => store.getVersion(slotName), | ||
| ], [slotName]); | ||
|
|
||
| useSyncExternalStore(subscribe, getSnapshot); | ||
|
|
||
| const Comp = store.getDefault(slotName); | ||
|
|
||
| if (!Comp) { | ||
| return ( | ||
| <div> | ||
| <Link to="homepage">homepage</Link> <Link to="problem_main">problem_main</Link> | ||
| Page not found: <code>{name}</code> | ||
| </div> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <SlotErrorBoundary slotName={slotName} label="renderer"> | ||
| <Suspense fallback={<div>Loading...</div>}> | ||
| <Comp /> | ||
| </Suspense> | ||
| </SlotErrorBoundary> | ||
| ); | ||
| }); | ||
|
|
||
| export default AppInner; | ||
| export default App; |
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,5 @@ | ||
| import { defineSlot } from '../registry'; | ||
|
|
||
| const Layout = defineSlot('app:layout', ({ children }: React.PropsWithChildren) => <>{children}</>); | ||
|
|
||
| export default Layout; |
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,19 @@ | ||
| import { Link } from '../components/link'; | ||
|
|
||
| export default function Homepage() { | ||
| return ( | ||
| <div> | ||
| <div>homepage</div> | ||
| <Link to="problem_main">problem_main</Link> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export function Layout({ children }: React.PropsWithChildren) { | ||
| return ( | ||
| <div> | ||
| {children} | ||
| <div>layout test</div> | ||
| </div> | ||
| ); | ||
| } |
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,4 @@ | ||
| import { registerPage } from '../registry/page'; | ||
|
|
||
| registerPage('homepage', () => import('./homepage')); | ||
| registerPage('problem_main', () => import('./problem_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Link } from '../components/link'; | ||
|
|
||
| export default function ProblemMain() { | ||
| return ( | ||
| <div> | ||
| <div>problem_main</div> | ||
| <Link to="homepage">homepage</Link> | ||
| </div> | ||
| ); | ||
| } |
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,28 @@ | ||
| import React, { lazy } from 'react'; | ||
| import Layout from '../components/layout'; | ||
| import { store } from './store'; | ||
|
|
||
| interface PageModule<P = any> { | ||
| default: React.ComponentType<P>; | ||
| Layout?: React.ComponentType<React.PropsWithChildren>; | ||
| } | ||
|
|
||
| type PageLoader<P = any> = () => Promise<PageModule<P>>; | ||
|
|
||
| export function registerPage<P = any>(name: string, loader: PageLoader<P>) { | ||
| store.setDefault(`page:${name}`, lazy(() => | ||
| loader().then((mod) => { | ||
| const PageComp = mod.default; | ||
| const LayoutComp = mod.Layout || Layout; | ||
| return { | ||
| default(props: React.PropsWithChildren<P>) { | ||
| return ( | ||
| <LayoutComp> | ||
| <PageComp {...props} /> | ||
| </LayoutComp> | ||
| ); | ||
| }, | ||
| }; | ||
| }), | ||
| )); | ||
| } |
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
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.
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.
Make generated plugin
namedeterministic and safely escaped.On Lines 52-55 and Lines 128-131,
{ name: '...', ...plugin${i} }letsplugin${i}.nameoverwrite the addon-derived name, and raw string interpolation can break generated JS for quoted names.💡 Suggested patch
Also applies to: 128-131
🤖 Prompt for AI Agents
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.
This expected behaviour.
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.
@renbaoshuo, understood — the placement ofnamebefore the plugin spread is intentional so that individual plugins can override the addon-derived name. I'll note this for future reviews.( `∪。∪。) ~~
✏️ Learnings added