Skip to content

Commit

Permalink
fix: hydrate App Router link with locales
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvin Poddar authored and arvinpoddar committed Mar 12, 2024
1 parent 056aac1 commit b953798
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-monkeys-reply.md
@@ -0,0 +1,5 @@
---
'@makeswift/runtime': patch
---

Button component and Link control now hydrate page links with the locale, if present. Brings automatic link localization to App Router, while still supporting Pages Router.
10 changes: 10 additions & 0 deletions packages/runtime/src/api/resource-types.ts
@@ -0,0 +1,10 @@
import { z } from 'zod'

const pagePathnameSliceSchema = z.object({
id: z.string(),
pathname: z.string(),
localizedPathname: z.string().optional().nullable(),
__typename: z.literal('PagePathnameSlice'),
})

export type PagePathnameSlice = z.infer<typeof pagePathnameSliceSchema>
7 changes: 6 additions & 1 deletion packages/runtime/src/components/shared/Link/index.tsx
Expand Up @@ -19,6 +19,8 @@ export const Link = forwardRef<HTMLAnchorElement, Props>(function Link(
) {
const pageId = link && link.type === 'OPEN_PAGE' ? link.payload.pageId : null
const page = usePagePathnameSlice(pageId ?? null)
const hasLocalizedPathname = page?.localizedPathname != null

const elementKey =
link?.type === 'SCROLL_TO_ELEMENT' ? link.payload.elementIdConfig?.elementKey : null
const elementId = useElementId(elementKey)
Expand All @@ -36,7 +38,7 @@ export const Link = forwardRef<HTMLAnchorElement, Props>(function Link(
if (page) {
useNextLink = true

href = `/${page.pathname}`
href = `/${page.localizedPathname ?? page.pathname}`
}

target = link.payload.openInNewTab ? '_blank' : '_self'
Expand Down Expand Up @@ -130,6 +132,9 @@ export const Link = forwardRef<HTMLAnchorElement, Props>(function Link(
target={target}
onClick={handleClick}
href={href}
{...(hasLocalizedPathname && {
locale: false,
})}
// Next.js v12 has legacyBehavior set to true by default
legacyBehavior={false}
/>
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/next/client.ts
Expand Up @@ -128,6 +128,7 @@ const pagePathnameSlicesAPISchema = z.array(
id: z.string(),
basePageId: z.string(),
pathname: z.string(),
localizedPathname: z.string().optional(),
__typename: z.literal('PagePathnameSlice'),
})
.nullable(),
Expand Down Expand Up @@ -609,9 +610,9 @@ export class Makeswift {
if (pagePathnameSlice == null) return null

return {
...pagePathnameSlice,
id: pagePathnameSlice.basePageId,
pathname: pagePathnameSlice.pathname,
__typename: pagePathnameSlice.__typename,
localizedPathname: pagePathnameSlice.localizedPathname ?? null,
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/src/runtimes/react/controls/link.ts
Expand Up @@ -19,6 +19,7 @@ export function useLinkControlValue<T extends LinkControlDefinition>(
): LinkControlValue<T> {
const pageId = link && link.type === 'OPEN_PAGE' ? link.payload.pageId : null
const page = usePagePathnameSlice(pageId ?? null)

const elementKey =
link?.type === 'SCROLL_TO_ELEMENT' ? link.payload.elementIdConfig?.elementKey : null
const elementId = useElementId(elementKey)
Expand All @@ -30,7 +31,7 @@ export function useLinkControlValue<T extends LinkControlDefinition>(
if (link) {
switch (link.type) {
case 'OPEN_PAGE': {
if (page) href = `/${page.pathname}`
if (page) href = `/${page.localizedPathname ?? page.pathname}`

target = link.payload.openInNewTab ? '_blank' : '_self'

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/runtimes/react/hooks/makeswift-api.ts
Expand Up @@ -6,11 +6,11 @@ import {
File,
GlobalElement,
LocalizedGlobalElement,
PagePathnameSlice,
Swatch,
Table,
Typography,
} from '../../../api'
import { PagePathnameSlice } from '../../../api/resource-types'
import { useMakeswiftHostApiClient } from '../../../next/context/makeswift-host-api-client'

export function useSwatch(swatchId: string | null): Swatch | null {
Expand Down

0 comments on commit b953798

Please sign in to comment.