Skip to content

Commit

Permalink
Add HashRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
riverar committed Feb 22, 2024
1 parent 9e03c57 commit 45cef03
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
39 changes: 39 additions & 0 deletions web/features/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"gh-pages": "^6.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.1",
"react-scripts": "^5.0.1",
"typescript": "^4.9.5"
},
Expand Down
9 changes: 7 additions & 2 deletions web/features/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import Path from './components/path';
import FeatureList from './components/featurelist';
import { IWorkerMessage, IInitializeMessage, IInitializeResultMessage, ISearchMessage, ISearchResultMessage } from './worker/worker';
import { useNavigate, useParams } from 'react-router-dom';

const useStyles = makeStyles({
root: {
Expand Down Expand Up @@ -121,8 +122,9 @@ function App() {
const [worker, setWorker] = React.useState<Worker | null>(null);
const [query, setQuery] = React.useState('');

const params = useParams();
const branches = process.env.REACT_APP_BRANCHES!.split(',');
const defaultBranch = branches[0];
const defaultBranch = branches.includes(params.branch!) ? params.branch : branches[0];
const [branch, setBranch] = React.useState(defaultBranch);

const styles = useStyles();
Expand Down Expand Up @@ -189,11 +191,14 @@ function App() {
setResultsTruncated(false);
};

const navigate = useNavigate();
const onBranchSelected = (
ev: SelectionEvents,
data: OptionOnSelectData
) => {
setBranch(data.optionValue ?? defaultBranch);
const branch = data.optionValue ?? defaultBranch;
setBranch(branch);
navigate(`/${branch}`, { replace: true });
};

return (
Expand Down
9 changes: 8 additions & 1 deletion web/features/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ReactDOM from 'react-dom/client';
import { RouterProvider, createHashRouter } from 'react-router-dom';
import { FluentProvider, createLightTheme } from '@fluentui/react-components';
import '@fontsource/fira-sans';
import App from './app';
Expand Down Expand Up @@ -32,14 +33,20 @@ const lightTheme = {
...coreTheme,
};

const router = createHashRouter([
{
path: '/:branch?',
element: <App />
}
]);
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
// https://github.com/microsoft/fluentui/issues/30450
//<StrictMode>
<FluentProvider theme={lightTheme}>
<App />
<RouterProvider router={router} />
</FluentProvider>
//</StrictMode>
);

0 comments on commit 45cef03

Please sign in to comment.