-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into fix-issue-6241
- Loading branch information
Showing
59 changed files
with
1,375 additions
and
811 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { usePath } from "raviger"; | ||
|
||
/** | ||
* Returns the slug from the current path. | ||
* @param prefix The prefix of the slug. | ||
* @returns The slug. | ||
* @example | ||
* // Current path: /consultation/94b9a | ||
* const consultation = useSlug("consultation"); // consultation = "94b9a" | ||
*/ | ||
export default function useSlug(prefix: string) { | ||
const path = usePath() ?? ""; | ||
return findSlug(path.split("/"), prefix); | ||
} | ||
|
||
/** | ||
* Returns the slugs from the current path. | ||
* @param prefix The prefixes of the slug. | ||
* @returns The slugs | ||
* @example | ||
* // Current path: /facility/5b0a/consultation/94b9a | ||
* const [facility, consultation] = useSlug("facility", "consultation"); | ||
* // facility = "5b0a" | ||
* // consultation = "94b9a" | ||
*/ | ||
export const useSlugs = (...prefix: string[]) => { | ||
const path = usePath() ?? ""; | ||
return prefix.map((p) => findSlug(path.split("/"), p)); | ||
}; | ||
|
||
const findSlug = (segments: string[], prefix: string) => { | ||
const index = segments.findIndex((segment) => segment === prefix); | ||
if (index === -1) { | ||
throw new Error( | ||
`Prefix "${prefix}" not found in path "${segments.join("/")}"` | ||
); | ||
} | ||
|
||
const slug = segments[index + 1]; | ||
if (!slug) { | ||
throw new Error(`Slug not found in path "${segments.join("/")}"`); | ||
} | ||
|
||
return slug; | ||
}; |
Oops, something went wrong.