Skip to content
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

Add HashRouter to web app for deep links #2867

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 11 additions & 4 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 @@ -57,7 +58,9 @@ const useStyles = makeStyles({
},
searchContainer: {
display: 'flex',
width: '100%'
flexWrap: 'wrap',
width: '100%',
...shorthands.gap('1em')
},
searchInput: {
flexGrow: 5
Expand Down Expand Up @@ -121,8 +124,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 +193,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 Expand Up @@ -233,7 +240,7 @@ function App() {
)
}
/>
<Dropdown className={styles.searchBranch} value={branch} onOptionSelect={onBranchSelected}>
<Dropdown size='large' className={styles.searchBranch} value={branch} onOptionSelect={onBranchSelected}>
{
branches.map((branch) => (
<Option key={branch}>
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>
);