Skip to content

Commit

Permalink
feat: progress for new createPages
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler committed Oct 19, 2024
1 parent c5ca886 commit 6764c3d
Showing 1 changed file with 83 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
GetSlugs,
PropsForPages,
} from './create-pages-utils/inferred-path-types.js';
import { Children, Slot } from '../minimal/client.js';

const hasPathSpecPrefix = (prefix: PathSpec, path: PathSpec) => {
for (let i = 0; i < prefix.length; i++) {
Expand Down Expand Up @@ -469,8 +470,10 @@ export const new_createPages = <
) => {
let configured = false;

const routeMap = new Map<string, Record<string, ReactNode>>();

// TODO I think there's room for improvement to refactor these structures
const staticPathSet = new Set<[string, PathSpec]>();
const fixedPathSet = new Set<[string, PathSpec]>();
const dynamicPagePathMap = new Map<
string,
[PathSpec, FunctionComponent<any>]
Expand Down Expand Up @@ -504,6 +507,28 @@ export const new_createPages = <
if (configured) {
throw new Error('createPage no longer available');
}

routeMap.set(page.path, {

Check failure on line 511 in packages/waku/src/router/create-pages.tsx

View workflow job for this annotation

GitHub Actions / test

Argument of type '{ [x: string]: string | number | bigint | boolean | Iterable<ReactNode> | Promise<AwaitedReactNode> | FunctionComponent<{ [K in keyof (Omit<RouteProps<ReplaceAll<Path, `[${string}]`, string>>, "hash"> & SlugTypes<Path>)]: (Omit<RouteProps<ReplaceAll<Path, `[${string}]`, string>>, "hash"> & SlugTypes<Path>)[K]; }> | JSX.Element | null | undefined; root: string | number | bigint | boolean | Iterable<ReactNode> | Promise<AwaitedReactNode> | JSX.Element | null | undefined; }' is not assignable to parameter of type 'Record<string, ReactNode>'.
[`route:${page.path}`]: (
// layout adding is different at each level
// TODO loop over the path spec to add layout slots
<Slot id="root">
<Slot id={`layout:${page.path}`}>
<Slot id={`page:${page.path}`} />
</Slot>
</Slot>
),
[`page:${page.path}`]: page.component,
root: rootItem ? (
rootItem.component({ children: <Children /> })
) : (
<DefaultRoot>
<Children />
</DefaultRoot>
),
// TODO add layout for path
});

const pathSpec = parsePathWithSlug(page.path);
if (page.unstable_disableSSR) {
noSsrSet.add(pathSpec);
Expand All @@ -522,7 +547,7 @@ export const new_createPages = <
return { numSlugs, numWildcards };
})();
if (page.render === 'static' && numSlugs === 0) {
staticPathSet.add([page.path, pathSpec]);
fixedPathSet.add([page.path, pathSpec]);
const id = joinPath(page.path, 'page').replace(/^\//, '');
registerStaticComponent(id, page.component);
} else if (
Expand Down Expand Up @@ -557,7 +582,7 @@ export const new_createPages = <
break;
}
});
staticPathSet.add([
fixedPathSet.add([
page.path,
pathItems.map((name) => ({ type: 'literal', name })),
]);
Expand Down Expand Up @@ -633,7 +658,7 @@ export const new_createPages = <
components: Record<string, { isStatic: boolean }>;
noSsr: boolean;
}[] = [];
for (const [path, pathSpec] of staticPathSet) {
for (const [path, pathSpec] of fixedPathSet) {
const noSsr = noSsrSet.has(pathSpec);
const isStatic = (() => {
for (const [_, [layoutPathSpec]] of dynamicLayoutPathMap) {
Expand Down Expand Up @@ -671,9 +696,10 @@ export const new_createPages = <
}
return paths;
},
renderRoute: async (path, options) => {
renderRoute: async (path, _options) => {
await configure();

// TODO handle skip
return routeMap.get(path) ?? null;
// return processSkip({
// 'route:/bar': (
// <Slot id="root">
Expand All @@ -695,58 +721,57 @@ export const new_createPages = <
// 'page:/bar': <BarPage />,
// });

if (id === 'root') {
if (options.skip?.includes('root')) {
return null;
}
return rootItem?.component ?? DefaultRoot;
}
const staticComponent = staticComponentMap.get(id);
if (staticComponent && !options.skip?.includes(id)) {
return staticComponent;
}
for (const [_, [pathSpec, Component]] of dynamicPagePathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'page' }],
id,
);

if (mapping) {
if (Object.keys(mapping).length === 0) {
return Component;
}
const WrappedComponent = (props: Record<string, unknown>) =>
createElement(Component, { ...props, ...mapping });
// unstable_setShouldSkip();
return WrappedComponent;
}
}
for (const [_, [pathSpec, Component]] of wildcardPagePathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'page' }],
id,
);
if (mapping) {
const WrappedComponent = (props: Record<string, unknown>) =>
createElement(Component, { ...props, ...mapping });
// unstable_setShouldSkip();
return WrappedComponent;
}
}
for (const [_, [pathSpec, Component]] of dynamicLayoutPathMap) {
const mapping = getPathMapping(
[...pathSpec, { type: 'literal', name: 'layout' }],
id,
);
if (mapping) {
if (Object.keys(mapping).length) {
throw new Error('[Bug] layout should not have slugs');
}
// unstable_setShouldSkip();
return Component;
}
}
return null; // not found
// if (id === 'root') {
// if (options.skip?.includes('root')) {
// return null;
// }
// return rootItem?.component ?? DefaultRoot;
// }
// const staticComponent = staticComponentMap.get(id);
// if (staticComponent && !options.skip?.includes(id)) {
// return staticComponent;
// }
// for (const [_, [pathSpec, Component]] of dynamicPagePathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'page' }],
// id,
// );

// if (mapping) {
// if (Object.keys(mapping).length === 0) {
// return Component;
// }
// const WrappedComponent = (props: Record<string, unknown>) =>
// createElement(Component, { ...props, ...mapping });
// // unstable_setShouldSkip();
// return WrappedComponent;
// }
// }
// for (const [_, [pathSpec, Component]] of wildcardPagePathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'page' }],
// id,
// );
// if (mapping) {
// const WrappedComponent = (props: Record<string, unknown>) =>
// createElement(Component, { ...props, ...mapping });
// // unstable_setShouldSkip();
// return WrappedComponent;
// }
// }
// for (const [_, [pathSpec, Component]] of dynamicLayoutPathMap) {
// const mapping = getPathMapping(
// [...pathSpec, { type: 'literal', name: 'layout' }],
// id,
// );
// if (mapping) {
// if (Object.keys(mapping).length) {
// throw new Error('[Bug] layout should not have slugs');
// }
// // unstable_setShouldSkip();
// return Component;
// }
// }
},
});

Expand Down

0 comments on commit 6764c3d

Please sign in to comment.