From 2370d7e32ea89fb94abbda22ef38bbfc1c568a69 Mon Sep 17 00:00:00 2001 From: tommaso1 Date: Fri, 2 Feb 2024 11:17:40 +0100 Subject: [PATCH] [DEV-866] Redirect to the main guide if no version is specified from the path (#507) * redirect to last guide if no one is specified from the path * handle main version * Create new-cows-melt.md * Update .changeset/new-cows-melt.md Co-authored-by: Marco Comi <9998393+kin0992@users.noreply.github.com> * improve logics * Update apps/nextjs-website/src/lib/types/guideData.ts Co-authored-by: Marco Ponchia * Update apps/nextjs-website/src/lib/types/guideData.ts Co-authored-by: Marco Ponchia * add main version for each guide * add main version guide * fix getMainGuide fn * changes after review * update logics for redirect * changes after review * remove unused regex * changes after review --------- Co-authored-by: marcobottaro <39835990+marcobottaro@users.noreply.github.com> Co-authored-by: Jeremy Gordillo Co-authored-by: Marco Comi <9998393+kin0992@users.noreply.github.com> Co-authored-by: Marco Ponchia --- .changeset/new-cows-melt.md | 5 ++ .../src/_contents/appIo/guides.ts | 6 ++ .../src/_contents/ioSign/guides.ts | 1 + apps/nextjs-website/src/_contents/makeDocs.ts | 55 +++++++++++-------- .../src/_contents/pagoPa/guides.ts | 10 ++++ .../src/_contents/send/guides.ts | 4 ++ .../guides/[...productGuidePage]/page.tsx | 3 +- apps/nextjs-website/src/lib/api.ts | 14 +++-- .../nextjs-website/src/lib/types/guideData.ts | 34 ++++++++++++ 9 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 .changeset/new-cows-melt.md diff --git a/.changeset/new-cows-melt.md b/.changeset/new-cows-melt.md new file mode 100644 index 000000000..4e23ef8eb --- /dev/null +++ b/.changeset/new-cows-melt.md @@ -0,0 +1,5 @@ +--- +"nextjs-website": minor +--- + +[DEV-866] Redirect to the main guide if no version is specified from the path diff --git a/apps/nextjs-website/src/_contents/appIo/guides.ts b/apps/nextjs-website/src/_contents/appIo/guides.ts index bc4dabece..aa57eb833 100644 --- a/apps/nextjs-website/src/_contents/appIo/guides.ts +++ b/apps/nextjs-website/src/_contents/appIo/guides.ts @@ -10,6 +10,7 @@ const guidaTecnica: GuideDefinition = { }, versions: [ { + main: true, version: 'v5.0', dirName: 'UAvruCMPsowDKZMH1jNr', }, @@ -53,6 +54,7 @@ const manualeDeiServizi: GuideDefinition = { }, versions: [ { + main: true, version: 'v2.0', dirName: 'xWONfJmawghGo2ekuaKh', }, @@ -76,6 +78,7 @@ const supportoAgliEnti: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'rPr79NYJ4xbKA7EvgHxo', }, @@ -91,6 +94,7 @@ const kitDiComunicazione: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'SpNLdqKSqoCvaOneGN7K', }, @@ -106,6 +110,7 @@ const cartaGiovani: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'Vgh5yq561A3SOPVQrWes', }, @@ -121,6 +126,7 @@ const accordiAdesione: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'O7clRJB6pY0VI5sEBF8J', }, diff --git a/apps/nextjs-website/src/_contents/ioSign/guides.ts b/apps/nextjs-website/src/_contents/ioSign/guides.ts index 925332575..4f383e74f 100644 --- a/apps/nextjs-website/src/_contents/ioSign/guides.ts +++ b/apps/nextjs-website/src/_contents/ioSign/guides.ts @@ -10,6 +10,7 @@ const manualeOperativo: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'AdBuOCmwur7AhLlgfCeG', }, diff --git a/apps/nextjs-website/src/_contents/makeDocs.ts b/apps/nextjs-website/src/_contents/makeDocs.ts index d2484a0a2..b5a103cd6 100644 --- a/apps/nextjs-website/src/_contents/makeDocs.ts +++ b/apps/nextjs-website/src/_contents/makeDocs.ts @@ -19,6 +19,7 @@ export type GuideDefinition = { readonly slug: string; }; readonly versions: ReadonlyArray<{ + readonly main?: boolean; readonly version: string; readonly dirName: string; }>; @@ -67,28 +68,38 @@ export const makeGuide = ({ const guidePath = `${product.path}/guides/${guide.slug}`; return pipe( versions, - RA.map(({ version, dirName }) => ({ - product: product, - guide: { - name: guide.name, - path: guidePath, - }, - version: { - name: version, - path: `${guidePath}/${version}`, - }, - versions: versions.map(({ version }) => ({ - name: version, - path: `${guidePath}/${version}`, - })), - source: { - pathPrefix: `${guidePath}/${version}`, - assetsPrefix: `${docsAssetsPath}/${dirName}`, - dirPath: `${docsPath}/${dirName}`, - spaceId: dirName, - }, - bannerLinks: bannerLinks, - })), + RA.map(({ main = false, version, dirName }) => { + const item = { + product: product, + guide: { + name: guide.name, + path: guidePath, + }, + version: { + main, + name: version, + path: `${guidePath}/${version}`, + }, + versions: versions.map(({ main = false, version }) => ({ + main, + name: version, + path: `${guidePath}/${version}`, + })), + source: { + pathPrefix: `${guidePath}/${version}`, + assetsPrefix: `${docsAssetsPath}/${dirName}`, + dirPath: `${docsPath}/${dirName}`, + spaceId: dirName, + }, + bannerLinks: bannerLinks, + }; + // We need to generate two items for the main version of the guide + // One with the name version and one without version in the path + return main + ? [item, { ...item, source: { ...item.source, pathPrefix: guidePath } }] + : [item]; + }), + RA.flatten, // parse docs files parseDocOrThrow ); diff --git a/apps/nextjs-website/src/_contents/pagoPa/guides.ts b/apps/nextjs-website/src/_contents/pagoPa/guides.ts index f5e22c8b8..c2b5ce741 100644 --- a/apps/nextjs-website/src/_contents/pagoPa/guides.ts +++ b/apps/nextjs-website/src/_contents/pagoPa/guides.ts @@ -10,6 +10,7 @@ const saci: GuideDefinition = { }, versions: [ { + main: true, version: '3.2.0', dirName: 'PXEYBQEZ9LagztJLF89O', }, @@ -45,6 +46,7 @@ const sanp: GuideDefinition = { dirName: 'PiBduZS70S7Ae4WpWywl', }, { + main: true, version: '3.6.0', dirName: 'MiKJPaFrEwpU051b68kr', }, @@ -104,6 +106,7 @@ const avvisi: GuideDefinition = { }, versions: [ { + main: true, version: '3.2.0', dirName: 'WNT7oSWxH3PFe3AGG2zK', }, @@ -128,6 +131,7 @@ const brand: GuideDefinition = { versions: [ { + main: true, version: 'v1.0', dirName: '8phwN5u2QXllSKsqBjQU', }, @@ -143,6 +147,7 @@ const pda: GuideDefinition = { }, versions: [ { + main: true, version: 'v4', dirName: 'EQluaHvgNKaiHlsDcnrT', }, @@ -166,6 +171,7 @@ const boEc: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'TbHElktP96kviaIsxPFs', }, @@ -181,6 +187,7 @@ const boPsp: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: '46oWOwuxwu0HEYnJPQ4h', }, @@ -196,6 +203,7 @@ const errori: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'mU2qgiLV1G3m9z1VjAOc', }, @@ -211,6 +219,7 @@ const metadata: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'u6YdY319vyFX9MIvnKBa', }, @@ -226,6 +235,7 @@ const redirect: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: '6zB9GJvZkc3ChlAsMjql', }, diff --git a/apps/nextjs-website/src/_contents/send/guides.ts b/apps/nextjs-website/src/_contents/send/guides.ts index 16032f7a2..47ce4b9d5 100644 --- a/apps/nextjs-website/src/_contents/send/guides.ts +++ b/apps/nextjs-website/src/_contents/send/guides.ts @@ -10,6 +10,7 @@ const validatore: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.0', dirName: 'niZ9BM7pjxsgBMMWBim1', }, @@ -25,6 +26,7 @@ const manualeOperativo: GuideDefinition = { }, versions: [ { + main: true, version: 'v1.1.1', dirName: 'XbcyIfLqbQFM4OsEpBQY', }, @@ -52,6 +54,7 @@ const knowledgeBase: GuideDefinition = { }, versions: [ { + main: true, version: 'v2.1', dirName: 'LjuHmRnHyge98hRa4Ao4', }, @@ -75,6 +78,7 @@ const modelloDiIntegrazione: GuideDefinition = { }, versions: [ { + main: true, version: 'v2.1', dirName: 'cQbgjUoB6uPKpsuCW3ZW', }, diff --git a/apps/nextjs-website/src/app/[productSlug]/guides/[...productGuidePage]/page.tsx b/apps/nextjs-website/src/app/[productSlug]/guides/[...productGuidePage]/page.tsx index c1a428bd1..c32da4b75 100644 --- a/apps/nextjs-website/src/app/[productSlug]/guides/[...productGuidePage]/page.tsx +++ b/apps/nextjs-website/src/app/[productSlug]/guides/[...productGuidePage]/page.tsx @@ -70,6 +70,7 @@ const Page = async ({ params }: { params: Params }) => { params?.productSlug, params?.productGuidePage ?? [''] ); + const { product, page, guide, version, versions, source, bannerLinks } = guideProps; const props: ProductGuidePageProps = { @@ -77,7 +78,7 @@ const Page = async ({ params }: { params: Params }) => { product, guide, version, - versions, + versions: Array.from(versions), bannerLinks, pathPrefix: source.pathPrefix, bodyConfig: { diff --git a/apps/nextjs-website/src/lib/api.ts b/apps/nextjs-website/src/lib/api.ts index 16de56944..732918f9d 100644 --- a/apps/nextjs-website/src/lib/api.ts +++ b/apps/nextjs-website/src/lib/api.ts @@ -11,6 +11,7 @@ import { import { Product, ProductSubpathsKeys } from './types/product'; import { Webinar } from '@/lib/types/webinar'; import { webinars } from '@/_contents/webinars'; +import { GuidePage } from './types/guideData'; function manageUndefined(props: undefined | null | T) { if (!props) { @@ -34,9 +35,10 @@ export async function getApi(productSlug?: string) { export async function getGuide( productSlug?: string, productGuidePage?: ReadonlyArray -) { +): Promise { const guidePath = productGuidePage?.join('/'); const path = `/${productSlug}/guides/${guidePath}`; + const props = manageUndefined(guides.find(({ page }) => page.path === path)); return { @@ -47,12 +49,16 @@ export async function getGuide( }; } +function getProductGuidePath(path: string) { + // the filter is to remove the first 3 elements of the path which are + // an empty string (the path begins with a / symbol), the product slug and 'guides' hard-coded string + return path.split('/').filter((p, index) => index > 2); +} + export function getGuidePaths() { return guides.map((guide) => ({ slug: guide.product.slug, - // the filter is to remove the first 3 elements of the path which are - // an empty string (the path begins with a / symbol), the product slug and 'guides' hard-coded string - guidePaths: guide.page.path.split('/').filter((p, index) => index > 2), + guidePaths: getProductGuidePath(guide.page.path), })); } diff --git a/apps/nextjs-website/src/lib/types/guideData.ts b/apps/nextjs-website/src/lib/types/guideData.ts index 51b24aba6..9e22276cf 100644 --- a/apps/nextjs-website/src/lib/types/guideData.ts +++ b/apps/nextjs-website/src/lib/types/guideData.ts @@ -1,6 +1,40 @@ +import { BannerLinkProps } from '@/editorialComponents/BannerLink'; import { Path } from '@/lib/types/path'; +import { DocPage } from 'gitbook-docs/parseDoc'; +import { Product } from './product'; export type Guide = { readonly title: string; readonly description: string; } & Path; + +export type GuideVersion = { + readonly main: boolean; + readonly name: string; + readonly path: string; +}; + +export type GuideSource = { + readonly pathPrefix: string; + readonly assetsPrefix: string; + readonly dirPath: string; + readonly spaceId: string; +}; + +export type GuidePage = NonNullable< + DocPage<{ + readonly product: Product; + readonly guide: { + readonly name: string; + readonly path: string; + }; + readonly version: GuideVersion; + readonly versions: readonly GuideVersion[]; + readonly source: GuideSource; + readonly bannerLinks: readonly BannerLinkProps[]; + readonly products: readonly Product[]; + readonly pathPrefix: string; + readonly assetsPrefix: string; + readonly redirect?: boolean; + }> +>;