@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.
Files changed (116) hide show
  1. package/README.md +161 -73
  2. package/dist/{chunk-6PMZ77RA.mjs → chunk-OSC5HR5V.mjs} +117 -8
  3. package/dist/chunk-OSC5HR5V.mjs.map +1 -0
  4. package/dist/client-boundary.js +1425 -0
  5. package/dist/client-boundary.js.map +1 -0
  6. package/dist/client-boundary.mjs +1395 -0
  7. package/dist/client-boundary.mjs.map +1 -0
  8. package/dist/index.js +292 -1003
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +90 -966
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/server.js +321 -91
  13. package/dist/server.js.map +1 -1
  14. package/dist/server.mjs +205 -28
  15. package/dist/server.mjs.map +1 -1
  16. package/dist/types/client-boundary.d.ts +25 -0
  17. package/dist/types/client-boundary.d.ts.map +1 -0
  18. package/dist/types/client.d.ts +16 -0
  19. package/dist/types/client.d.ts.map +1 -0
  20. package/dist/types/hooks.d.ts +20 -0
  21. package/dist/types/hooks.d.ts.map +1 -0
  22. package/dist/types/index.d.ts +33 -0
  23. package/dist/types/index.d.ts.map +1 -0
  24. package/dist/types/item.d.ts +60 -0
  25. package/dist/types/item.d.ts.map +1 -0
  26. package/dist/types/loader.d.ts +25 -0
  27. package/dist/types/loader.d.ts.map +1 -0
  28. package/dist/types/main.d.ts +11 -0
  29. package/dist/types/main.d.ts.map +1 -0
  30. package/dist/types/provider.d.ts +82 -0
  31. package/dist/types/provider.d.ts.map +1 -0
  32. package/dist/types/registry.d.ts +15 -0
  33. package/dist/types/registry.d.ts.map +1 -0
  34. package/dist/types/renderer.d.ts +21 -0
  35. package/dist/types/renderer.d.ts.map +1 -0
  36. package/dist/types/request-info.d.ts +19 -0
  37. package/dist/types/request-info.d.ts.map +1 -0
  38. package/dist/types/revalidate-item.d.ts +82 -0
  39. package/dist/types/revalidate-item.d.ts.map +1 -0
  40. package/dist/types/root-provider.d.ts +62 -0
  41. package/dist/types/root-provider.d.ts.map +1 -0
  42. package/dist/types/runtime.d.ts +113 -0
  43. package/dist/types/runtime.d.ts.map +1 -0
  44. package/dist/types/seo.d.ts +55 -0
  45. package/dist/types/seo.d.ts.map +1 -0
  46. package/dist/types/server/configs.d.ts +27 -0
  47. package/dist/types/server/configs.d.ts.map +1 -0
  48. package/dist/types/server/normalize-page-url.d.ts +19 -0
  49. package/dist/types/server/normalize-page-url.d.ts.map +1 -0
  50. package/dist/types/server/revalidate-handler.d.ts +55 -0
  51. package/dist/types/server/revalidate-handler.d.ts.map +1 -0
  52. package/dist/types/server/server-client.d.ts +21 -0
  53. package/dist/types/server/server-client.d.ts.map +1 -0
  54. package/dist/types/server.d.ts +19 -0
  55. package/dist/types/server.d.ts.map +1 -0
  56. package/dist/types/studio-bridge.d.ts +16 -0
  57. package/dist/types/studio-bridge.d.ts.map +1 -0
  58. package/dist/types/studio-connect.d.ts +27 -0
  59. package/dist/types/studio-connect.d.ts.map +1 -0
  60. package/dist/types/studio-router.d.ts +50 -0
  61. package/dist/types/studio-router.d.ts.map +1 -0
  62. package/dist/types/studio-script-src.d.ts +12 -0
  63. package/dist/types/studio-script-src.d.ts.map +1 -0
  64. package/dist/types/theme-settings-store.d.ts +18 -0
  65. package/dist/types/theme-settings-store.d.ts.map +1 -0
  66. package/dist/types/translate.d.ts +56 -0
  67. package/dist/types/translate.d.ts.map +1 -0
  68. package/dist/types/translation-context.d.ts +109 -0
  69. package/dist/types/translation-context.d.ts.map +1 -0
  70. package/dist/types/translation-store.d.ts +44 -0
  71. package/dist/types/translation-store.d.ts.map +1 -0
  72. package/dist/types/types.d.ts +665 -0
  73. package/dist/types/types.d.ts.map +1 -0
  74. package/dist/types/use-weaverse-next-studio.d.ts +34 -0
  75. package/dist/types/use-weaverse-next-studio.d.ts.map +1 -0
  76. package/dist/types/utils.d.ts +22 -0
  77. package/dist/types/utils.d.ts.map +1 -0
  78. package/package.json +12 -27
  79. package/src/client-boundary.ts +50 -0
  80. package/src/client.ts +102 -0
  81. package/src/hooks.ts +50 -0
  82. package/src/index.ts +143 -0
  83. package/src/item.ts +167 -0
  84. package/src/loader.ts +135 -0
  85. package/src/main.tsx +45 -0
  86. package/src/provider.tsx +223 -0
  87. package/src/registry.ts +63 -0
  88. package/src/renderer.tsx +100 -0
  89. package/src/request-info.ts +124 -0
  90. package/src/revalidate-item.ts +211 -0
  91. package/src/root-provider.tsx +180 -0
  92. package/src/runtime.ts +555 -0
  93. package/src/seo.ts +157 -0
  94. package/src/server/configs.ts +178 -0
  95. package/src/server/normalize-page-url.ts +73 -0
  96. package/src/server/revalidate-handler.ts +357 -0
  97. package/src/server/server-client.ts +728 -0
  98. package/src/server.ts +67 -0
  99. package/src/studio-bridge.tsx +82 -0
  100. package/src/studio-connect.tsx +88 -0
  101. package/src/studio-router.ts +85 -0
  102. package/src/studio-script-src.ts +128 -0
  103. package/src/theme-settings-store.ts +47 -0
  104. package/src/translate.ts +125 -0
  105. package/src/translation-context.tsx +182 -0
  106. package/src/translation-store.ts +69 -0
  107. package/src/types.ts +753 -0
  108. package/src/use-weaverse-next-studio.tsx +106 -0
  109. package/src/utils.ts +49 -0
  110. package/dist/chunk-6PMZ77RA.mjs.map +0 -1
  111. package/dist/index.d.mts +0 -580
  112. package/dist/index.d.ts +0 -580
  113. package/dist/seo-Dos9t22_.d.mts +0 -477
  114. package/dist/seo-Dos9t22_.d.ts +0 -477
  115. package/dist/server.d.mts +0 -99
  116. 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