Merged in feat/refactor-header-footer-sitewidealert (pull request #1374)
Refactor: removed parallel routes for header, footer and sidewidealert. Langswitcher and sidewidealert now client components * feat - removed parallel routes and made sidepeek and sitewidealerts as client components * Langswitcher as client component * Fixed lang switcher for current header * Passing lang when fetching siteconfig * Merge branch 'master' into feat/refactor-header-footer-sitewidealert * Refactor * Removed dead code * Show only languages that has translation * Refetch sitewidealert every 60 seconds * Merge branch 'master' into feat/refactor-header-footer-sitewidealert * Removed sidepeek parallel route from my-stay * Added missing env.var to env.test * Removed console.log Approved-by: Joakim Jäderberg
This commit is contained in:
10
server/routers/contentstack/languageSwitcher/input.ts
Normal file
10
server/routers/contentstack/languageSwitcher/input.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const getLanguageSwitcherInput = z
|
||||
.object({
|
||||
lang: z.nativeEnum(Lang),
|
||||
pathName: z.string(),
|
||||
})
|
||||
.optional()
|
||||
@@ -46,8 +46,10 @@ import {
|
||||
import { internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { getUidAndContentTypeByPath } from "@/services/cms/getUidAndContentTypeByPath"
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import { getLanguageSwitcherInput } from "./input"
|
||||
import { validateLanguageSwitcherData } from "./output"
|
||||
import { languageSwitcherAffix } from "./utils"
|
||||
|
||||
@@ -163,87 +165,103 @@ async function getLanguageSwitcher(options: LanguageSwitcherVariables) {
|
||||
}
|
||||
|
||||
export const languageSwitcherQueryRouter = router({
|
||||
get: publicProcedure.query(async ({ ctx }) => {
|
||||
if (!ctx.uid || !ctx.lang) {
|
||||
return { lang: ctx.lang, urls: baseUrls }
|
||||
}
|
||||
getLanguageSwitcherCounter.add(1, {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
})
|
||||
console.info(
|
||||
"contentstack.languageSwitcher start",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
},
|
||||
get: publicProcedure
|
||||
.input(getLanguageSwitcherInput)
|
||||
.query(async ({ input, ctx }) => {
|
||||
let uid = ctx.uid
|
||||
let contentType = ctx.contentType
|
||||
let lang = ctx.lang ?? input?.lang
|
||||
|
||||
if (input) {
|
||||
const data = await getUidAndContentTypeByPath(input.pathName)
|
||||
uid = data.uid
|
||||
contentType = data.contentType ?? ctx.contentType
|
||||
}
|
||||
|
||||
if (!uid || !lang) {
|
||||
return { lang: lang, urls: baseUrls }
|
||||
}
|
||||
|
||||
getLanguageSwitcherCounter.add(1, {
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
})
|
||||
)
|
||||
const res = await getLanguageSwitcher({
|
||||
contentType: ctx.contentType!,
|
||||
uid: ctx.uid,
|
||||
})
|
||||
|
||||
const urls = Object.keys(res.data).reduce<LanguageSwitcherData>(
|
||||
(acc, key) => {
|
||||
const item = res.data[key as Lang]
|
||||
const url = item
|
||||
? item.web?.original_url || `/${key}${item.url}`
|
||||
: undefined
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: { url, isExternal: !!item?.web?.original_url },
|
||||
}
|
||||
},
|
||||
{} as LanguageSwitcherData
|
||||
)
|
||||
|
||||
const validatedLanguageSwitcherData =
|
||||
validateLanguageSwitcherData.safeParse(urls)
|
||||
|
||||
if (!validatedLanguageSwitcherData.success) {
|
||||
getLanguageSwitcherFailCounter.add(1, {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedLanguageSwitcherData.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.languageSwitcher validation error",
|
||||
console.info(
|
||||
"contentstack.languageSwitcher start",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
},
|
||||
error: validatedLanguageSwitcherData.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
getLanguageSwitcherSuccessCounter.add(1, {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
})
|
||||
console.info(
|
||||
"contentstack.languageSwitcher success",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
uid: ctx.uid,
|
||||
lang: ctx.lang,
|
||||
contentType: ctx.contentType,
|
||||
},
|
||||
const res = await getLanguageSwitcher({
|
||||
contentType: contentType!,
|
||||
uid: uid,
|
||||
})
|
||||
)
|
||||
return {
|
||||
lang: ctx.lang,
|
||||
urls,
|
||||
}
|
||||
}),
|
||||
|
||||
const urls = Object.keys(res.data).reduce<LanguageSwitcherData>(
|
||||
(acc, key) => {
|
||||
const item = res.data[key as Lang]
|
||||
|
||||
if (!item?.url) return acc // Skip languages without a URL
|
||||
|
||||
const url = item
|
||||
? item.web?.original_url || `/${key}${item.url}`
|
||||
: undefined
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: { url, isExternal: !!item?.web?.original_url },
|
||||
}
|
||||
},
|
||||
{} as LanguageSwitcherData
|
||||
)
|
||||
|
||||
const validatedLanguageSwitcherData =
|
||||
validateLanguageSwitcherData.safeParse(urls)
|
||||
|
||||
if (!validatedLanguageSwitcherData.success) {
|
||||
getLanguageSwitcherFailCounter.add(1, {
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedLanguageSwitcherData.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.languageSwitcher validation error",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
},
|
||||
error: validatedLanguageSwitcherData.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
getLanguageSwitcherSuccessCounter.add(1, {
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
})
|
||||
console.info(
|
||||
"contentstack.languageSwitcher success",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
uid: uid,
|
||||
lang: lang,
|
||||
contentType: contentType,
|
||||
},
|
||||
})
|
||||
)
|
||||
return {
|
||||
lang: lang,
|
||||
urls,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user