From 8f81318ecfe44236001388fa811f7a59b7469b28 Mon Sep 17 00:00:00 2001 From: Emmanuel Pelletier Date: Fri, 26 Jul 2024 00:36:10 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20trying=20out=20c?= =?UTF-8?q?leaner=20way=20to=20handle=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit it was a bit cumbersome to navigate by paths accross the app as if one path changes a tiny bit we have to make sure we updated everything everywhere and it's kind of error-prone and cumbersome to build paths by hand. now we have a routes file that describes everything: what is the path to each route, and how to navigate to them. We use a new navigateTo helper that helps us. It could be better typed but i'm a newbie. --- src/frontend/src/App.tsx | 16 +++++------ .../home/components/JoinMeetingDialog.tsx | 5 ++-- src/frontend/src/features/home/index.ts | 1 - .../home/navigation/navigateToHome.ts | 5 ---- .../src/features/home/routes/Home.tsx | 9 +++++-- .../features/rooms/components/Conference.tsx | 4 +-- src/frontend/src/features/rooms/index.ts | 5 ++-- .../rooms/navigation/navigateToNewRoom.ts | 6 ----- .../rooms/navigation/navigateToRoom.ts | 5 ---- .../src/features/rooms/utils/isRoomValid.ts | 3 +-- src/frontend/src/layout/Header.tsx | 2 +- src/frontend/src/navigation/getRouteByName.ts | 9 +++++++ src/frontend/src/navigation/navigateTo.ts | 21 +++++++++++++++ .../src/navigation/useMatchesRoute.ts | 7 +++++ src/frontend/src/routes.ts | 27 +++++++++++++++++++ src/frontend/src/routes/NotFound.tsx | 5 ---- src/frontend/src/utils/useMatchesRoute.ts | 17 ------------ 17 files changed, 88 insertions(+), 59 deletions(-) delete mode 100644 src/frontend/src/features/home/navigation/navigateToHome.ts delete mode 100644 src/frontend/src/features/rooms/navigation/navigateToNewRoom.ts delete mode 100644 src/frontend/src/features/rooms/navigation/navigateToRoom.ts create mode 100644 src/frontend/src/navigation/getRouteByName.ts create mode 100644 src/frontend/src/navigation/navigateTo.ts create mode 100644 src/frontend/src/navigation/useMatchesRoute.ts create mode 100644 src/frontend/src/routes.ts delete mode 100644 src/frontend/src/routes/NotFound.tsx delete mode 100644 src/frontend/src/utils/useMatchesRoute.ts diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 45b14798..9a055d34 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -5,12 +5,11 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { useLang } from 'hoofd' -import { Route, Switch } from 'wouter' -import { HomeRoute } from '@/features/home' -import { RoomRoute, roomRouteRegex } from '@/features/rooms' -import { NotFound } from './routes/NotFound' -import './i18n/init' +import { Switch, Route } from 'wouter' +import { NotFoundScreen } from './layout/NotFoundScreen' import { RenderIfUserFetched } from './features/auth' +import { routes } from './routes' +import './i18n/init' const queryClient = new QueryClient() @@ -22,9 +21,10 @@ function App() { - - - + {Object.entries(routes).map(([, route], i) => ( + + ))} + diff --git a/src/frontend/src/features/home/components/JoinMeetingDialog.tsx b/src/frontend/src/features/home/components/JoinMeetingDialog.tsx index 31a5f01a..fc2f5d45 100644 --- a/src/frontend/src/features/home/components/JoinMeetingDialog.tsx +++ b/src/frontend/src/features/home/components/JoinMeetingDialog.tsx @@ -1,6 +1,7 @@ import { useTranslation } from 'react-i18next' import { Field, Ul, H, P, Form, Dialog } from '@/primitives' -import { isRoomValid, navigateToRoom } from '@/features/rooms' +import { navigateTo } from '@/navigation/navigateTo' +import { isRoomValid } from '@/features/rooms' export const JoinMeetingDialog = () => { const { t } = useTranslation('home') @@ -8,7 +9,7 @@ export const JoinMeetingDialog = () => {
{ - navigateToRoom((data.roomId as string).trim()) + navigateTo('room', data.roomId as string) }} submitLabel={t('joinInputSubmit')} > diff --git a/src/frontend/src/features/home/index.ts b/src/frontend/src/features/home/index.ts index 0d677a0d..bda6a40d 100644 --- a/src/frontend/src/features/home/index.ts +++ b/src/frontend/src/features/home/index.ts @@ -1,2 +1 @@ -export { navigateToHome } from './navigation/navigateToHome' export { Home as HomeRoute } from './routes/Home' diff --git a/src/frontend/src/features/home/navigation/navigateToHome.ts b/src/frontend/src/features/home/navigation/navigateToHome.ts deleted file mode 100644 index 42f84d16..00000000 --- a/src/frontend/src/features/home/navigation/navigateToHome.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { navigate } from 'wouter/use-browser-location' - -export const navigateToHome = () => { - navigate(`/`) -} diff --git a/src/frontend/src/features/home/routes/Home.tsx b/src/frontend/src/features/home/routes/Home.tsx index f4f9274d..86ced491 100644 --- a/src/frontend/src/features/home/routes/Home.tsx +++ b/src/frontend/src/features/home/routes/Home.tsx @@ -2,8 +2,9 @@ import { useTranslation } from 'react-i18next' import { DialogTrigger } from 'react-aria-components' import { Button, Div, Text, VerticallyOffCenter } from '@/primitives' import { HStack } from '@/styled-system/jsx' +import { navigateTo } from '@/navigation/navigateTo' +import { generateRoomId } from '@/features/rooms' import { authUrl, useUser } from '@/features/auth' -import { navigateToNewRoom } from '@/features/rooms' import { Screen } from '@/layout/Screen' import { JoinMeetingDialog } from '../components/JoinMeetingDialog' @@ -28,7 +29,11 @@ export const Home = () => {