1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 06:44:21 +00:00

🔧 Handle invalid paths

This commit is contained in:
Steffo 2021-11-08 18:50:44 +01:00 committed by Stefano Pigozzi
parent a8e78c29d7
commit a4d452c874
2 changed files with 14 additions and 10 deletions

View file

@ -1,4 +1,5 @@
import {faBook, faProjectDiagram, faServer, faUniversity, faUser, faUsers} from "@fortawesome/free-solid-svg-icons" import {faBook, faExclamationCircle, faProjectDiagram, faServer, faUniversity, faUser, faUsers} from "@fortawesome/free-solid-svg-icons"
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"
import {Box} from "@steffo/bluelib-react" import {Box} from "@steffo/bluelib-react"
import * as React from "react" import * as React from "react"
import {useSophonPath} from "../../hooks/useSophonPath" import {useSophonPath} from "../../hooks/useSophonPath"
@ -11,7 +12,7 @@ export function BreadcrumbsBox(): JSX.Element {
const location = useSophonPath() const location = useSophonPath()
return ( return (
<Box> <Box builtinColor={location.valid ? undefined : "red"}>
<BreadcrumbLink <BreadcrumbLink
href={"/"} href={"/"}
icon={faServer} icon={faServer}
@ -67,6 +68,10 @@ export function BreadcrumbsBox(): JSX.Element {
) : null} ) : null}
</> </>
) : null} ) : null}
{location.valid ? null : <>
<BreadcrumbSeparator/>
<FontAwesomeIcon icon={faExclamationCircle}/>&nbsp;Invalid path!
</>}
</Box> </Box>
) )
} }

View file

@ -58,17 +58,12 @@ interface ParsePathSegmentConfig {
function parsePathSegment({path, parsed, regex, key, next}: ParsePathSegmentConfig): ParsedPath { function parsePathSegment({path, parsed, regex, key, next}: ParsePathSegmentConfig): ParsedPath {
// If the path is empty, return
if(!path) {
return parsed
}
// Try matching the regex // Try matching the regex
const match = path.match(regex) const match = path.match(regex)
// If the match fails, it means the path is invalid // If the match fails, it means the matching is over
if(!match || !match.groups) { if(!match || !match.groups) {
parsed.valid = Boolean(path) parsed.valid = path === "/"
return parsed return parsed
} }
@ -95,7 +90,7 @@ function parsePathSegment({path, parsed, regex, key, next}: ParsePathSegmentConf
* @param path - The path to split. * @param path - The path to split.
*/ */
export function parsePath(path: string): ParsedPath { export function parsePath(path: string): ParsedPath {
return parsePathSegment({ const result = parsePathSegment({
path, path,
parsed: {count: 0, valid: true}, parsed: {count: 0, valid: true},
regex: INSTANCE_REGEX, regex: INSTANCE_REGEX,
@ -142,4 +137,8 @@ export function parsePath(path: string): ParsedPath {
}, },
] ]
}) })
console.debug("[ParsePath] Parsed", result)
return result
} }