6f4a680156
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / test (push) Has been cancelled
CI / frontend (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Security Scan / backend-security (push) Has been cancelled
Security Scan / frontend-security (push) Has been cancelled
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import type { CustomMenuItem, CustomMenuLinkOpenMode, CustomMenuPlacement } from '@/types'
|
|
|
|
export const DEFAULT_CUSTOM_MENU_PLACEMENT: CustomMenuPlacement = 'sidebar'
|
|
export const DEFAULT_CUSTOM_MENU_LINK_OPEN_MODE: CustomMenuLinkOpenMode = 'internal'
|
|
|
|
export function normalizeCustomMenuPlacement(
|
|
value: unknown,
|
|
): CustomMenuPlacement {
|
|
if (value === 'home_header' || value === 'both') {
|
|
return value
|
|
}
|
|
return DEFAULT_CUSTOM_MENU_PLACEMENT
|
|
}
|
|
|
|
export function normalizeCustomMenuLinkOpenMode(
|
|
value: unknown,
|
|
): CustomMenuLinkOpenMode {
|
|
if (value === 'external') {
|
|
return value
|
|
}
|
|
return DEFAULT_CUSTOM_MENU_LINK_OPEN_MODE
|
|
}
|
|
|
|
export function normalizeCustomMenuItem(item: CustomMenuItem): CustomMenuItem {
|
|
return {
|
|
...item,
|
|
placement: normalizeCustomMenuPlacement(item.placement),
|
|
link_open_mode: normalizeCustomMenuLinkOpenMode(item.link_open_mode),
|
|
}
|
|
}
|
|
|
|
export function normalizeCustomMenuItems(
|
|
items: CustomMenuItem[] | null | undefined,
|
|
): CustomMenuItem[] {
|
|
if (!Array.isArray(items)) {
|
|
return []
|
|
}
|
|
return items.map((item) => normalizeCustomMenuItem(item))
|
|
}
|
|
|
|
export function isSidebarMenuPlacement(
|
|
item: Pick<CustomMenuItem, 'placement'>,
|
|
): boolean {
|
|
const placement = normalizeCustomMenuPlacement(item.placement)
|
|
return placement === 'sidebar' || placement === 'both'
|
|
}
|
|
|
|
export function isHomeHeaderMenuPlacement(
|
|
item: Pick<CustomMenuItem, 'placement'>,
|
|
): boolean {
|
|
const placement = normalizeCustomMenuPlacement(item.placement)
|
|
return placement === 'home_header' || placement === 'both'
|
|
}
|
|
|
|
export function getCustomMenuRoute(id: string): string {
|
|
return `/custom/${encodeURIComponent(id)}`
|
|
}
|
|
|
|
export function getCustomMenuHref(
|
|
item: Pick<CustomMenuItem, 'id' | 'url' | 'page_slug'>,
|
|
): string {
|
|
if (item.page_slug || item.url?.startsWith('md:')) {
|
|
return getCustomMenuRoute(item.id)
|
|
}
|
|
|
|
const rawUrl = item.url?.trim()
|
|
if (rawUrl) {
|
|
return rawUrl
|
|
}
|
|
|
|
return getCustomMenuRoute(item.id)
|
|
}
|
|
|
|
export function isExternalCustomMenuLink(
|
|
item: { link_open_mode?: unknown },
|
|
): boolean {
|
|
return normalizeCustomMenuLinkOpenMode(item.link_open_mode) === 'external'
|
|
}
|
|
|
|
export const getHomeHeaderMenuHref = getCustomMenuHref
|