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:
parent
a8e78c29d7
commit
a4d452c874
2 changed files with 14 additions and 10 deletions
|
@ -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 * as React from "react"
|
||||
import {useSophonPath} from "../../hooks/useSophonPath"
|
||||
|
@ -11,7 +12,7 @@ export function BreadcrumbsBox(): JSX.Element {
|
|||
const location = useSophonPath()
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Box builtinColor={location.valid ? undefined : "red"}>
|
||||
<BreadcrumbLink
|
||||
href={"/"}
|
||||
icon={faServer}
|
||||
|
@ -67,6 +68,10 @@ export function BreadcrumbsBox(): JSX.Element {
|
|||
) : null}
|
||||
</>
|
||||
) : null}
|
||||
{location.valid ? null : <>
|
||||
<BreadcrumbSeparator/>
|
||||
<FontAwesomeIcon icon={faExclamationCircle}/> Invalid path!
|
||||
</>}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -58,17 +58,12 @@ interface ParsePathSegmentConfig {
|
|||
|
||||
|
||||
function parsePathSegment({path, parsed, regex, key, next}: ParsePathSegmentConfig): ParsedPath {
|
||||
// If the path is empty, return
|
||||
if(!path) {
|
||||
return parsed
|
||||
}
|
||||
|
||||
// Try matching the 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) {
|
||||
parsed.valid = Boolean(path)
|
||||
parsed.valid = path === "/"
|
||||
return parsed
|
||||
}
|
||||
|
||||
|
@ -95,7 +90,7 @@ function parsePathSegment({path, parsed, regex, key, next}: ParsePathSegmentConf
|
|||
* @param path - The path to split.
|
||||
*/
|
||||
export function parsePath(path: string): ParsedPath {
|
||||
return parsePathSegment({
|
||||
const result = parsePathSegment({
|
||||
path,
|
||||
parsed: {count: 0, valid: true},
|
||||
regex: INSTANCE_REGEX,
|
||||
|
@ -142,4 +137,8 @@ export function parsePath(path: string): ParsedPath {
|
|||
},
|
||||
]
|
||||
})
|
||||
|
||||
console.debug("[ParsePath] Parsed", result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue