-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.tsx
57 lines (46 loc) · 1.92 KB
/
utils.tsx
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
import {stringify} from "qs"
import {MenuItem, NodeInterface} from "@/lib/gql/__generated__/drupal.d"
export const buildUrl = (path: string, params?: string | Record<string, string> | URLSearchParams): URL => {
const url = new URL(path.charAt(0) === "/" ? `${process.env.NEXT_PUBLIC_DRUPAL_BASE_URL}${path}` : path)
if (params) {
// Use instead URLSearchParams for nested params.
url.search = stringify(params)
}
return url
}
export const buildHeaders = (headers?: HeadersInit, isPreviewMode?: boolean): Headers => {
const requestHeaders = new Headers(headers)
const authCreds = (isPreviewMode ? process.env.DRUPAL_BASIC_AUTH_ADMIN : process.env.DRUPAL_BASIC_AUTH) as string
requestHeaders.set("Authorization", "Basic " + Buffer.from(authCreds).toString("base64"))
return requestHeaders
}
export const getActiveTrail = (menuItems: MenuItem[], currentPath: NodeInterface["path"]) => {
const getActiveTrailInner = (menuItems: MenuItem[], trail: string[] = []): string[] => {
let childTrail, currentTrail
for (let i = 0; i < menuItems.length; i++) {
currentTrail = [...trail]
currentTrail.push(menuItems[i].id)
if (currentPath === menuItems[i].url) {
return currentTrail
}
if (menuItems[i].children) {
childTrail = getActiveTrailInner(menuItems[i].children, [...currentTrail])
if (childTrail.length > 0) {
return childTrail
}
}
}
return []
}
return getActiveTrailInner(menuItems)
}
export type Slug = {slug: string[]}
export type PageProps = {
params: Promise<Slug>
searchParams?: Promise<Record<string, string | string[] | undefined>>
}
export const getPathFromContext = (slug: string | string[], prefix = ""): string => {
let slugString = Array.isArray(slug) ? slug.map(s => encodeURIComponent(s)).join("/") : slug
slugString = slugString.replace(/^\//, "")
return prefix ? `${prefix}/${slugString}` : `/${slugString}`
}