* feat(SW-3108): Added external link options to shortcuts * feat(SW-3108): Added external link options to header Approved-by: Matilda Landström
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { BlocksEnums } from "../../../../types/blocksEnum"
|
|
import {
|
|
linkRefsUnionSchema,
|
|
linkUnionSchema,
|
|
transformPageLink,
|
|
} from "../pageLinks"
|
|
|
|
export const shortcutsBlockSchema = z.object({
|
|
shortcuts: z
|
|
.object({
|
|
subtitle: z.string().nullable(),
|
|
title: z.string().nullable(),
|
|
two_column_list: z.boolean().nullable().default(false),
|
|
shortcuts: z
|
|
.array(
|
|
z
|
|
.object({
|
|
is_contentstack_link: z.boolean().nullish(),
|
|
external_link: z
|
|
.object({
|
|
href: z.string().nullish().default(""),
|
|
title: z.string().nullish(),
|
|
})
|
|
.nullish(),
|
|
open_in_new_tab: z.boolean(),
|
|
text: z.string().optional().default(""),
|
|
linkConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: linkUnionSchema.transform((data) => {
|
|
const link = transformPageLink(data)
|
|
if (link) {
|
|
return link
|
|
}
|
|
return data
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
.transform(
|
|
({
|
|
is_contentstack_link,
|
|
external_link,
|
|
linkConnection,
|
|
open_in_new_tab,
|
|
text,
|
|
}) => {
|
|
if (
|
|
is_contentstack_link !== false &&
|
|
linkConnection.edges.length
|
|
) {
|
|
const linkRef = linkConnection.edges[0].node
|
|
return {
|
|
openInNewTab: open_in_new_tab,
|
|
text: text || linkRef.title,
|
|
url: linkRef.url,
|
|
}
|
|
} else if (
|
|
is_contentstack_link === false &&
|
|
external_link?.href
|
|
) {
|
|
return {
|
|
openInNewTab: open_in_new_tab,
|
|
text: text || external_link.title || "",
|
|
url: external_link.href,
|
|
}
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
)
|
|
)
|
|
.transform((data) => data.filter((item) => !!item)),
|
|
})
|
|
.transform(({ two_column_list, ...rest }) => {
|
|
return {
|
|
...rest,
|
|
hasTwoColumns: !!two_column_list,
|
|
}
|
|
}),
|
|
})
|
|
|
|
export const shortcutsSchema = z
|
|
.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.Shortcuts)
|
|
.optional()
|
|
.default(BlocksEnums.block.Shortcuts),
|
|
})
|
|
.merge(shortcutsBlockSchema)
|
|
|
|
export const shortcutsRefsSchema = z.object({
|
|
shortcuts: z.object({
|
|
shortcuts: z
|
|
.array(
|
|
z.object({
|
|
linkConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: linkRefsUnionSchema,
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
)
|
|
.transform((data) =>
|
|
data
|
|
.map((shortcut) => {
|
|
return shortcut.linkConnection.edges.map(({ node }) => node.system)
|
|
})
|
|
.flat()
|
|
),
|
|
}),
|
|
})
|