Skip to content

Commit 4387ca6

Browse files
authored
chore suppport folders (#12)
* chore: Support folders in dev pages * feat: Parse any path into Page component * fix: Remove unnecessary comma in pattern
1 parent 8ebcd87 commit 4387ca6

File tree

4 files changed

+48
-40
lines changed

4 files changed

+48
-40
lines changed

pages/app.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

pages/app/index.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { HashRouter, Link, Route, Routes, useLocation } from "react-router-dom";
4+
import Page from "./page";
5+
import { pages } from "../pages";
6+
7+
export default function App() {
8+
return (
9+
<HashRouter>
10+
<Routes>
11+
<Route path="/" element={<Start />} />
12+
<Route path="/*" element={<PageWithFallback />} />
13+
</Routes>
14+
</HashRouter>
15+
);
16+
}
17+
18+
const Start = () => (
19+
<>
20+
<h1>Pages</h1>
21+
<main>
22+
<Index />
23+
</main>
24+
</>
25+
);
26+
27+
const Index = () => (
28+
<ul className="list">
29+
{pages.map((page) => (
30+
<li key={page}>
31+
<Link to={`${page}`}>{page}</Link>
32+
</li>
33+
))}
34+
</ul>
35+
);
36+
37+
const PageWithFallback = () => {
38+
const { pathname: page } = useLocation();
39+
40+
if (!page || !page.includes(page)) {
41+
return <span>Not Found</span>;
42+
}
43+
44+
return <Page pageId={page} />;
45+
};

pages/page.tsx renamed to pages/app/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
43
import { Suspense } from "react";
5-
import { pagesMap } from "./pages";
4+
import { pagesMap } from "../pages";
65

76
export interface PageProps {
87
pageId: string;

pages/pages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import React from "react";
44

5-
const pagesRaw = import.meta.glob("./*.pages.tsx");
6-
const pageIdRegex = /.*\/([\w,-]+)\.pages\.tsx/;
5+
const pagesRaw = import.meta.glob("./**/*.page.tsx");
6+
const pageIdRegex = /([\w-/]+)\.page\.tsx/;
77
const getPage = (path: string) => path.match(pageIdRegex)?.[1];
88

99
export const pages = Object.keys(pagesRaw).map(getPage);

0 commit comments

Comments
 (0)