Skip to content

Commit 2ce26fa

Browse files
authored
fix(next): version view throws useLocale() server error (#15380)
### What Fixed `Attempted to call useLocale() from the server` error when navigating to version view. ### Why `getVersionLabel` was calling the `useLocale()` hook directly, but it was being invoked from a server component context in the version comparison dropdown. ### How - Changed `getVersionLabel` to accept `currentLocale` as an optional parameter - Client component (`VersionPillLabel`) now calls `useLocale()` and passes the value down - Server component (`VersionView`) passes the user's locale from the request Fixes #15294
1 parent ba9605e commit 2ce26fa

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/next/src/views/Version/VersionPillLabel/VersionPillLabel.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import type { TypeWithVersion } from 'payload'
44

5-
import { Pill, useConfig, useTranslation } from '@payloadcms/ui'
5+
import { Pill, useConfig, useLocale, useTranslation } from '@payloadcms/ui'
66
import { formatDate } from '@payloadcms/ui/shared'
77
import React from 'react'
88

@@ -63,8 +63,10 @@ export const VersionPillLabel: React.FC<{
6363
},
6464
} = useConfig()
6565
const { i18n, t } = useTranslation()
66+
const { code: currentLocale } = useLocale()
6667

6768
const { label, pillStyle } = getVersionLabel({
69+
currentLocale,
6870
currentlyPublishedVersion,
6971
latestDraftVersion,
7072
t,

packages/next/src/views/Version/VersionPillLabel/getVersionLabel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { TFunction } from '@payloadcms/translations'
2-
3-
import { type Pill, useLocale } from '@payloadcms/ui'
2+
import type { Pill } from '@payloadcms/ui'
43

54
type Args = {
5+
currentLocale?: string
66
currentlyPublishedVersion?: {
77
id: number | string
88
publishedLocale?: string
@@ -28,6 +28,7 @@ type Args = {
2828
* given existing versions and the current version status.
2929
*/
3030
export function getVersionLabel({
31+
currentLocale,
3132
currentlyPublishedVersion,
3233
latestDraftVersion,
3334
t,
@@ -37,7 +38,6 @@ export function getVersionLabel({
3738
name: 'currentDraft' | 'currentlyPublished' | 'draft' | 'previouslyPublished' | 'published'
3839
pillStyle: Parameters<typeof Pill>[0]['pillStyle']
3940
} {
40-
const { code: currentLocale } = useLocale()
4141
const status = version.version._status
4242

4343
if (status === 'draft') {

packages/next/src/views/Version/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export async function VersionView(props: DocumentViewServerProps) {
4141

4242
const draftsEnabled = hasDraftsEnabled(collectionConfig || globalConfig)
4343

44+
// Resolve user's current locale for version label comparison (not 'all')
45+
const userLocale =
46+
(searchParams.locale as string) ||
47+
(req.locale !== 'all' ? req.locale : localization && localization.defaultLocale)
48+
4449
const localeCodesFromParams = searchParams.localeCodes
4550
? JSON.parse(searchParams.localeCodes as string)
4651
: null
@@ -393,6 +398,7 @@ export async function VersionView(props: DocumentViewServerProps) {
393398
const label =
394399
optionWithSameID.labelOverride ||
395400
getVersionLabel({
401+
currentLocale: userLocale,
396402
currentlyPublishedVersion,
397403
latestDraftVersion,
398404
t: i18n.t,

0 commit comments

Comments
 (0)