Skip to content

Commit

Permalink
refactor: move locale switching logic out of state
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Feb 19, 2024
1 parent f87b369 commit 3b25c9a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-mirrors-perform.md
@@ -0,0 +1,5 @@
---
'@makeswift/runtime': patch
---

Moves locale switching logic out of the redux middleware state and closer to Next.js logic.
16 changes: 16 additions & 0 deletions packages/runtime/src/components/hooks/useRouterLocaleSync.ts
@@ -0,0 +1,16 @@
import { useRouter } from 'next/router'

import { useEffect } from 'react'
import { setLocale } from '../../state/actions'
import { useDispatch } from '../../runtimes/react/hooks/use-dispatch'

export const useRouterLocaleSync = () => {
const router = useRouter()
const dispatch = useDispatch()

useEffect(() => {
if (!router.locale) return

dispatch(setLocale(new Intl.Locale(router.locale)))
}, [router.locale])
}
3 changes: 3 additions & 0 deletions packages/runtime/src/components/page/Page.tsx
Expand Up @@ -13,6 +13,7 @@ import { useIsInBuilder } from '../../react'
import deepEqual from '../../utils/deepEqual'
import { MakeswiftPageDocument } from '../../next'
import { Page as PageType, Site } from '../../api'
import { useRouterLocaleSync } from '../hooks/useRouterLocaleSync'

const SnippetLocation = {
Body: 'BODY',
Expand Down Expand Up @@ -165,6 +166,8 @@ export function Page({ document: page }: Props): JSX.Element {

const documentId = baseLocalizedPage?.elementTreeId ?? page.id

useRouterLocaleSync()

return (
<>
<Head>
Expand Down
15 changes: 1 addition & 14 deletions packages/runtime/src/state/react-builder-preview.ts
Expand Up @@ -45,7 +45,6 @@ import {
changePathnameComplete,
elementFromPointChange,
setBreakpoints,
setLocale,
} from './actions'
import { ActionTypes } from './actions'
import { createPropController } from '../prop-controllers/instances'
Expand Down Expand Up @@ -496,11 +495,6 @@ export function messageChannelMiddleware(
const breakpoints = ReactPage.getBreakpoints(state)
messageChannel.port1.postMessage(setBreakpoints(breakpoints))

const routerLocale = Router.locale
if (routerLocale != null) {
messageChannel.port1.postMessage(setLocale(new Intl.Locale(routerLocale)))
}

Router.events.on('routeChangeStart', () => {
messageChannel.port1.postMessage(changePathnameStart())
})
Expand All @@ -519,6 +513,7 @@ export function messageChannelMiddleware(
case ActionTypes.HANDLE_WHEEL:
case ActionTypes.HANDLE_POINTER_MOVE:
case ActionTypes.ELEMENT_FROM_POINT_CHANGE:
case ActionTypes.SET_LOCALE:
messageChannel.port1.postMessage(action)
break

Expand Down Expand Up @@ -550,14 +545,6 @@ export function messageChannelMiddleware(
window.getSelection()?.removeAllRanges()
break

case ActionTypes.SET_LOCALE: {
const { pathname: currentPathname, query } = Router
const pathname = (action.payload.pathname ?? currentPathname).replace(/^\//, '/')

Router.replace({ pathname, query }, undefined, { locale: action.payload.locale })
break
}

case ActionTypes.CHANGE_PATHNAME: {
const pathname = action.payload.pathname.replace(/^\//, '/')
const currentPathname = Router.asPath.replace(/^\//, '/')
Expand Down

0 comments on commit 3b25c9a

Please sign in to comment.