♻️(frontend) tiny cleanup of the room regex related stuff

since this is used for the routing *and* for common validation, split a
few things
This commit is contained in:
Emmanuel Pelletier
2024-07-23 19:41:10 +02:00
parent 2e081e5e1e
commit 82208c220a
4 changed files with 8 additions and 5 deletions

View File

@@ -7,7 +7,7 @@ import { useTranslation } from 'react-i18next'
import { useLang } from 'hoofd'
import { Route, Switch } from 'wouter'
import { HomeRoute } from '@/features/home'
import { RoomRoute, roomIdRegex } from '@/features/rooms'
import { RoomRoute, roomRouteRegex } from '@/features/rooms'
import { NotFound } from './routes/NotFound'
import './i18n/init'
import { RenderIfUserFetched } from './features/auth'
@@ -23,7 +23,7 @@ function App() {
<RenderIfUserFetched>
<Switch>
<Route path="/" component={HomeRoute} />
<Route path={roomIdRegex} component={RoomRoute} />
<Route path={roomRouteRegex} component={RoomRoute} />
<Route component={NotFound} />
</Switch>
</RenderIfUserFetched>

View File

@@ -1,3 +1,3 @@
export { navigateToNewRoom } from './navigation/navigateToNewRoom'
export { Room as RoomRoute } from './routes/Room'
export { roomIdRegex } from './utils/generateRoomId'
export { roomRouteRegex, isRoomValid } from './utils/isRoomValid'

View File

@@ -12,5 +12,3 @@ const generateSegment = (length: number): string =>
// Generates a unique room identifier following the Google Meet format
export const generateRoomId = () =>
[generateSegment(3), generateSegment(4), generateSegment(3)].join('-')
export const roomIdRegex = /^[/](?<roomId>[a-z]{3}-[a-z]{4}-[a-z]{3})$/

View File

@@ -0,0 +1,5 @@
const roomIdPattern = '[a-z]{3}-[a-z]{4}-[a-z]{3}'
export const roomRouteRegex = new RegExp(`^[/](?<roomId>${roomIdPattern})$`)
export const isRoomValid = (roomId: string) =>
new RegExp(`^${roomIdPattern}$`).test(roomId)