forked from solidjs/solid-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute-metadata-helper.ts
More file actions
67 lines (54 loc) · 1.49 KB
/
route-metadata-helper.ts
File metadata and controls
67 lines (54 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useLocation } from "@solidjs/router";
import { SUPPORTED_LOCALES } from "~/i18n/config";
export enum Project {
Core = "",
Router = "/solid-router",
Start = "/solid-start",
Meta = "/solid-meta",
}
interface CurrentRouteMetaData {
project: Project | null;
locale: string;
isProjectRoot: boolean;
}
export const useCurrentRouteMetaData = (): CurrentRouteMetaData => {
let currentPath = useLocation().pathname;
// Trim trailing slash
currentPath = currentPath.endsWith("/")
? currentPath.slice(0, -1)
: currentPath;
const pathParts = currentPath.split("/").filter(Boolean);
const projectOrLocale: string = pathParts[0];
const returnObject: CurrentRouteMetaData = {
isProjectRoot: true,
locale: "",
project: null,
};
if (SUPPORTED_LOCALES.includes(projectOrLocale)) {
if (pathParts.length > 2) {
returnObject.isProjectRoot = false;
}
returnObject.locale = projectOrLocale;
checkPathBeyondLocale(pathParts[1] ?? "");
} else {
if (pathParts.length > 1) {
returnObject.isProjectRoot = false;
}
checkPathBeyondLocale(pathParts[0] ?? "");
}
function isInProjectEnum(projectPath: string): boolean {
return Object.values(Project).includes(projectPath as Project);
}
function checkPathBeyondLocale(path: string) {
if (path.length > 0) {
path = "/" + path;
}
if (isInProjectEnum(path)) {
returnObject.project = path as Project;
} else {
returnObject.isProjectRoot = false;
returnObject.project = "" as Project;
}
}
return returnObject;
};