@weaverse/next 0.1.0-alpha.9 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -73
- package/dist/{chunk-6PMZ77RA.mjs → chunk-OSC5HR5V.mjs} +117 -8
- package/dist/chunk-OSC5HR5V.mjs.map +1 -0
- package/dist/client-boundary.js +1425 -0
- package/dist/client-boundary.js.map +1 -0
- package/dist/client-boundary.mjs +1395 -0
- package/dist/client-boundary.mjs.map +1 -0
- package/dist/index.js +292 -1003
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +90 -966
- package/dist/index.mjs.map +1 -1
- package/dist/server.js +321 -91
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +205 -28
- package/dist/server.mjs.map +1 -1
- package/dist/types/client-boundary.d.ts +25 -0
- package/dist/types/client-boundary.d.ts.map +1 -0
- package/dist/types/client.d.ts +16 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/hooks.d.ts +20 -0
- package/dist/types/hooks.d.ts.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/item.d.ts +60 -0
- package/dist/types/item.d.ts.map +1 -0
- package/dist/types/loader.d.ts +25 -0
- package/dist/types/loader.d.ts.map +1 -0
- package/dist/types/main.d.ts +11 -0
- package/dist/types/main.d.ts.map +1 -0
- package/dist/types/provider.d.ts +82 -0
- package/dist/types/provider.d.ts.map +1 -0
- package/dist/types/registry.d.ts +15 -0
- package/dist/types/registry.d.ts.map +1 -0
- package/dist/types/renderer.d.ts +21 -0
- package/dist/types/renderer.d.ts.map +1 -0
- package/dist/types/request-info.d.ts +19 -0
- package/dist/types/request-info.d.ts.map +1 -0
- package/dist/types/revalidate-item.d.ts +82 -0
- package/dist/types/revalidate-item.d.ts.map +1 -0
- package/dist/types/root-provider.d.ts +62 -0
- package/dist/types/root-provider.d.ts.map +1 -0
- package/dist/types/runtime.d.ts +113 -0
- package/dist/types/runtime.d.ts.map +1 -0
- package/dist/types/seo.d.ts +55 -0
- package/dist/types/seo.d.ts.map +1 -0
- package/dist/types/server/configs.d.ts +27 -0
- package/dist/types/server/configs.d.ts.map +1 -0
- package/dist/types/server/normalize-page-url.d.ts +19 -0
- package/dist/types/server/normalize-page-url.d.ts.map +1 -0
- package/dist/types/server/revalidate-handler.d.ts +55 -0
- package/dist/types/server/revalidate-handler.d.ts.map +1 -0
- package/dist/types/server/server-client.d.ts +21 -0
- package/dist/types/server/server-client.d.ts.map +1 -0
- package/dist/types/server.d.ts +19 -0
- package/dist/types/server.d.ts.map +1 -0
- package/dist/types/studio-bridge.d.ts +16 -0
- package/dist/types/studio-bridge.d.ts.map +1 -0
- package/dist/types/studio-connect.d.ts +27 -0
- package/dist/types/studio-connect.d.ts.map +1 -0
- package/dist/types/studio-router.d.ts +50 -0
- package/dist/types/studio-router.d.ts.map +1 -0
- package/dist/types/studio-script-src.d.ts +12 -0
- package/dist/types/studio-script-src.d.ts.map +1 -0
- package/dist/types/theme-settings-store.d.ts +18 -0
- package/dist/types/theme-settings-store.d.ts.map +1 -0
- package/dist/types/translate.d.ts +56 -0
- package/dist/types/translate.d.ts.map +1 -0
- package/dist/types/translation-context.d.ts +109 -0
- package/dist/types/translation-context.d.ts.map +1 -0
- package/dist/types/translation-store.d.ts +44 -0
- package/dist/types/translation-store.d.ts.map +1 -0
- package/dist/types/types.d.ts +665 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/use-weaverse-next-studio.d.ts +34 -0
- package/dist/types/use-weaverse-next-studio.d.ts.map +1 -0
- package/dist/types/utils.d.ts +22 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +12 -27
- package/src/client-boundary.ts +50 -0
- package/src/client.ts +102 -0
- package/src/hooks.ts +50 -0
- package/src/index.ts +143 -0
- package/src/item.ts +167 -0
- package/src/loader.ts +135 -0
- package/src/main.tsx +45 -0
- package/src/provider.tsx +223 -0
- package/src/registry.ts +63 -0
- package/src/renderer.tsx +100 -0
- package/src/request-info.ts +124 -0
- package/src/revalidate-item.ts +211 -0
- package/src/root-provider.tsx +180 -0
- package/src/runtime.ts +555 -0
- package/src/seo.ts +157 -0
- package/src/server/configs.ts +178 -0
- package/src/server/normalize-page-url.ts +73 -0
- package/src/server/revalidate-handler.ts +357 -0
- package/src/server/server-client.ts +728 -0
- package/src/server.ts +67 -0
- package/src/studio-bridge.tsx +82 -0
- package/src/studio-connect.tsx +88 -0
- package/src/studio-router.ts +85 -0
- package/src/studio-script-src.ts +128 -0
- package/src/theme-settings-store.ts +47 -0
- package/src/translate.ts +125 -0
- package/src/translation-context.tsx +182 -0
- package/src/translation-store.ts +69 -0
- package/src/types.ts +753 -0
- package/src/use-weaverse-next-studio.tsx +106 -0
- package/src/utils.ts +49 -0
- package/dist/chunk-6PMZ77RA.mjs.map +0 -1
- package/dist/index.d.mts +0 -580
- package/dist/index.d.ts +0 -580
- package/dist/seo-Dos9t22_.d.mts +0 -477
- package/dist/seo-Dos9t22_.d.ts +0 -477
- package/dist/server.d.mts +0 -99
- package/dist/server.d.ts +0 -99
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useSafeExternalStore } from '@weaverse/react'
|
|
4
|
+
import { createContext, type ReactNode, useContext, useMemo } from 'react'
|
|
5
|
+
import { createTranslate, type TranslateFunction } from './translate'
|
|
6
|
+
import type { TranslationStore } from './translation-store'
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Context & Provider
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/** Translation function and live override stores exposed by `useTranslation`. */
|
|
13
|
+
export interface TranslationValue {
|
|
14
|
+
/** Active-locale translated keys loaded from the storefront API. */
|
|
15
|
+
merchantOverrides: Record<string, unknown> | null
|
|
16
|
+
/**
|
|
17
|
+
* Translate a key with optional variable interpolation.
|
|
18
|
+
*
|
|
19
|
+
* Priority chain (highest wins):
|
|
20
|
+
* 1. External `t` (passed into `WeaverseNextRootProvider` / `TranslationProvider`)
|
|
21
|
+
* 2. Design overrides (live Builder edits from `TranslationStore`)
|
|
22
|
+
* 3. `merchantOverrides` (DB/API overrides for the current locale)
|
|
23
|
+
* 4. `staticContent` (theme default JSON)
|
|
24
|
+
* 5. The key itself (fallback)
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* t('cart.title') // => "Cart"
|
|
28
|
+
* t('badge.sale', { percentage: 15 }) // => "-15% Off"
|
|
29
|
+
*/
|
|
30
|
+
t: TranslateFunction
|
|
31
|
+
/**
|
|
32
|
+
* Legacy name for the live design-mode translation store.
|
|
33
|
+
*
|
|
34
|
+
* @deprecated Migrate to {@link TranslationValue.translationStore}. This
|
|
35
|
+
* alias points at the same store instance.
|
|
36
|
+
*/
|
|
37
|
+
themeTextStore: TranslationStore | null
|
|
38
|
+
/** Exposed so the runtime/Studio bridge can attach it to `internal`. */
|
|
39
|
+
translationStore: TranslationStore | null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const TranslationContext = createContext<TranslationValue | null>(null)
|
|
43
|
+
TranslationContext.displayName = 'WeaverseNextTranslationContext'
|
|
44
|
+
|
|
45
|
+
/** Sources and content rendered by {@link TranslationProvider}. */
|
|
46
|
+
export interface TranslationProviderProps {
|
|
47
|
+
/** React subtree that can call `useTranslation`. */
|
|
48
|
+
children: ReactNode
|
|
49
|
+
/** Active-locale static-text overrides loaded from Weaverse. */
|
|
50
|
+
merchantOverrides?: Record<string, unknown>
|
|
51
|
+
/** Default-locale static content from the theme schema. */
|
|
52
|
+
staticContent: Record<string, unknown>
|
|
53
|
+
/**
|
|
54
|
+
* Optional external translation function.
|
|
55
|
+
* When provided it becomes the **highest-priority** source:
|
|
56
|
+
* if it returns a non-empty string that differs from the raw key,
|
|
57
|
+
* that value is used; otherwise the internal resolution chain continues.
|
|
58
|
+
*/
|
|
59
|
+
t?: TranslateFunction
|
|
60
|
+
/**
|
|
61
|
+
* Legacy prop name for the live translation store.
|
|
62
|
+
*
|
|
63
|
+
* @deprecated Migrate to {@link TranslationProviderProps.translationStore}.
|
|
64
|
+
* Still accepted for backward compatibility; ignored when `translationStore`
|
|
65
|
+
* is also provided.
|
|
66
|
+
*/
|
|
67
|
+
themeTextStore?: TranslationStore | null
|
|
68
|
+
/** Optional store for live design-mode text overrides from the builder. */
|
|
69
|
+
translationStore?: TranslationStore | null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// No-op fallback for useSafeExternalStore when store is null (production).
|
|
73
|
+
// Module-level constants — stable references, zero allocation per render.
|
|
74
|
+
const EMPTY: Record<string, string> = {}
|
|
75
|
+
const NOOP_SUBSCRIBE = () => () => {
|
|
76
|
+
// No-op unsubscribe for production/static renders without a translation store.
|
|
77
|
+
}
|
|
78
|
+
const NOOP_SNAPSHOT = () => EMPTY
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Provider that wraps the app to expose the `t()` function.
|
|
82
|
+
*
|
|
83
|
+
* - `staticContent` – the theme's default locale JSON (from themeSchema.i18n)
|
|
84
|
+
* - `merchantOverrides` – locale-specific overrides fetched from the API
|
|
85
|
+
* - `translationStore` – optional store for live design-mode text overrides
|
|
86
|
+
* (the deprecated `themeTextStore` prop is accepted as a fallback)
|
|
87
|
+
*/
|
|
88
|
+
export function TranslationProvider({
|
|
89
|
+
staticContent,
|
|
90
|
+
merchantOverrides,
|
|
91
|
+
t: externalT,
|
|
92
|
+
translationStore,
|
|
93
|
+
themeTextStore,
|
|
94
|
+
children,
|
|
95
|
+
}: TranslationProviderProps) {
|
|
96
|
+
// Canonical store, falling back to the deprecated `themeTextStore` prop.
|
|
97
|
+
let store = translationStore ?? themeTextStore ?? null
|
|
98
|
+
|
|
99
|
+
// Subscribe to design-mode overrides. No-op when store is null (production).
|
|
100
|
+
let designOverrides = useSafeExternalStore(
|
|
101
|
+
store?.subscribe ?? NOOP_SUBSCRIBE,
|
|
102
|
+
store?.getSnapshot ?? NOOP_SNAPSHOT,
|
|
103
|
+
store?.getServerSnapshot ?? NOOP_SNAPSHOT
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
let value = useMemo<TranslationValue>(
|
|
107
|
+
() => ({
|
|
108
|
+
t: createTranslate({
|
|
109
|
+
staticContent,
|
|
110
|
+
merchantOverrides,
|
|
111
|
+
designOverrides,
|
|
112
|
+
externalT,
|
|
113
|
+
}),
|
|
114
|
+
translationStore: store,
|
|
115
|
+
// Deprecated alias — same instance, kept for backward compatibility.
|
|
116
|
+
themeTextStore: store,
|
|
117
|
+
merchantOverrides: merchantOverrides ?? null,
|
|
118
|
+
}),
|
|
119
|
+
[staticContent, merchantOverrides, designOverrides, externalT, store]
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<TranslationContext.Provider value={value}>
|
|
124
|
+
{children}
|
|
125
|
+
</TranslationContext.Provider>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Hook to access the translation `t()` function.
|
|
131
|
+
*
|
|
132
|
+
* Must be used inside a component wrapped by `WeaverseNextRootProvider` or
|
|
133
|
+
* `TranslationProvider`.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```tsx
|
|
137
|
+
* function CartTitle() {
|
|
138
|
+
* let { t } = useTranslation()
|
|
139
|
+
* return <h2>{t('cart.title')}</h2>
|
|
140
|
+
* }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export function useTranslation(): TranslationValue {
|
|
144
|
+
let ctx = useContext(TranslationContext)
|
|
145
|
+
if (!ctx) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
'useTranslation must be used within <TranslationProvider>. Wrap your app with <WeaverseNextRootProvider>.'
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
return ctx
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Deprecated aliases (backward compatibility — kept exported indefinitely)
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Legacy name for {@link TranslationProvider}.
|
|
159
|
+
*
|
|
160
|
+
* @deprecated Replace `ThemeTextProvider` with {@link TranslationProvider};
|
|
161
|
+
* the props and runtime behavior are unchanged.
|
|
162
|
+
*/
|
|
163
|
+
export const ThemeTextProvider = TranslationProvider
|
|
164
|
+
/**
|
|
165
|
+
* Legacy name for {@link useTranslation}.
|
|
166
|
+
*
|
|
167
|
+
* @deprecated Replace `useThemeText` with {@link useTranslation}; the return
|
|
168
|
+
* value is unchanged.
|
|
169
|
+
*/
|
|
170
|
+
export const useThemeText = useTranslation
|
|
171
|
+
/**
|
|
172
|
+
* Legacy name for {@link TranslationValue}.
|
|
173
|
+
*
|
|
174
|
+
* @deprecated Import {@link TranslationValue} instead.
|
|
175
|
+
*/
|
|
176
|
+
export type ThemeTextValue = TranslationValue
|
|
177
|
+
/**
|
|
178
|
+
* Legacy name for {@link TranslationProviderProps}.
|
|
179
|
+
*
|
|
180
|
+
* @deprecated Import {@link TranslationProviderProps} instead.
|
|
181
|
+
*/
|
|
182
|
+
export type ThemeTextProviderProps = TranslationProviderProps
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal mutable store for design-mode static text overrides.
|
|
3
|
+
*
|
|
4
|
+
* `useSyncExternalStore`-compatible. Passed into {@link TranslationProvider}
|
|
5
|
+
* (from `./translation-context`) which subscribes internally and layers
|
|
6
|
+
* overrides on top of `merchantOverrides`/`staticContent`. The Studio bridge
|
|
7
|
+
* calls `updateOverrides()` via `runtime.internal.translationStore` (also
|
|
8
|
+
* exposed as the deprecated `runtime.internal.themeTextStore` alias) so live
|
|
9
|
+
* static-text edits reach the runtime — identical surface to Hydrogen's store,
|
|
10
|
+
* so Builder's existing static-text RPC works with `@weaverse/next` unchanged.
|
|
11
|
+
*
|
|
12
|
+
* Shape: flat `Record<string, string>` — dot-notation keys map to values
|
|
13
|
+
* (e.g. "cart.title" → "Shopping Cart").
|
|
14
|
+
*/
|
|
15
|
+
export class TranslationStore {
|
|
16
|
+
/** Current flat translation overrides keyed by dot path. */
|
|
17
|
+
overrides: Record<string, string> = {}
|
|
18
|
+
private readonly listeners: Set<() => void> = new Set()
|
|
19
|
+
|
|
20
|
+
/** Replace every override and notify subscribers. */
|
|
21
|
+
setOverrides = (overrides: Record<string, string>) => {
|
|
22
|
+
this.overrides = { ...overrides }
|
|
23
|
+
this.emit()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Merge changed overrides into the current values and notify subscribers. */
|
|
27
|
+
updateOverrides = (newOverrides: Record<string, string>) => {
|
|
28
|
+
this.overrides = { ...this.overrides, ...newOverrides }
|
|
29
|
+
this.emit()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Return the current client-side override snapshot. */
|
|
33
|
+
getSnapshot = () => this.overrides
|
|
34
|
+
|
|
35
|
+
/** Return the override snapshot used during server rendering. */
|
|
36
|
+
getServerSnapshot = () => this.overrides
|
|
37
|
+
|
|
38
|
+
/** Subscribe to override changes and return an unsubscribe function. */
|
|
39
|
+
subscribe = (callback: () => void) => {
|
|
40
|
+
this.listeners.add(callback)
|
|
41
|
+
return () => {
|
|
42
|
+
this.listeners.delete(callback)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private readonly emit = () => {
|
|
47
|
+
for (const listener of Array.from(this.listeners)) {
|
|
48
|
+
try {
|
|
49
|
+
listener()
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error('TranslationStore: Listener error', error)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Legacy constructor name for {@link TranslationStore}.
|
|
59
|
+
*
|
|
60
|
+
* @deprecated Replace `ThemeTextStore` with {@link TranslationStore}; the
|
|
61
|
+
* store contract and runtime behavior are unchanged.
|
|
62
|
+
*/
|
|
63
|
+
export const ThemeTextStore = TranslationStore
|
|
64
|
+
/**
|
|
65
|
+
* Legacy type name for {@link TranslationStore}.
|
|
66
|
+
*
|
|
67
|
+
* @deprecated Import {@link TranslationStore} instead.
|
|
68
|
+
*/
|
|
69
|
+
export type ThemeTextStore = TranslationStore
|