@vicacha-devs/medusa-plugin-stories 0.0.0-stage → 0.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 (143) hide show
  1. package/.medusa/server/src/__tests__/stories-plugin.spec.js +358 -0
  2. package/.medusa/server/src/admin/i18n/index.js +35 -0
  3. package/.medusa/server/src/admin/i18n/json/ar.json +196 -0
  4. package/.medusa/server/src/admin/i18n/json/de.json +196 -0
  5. package/.medusa/server/src/admin/i18n/json/en.json +196 -0
  6. package/.medusa/server/src/admin/i18n/json/es.json +196 -0
  7. package/.medusa/server/src/admin/i18n/json/fr.json +196 -0
  8. package/.medusa/server/src/admin/i18n/json/it.json +196 -0
  9. package/.medusa/server/src/admin/i18n/json/nl.json +196 -0
  10. package/.medusa/server/src/admin/i18n/json/pl.json +196 -0
  11. package/.medusa/server/src/admin/i18n/json/pt.json +196 -0
  12. package/.medusa/server/src/admin/i18n/json/tr.json +196 -0
  13. package/.medusa/server/src/admin/index.cjs +4699 -0
  14. package/.medusa/server/src/admin/index.js +4699 -0
  15. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  16. package/.medusa/server/src/api/admin/stories/[id]/hotspots/route.js +14 -0
  17. package/.medusa/server/src/api/admin/stories/[id]/route.js +27 -0
  18. package/.medusa/server/src/api/admin/stories/[id]/stickers/route.js +14 -0
  19. package/.medusa/server/src/api/admin/stories/analytics/route.js +7 -0
  20. package/.medusa/server/src/api/admin/stories/groups/[id]/route.js +23 -0
  21. package/.medusa/server/src/api/admin/stories/groups/route.js +15 -0
  22. package/.medusa/server/src/api/admin/stories/route.js +21 -0
  23. package/.medusa/server/src/api/middlewares.js +58 -0
  24. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  25. package/.medusa/server/src/api/store/stories/[id]/events/route.js +12 -0
  26. package/.medusa/server/src/api/store/stories/[id]/interact/route.js +14 -0
  27. package/.medusa/server/src/api/store/stories/route.js +33 -0
  28. package/.medusa/server/src/api/validators.js +136 -0
  29. package/.medusa/server/src/hooks/index.js +213 -0
  30. package/.medusa/server/src/hooks/use-sdk-primitives.js +93 -0
  31. package/.medusa/server/src/index.js +6 -0
  32. package/.medusa/server/src/jobs/expire-ephemeral-stories.js +10 -0
  33. package/.medusa/server/src/modules/index.js +7 -0
  34. package/.medusa/server/src/modules/stories/index.js +3 -0
  35. package/.medusa/server/src/modules/story/index.js +23 -0
  36. package/.medusa/server/src/modules/story/migrations/Migration20260514000000.js +181 -0
  37. package/.medusa/server/src/modules/story/models/index.js +141 -0
  38. package/.medusa/server/src/modules/story/service.js +673 -0
  39. package/.medusa/server/src/modules/story/types.js +3 -0
  40. package/.medusa/server/src/modules/story/utils/security.js +76 -0
  41. package/.medusa/server/src/rbac.js +75 -0
  42. package/.medusa/server/src/sdk/index.js +148 -0
  43. package/.medusa/server/src/subscribers/product-deleted.js +11 -0
  44. package/.medusa/server/src/types/index.js +2 -0
  45. package/.medusa/server/src/ui/index.js +5 -0
  46. package/.medusa/server/src/ui/story-hotspot-pin.js +43 -0
  47. package/.medusa/server/src/ui/story-player-modal.js +104 -0
  48. package/.medusa/server/src/ui/story-product-sheet.js +7 -0
  49. package/.medusa/server/src/ui/story-rings-bar.js +42 -0
  50. package/.medusa/server/src/workflows/index.js +50 -0
  51. package/LICENSE +21 -0
  52. package/README.md +246 -2
  53. package/package.json +127 -3
  54. package/src/__tests__/medusa-shim.mjs +533 -0
  55. package/src/__tests__/register-ts-resolver.mjs +141 -0
  56. package/src/__tests__/stories-plugin.spec.ts +526 -0
  57. package/src/admin/README.md +94 -0
  58. package/src/admin/components/analytics-tab.tsx +225 -0
  59. package/src/admin/components/canvas-editor-tab.tsx +77 -0
  60. package/src/admin/components/hotspot-form-section.tsx +155 -0
  61. package/src/admin/components/index.ts +15 -0
  62. package/src/admin/components/overview-cards.tsx +96 -0
  63. package/src/admin/components/product-stories-card.tsx +123 -0
  64. package/src/admin/components/rbac-guard.tsx +153 -0
  65. package/src/admin/components/slide-form-section.tsx +187 -0
  66. package/src/admin/components/slide-preview-canvas.tsx +166 -0
  67. package/src/admin/components/slide-timeline-section.tsx +126 -0
  68. package/src/admin/components/sticker-form-section.tsx +201 -0
  69. package/src/admin/components/sticker-list-section.tsx +113 -0
  70. package/src/admin/components/stickers-tab.tsx +34 -0
  71. package/src/admin/components/story-ring-form.tsx +241 -0
  72. package/src/admin/components/story-rings-tab.tsx +204 -0
  73. package/src/admin/components/story-rings-table.tsx +222 -0
  74. package/src/admin/components/studio-header.tsx +67 -0
  75. package/src/admin/hooks/use-product-stories.ts +71 -0
  76. package/src/admin/hooks/use-stories-studio.ts +439 -0
  77. package/src/admin/i18n/README.md +84 -0
  78. package/src/admin/i18n/index.ts +36 -0
  79. package/src/admin/i18n/json/ar.json +196 -0
  80. package/src/admin/i18n/json/de.json +196 -0
  81. package/src/admin/i18n/json/en.json +196 -0
  82. package/src/admin/i18n/json/es.json +196 -0
  83. package/src/admin/i18n/json/fr.json +196 -0
  84. package/src/admin/i18n/json/it.json +196 -0
  85. package/src/admin/i18n/json/nl.json +196 -0
  86. package/src/admin/i18n/json/pl.json +196 -0
  87. package/src/admin/i18n/json/pt.json +196 -0
  88. package/src/admin/i18n/json/tr.json +196 -0
  89. package/src/admin/routes/stories/analytics/page.tsx +66 -0
  90. package/src/admin/routes/stories/canvas/page.tsx +101 -0
  91. package/src/admin/routes/stories/page.tsx +164 -0
  92. package/src/admin/routes/stories/rings/@create/page.tsx +61 -0
  93. package/src/admin/routes/stories/rings/page.tsx +88 -0
  94. package/src/admin/routes/stories/stickers/page.tsx +74 -0
  95. package/src/admin/tsconfig.json +23 -0
  96. package/src/admin/utils/theme-presets.ts +94 -0
  97. package/src/admin/vite-env.d.ts +1 -0
  98. package/src/admin/widgets/product-stories-widget.tsx +32 -0
  99. package/src/api/README.md +61 -0
  100. package/src/api/admin/plugin/route.ts +5 -0
  101. package/src/api/admin/stories/[id]/hotspots/route.ts +17 -0
  102. package/src/api/admin/stories/[id]/route.ts +40 -0
  103. package/src/api/admin/stories/[id]/stickers/route.ts +20 -0
  104. package/src/api/admin/stories/analytics/route.ts +8 -0
  105. package/src/api/admin/stories/groups/[id]/route.ts +30 -0
  106. package/src/api/admin/stories/groups/route.ts +18 -0
  107. package/src/api/admin/stories/route.ts +31 -0
  108. package/src/api/middlewares.ts +73 -0
  109. package/src/api/store/plugin/route.ts +5 -0
  110. package/src/api/store/stories/[id]/events/route.ts +14 -0
  111. package/src/api/store/stories/[id]/interact/route.ts +18 -0
  112. package/src/api/store/stories/route.ts +44 -0
  113. package/src/api/validators.ts +166 -0
  114. package/src/hooks/index.ts +419 -0
  115. package/src/hooks/use-sdk-primitives.ts +114 -0
  116. package/src/index.ts +14 -0
  117. package/src/jobs/README.md +16 -0
  118. package/src/jobs/expire-ephemeral-stories.ts +14 -0
  119. package/src/links/README.md +18 -0
  120. package/src/modules/README.md +46 -0
  121. package/src/modules/index.ts +15 -0
  122. package/src/modules/stories/index.ts +11 -0
  123. package/src/modules/story/index.ts +34 -0
  124. package/src/modules/story/migrations/Migration20260514000000.ts +196 -0
  125. package/src/modules/story/models/index.ts +146 -0
  126. package/src/modules/story/service.ts +1052 -0
  127. package/src/modules/story/types.ts +190 -0
  128. package/src/modules/story/utils/security.ts +100 -0
  129. package/src/providers/README.md +14 -0
  130. package/src/rbac.ts +89 -0
  131. package/src/sdk/index.ts +233 -0
  132. package/src/subscribers/README.md +16 -0
  133. package/src/subscribers/product-deleted.ts +15 -0
  134. package/src/types/index.ts +1 -0
  135. package/src/types/medusa-ui.d.ts +451 -0
  136. package/src/ui/index.ts +4 -0
  137. package/src/ui/story-hotspot-pin.tsx +122 -0
  138. package/src/ui/story-player-modal.tsx +361 -0
  139. package/src/ui/story-product-sheet.tsx +85 -0
  140. package/src/ui/story-rings-bar.tsx +113 -0
  141. package/src/workflows/README.md +19 -0
  142. package/src/workflows/index.ts +217 -0
  143. package/tsconfig.json +39 -0
@@ -0,0 +1,166 @@
1
+ import { z } from "@medusajs/framework/zod"
2
+
3
+ export const StoryRingThemeSchema = z.enum([
4
+ "instagram",
5
+ "tiktok",
6
+ "sunset",
7
+ "emerald",
8
+ "violet",
9
+ "cyber",
10
+ "neon",
11
+ "gold",
12
+ "minimal",
13
+ "custom",
14
+ ])
15
+
16
+ export const StoryPlacementZoneSchema = z.enum([
17
+ "home",
18
+ "collection",
19
+ "category",
20
+ "product",
21
+ "floating_bubble",
22
+ "global",
23
+ ])
24
+
25
+ export const CreateStoryGroupSchema = z.object({
26
+ title: z.string().min(1).max(160),
27
+ handle: z.string().max(160).optional(),
28
+ subtitle: z.string().max(240).nullable().optional(),
29
+ avatar_url: z.string().max(1024).nullable().optional(),
30
+ ring_theme: StoryRingThemeSchema.optional(),
31
+ custom_ring_color: z.string().max(32).nullable().optional(),
32
+ placement: StoryPlacementZoneSchema.optional(),
33
+ target_entity_id: z.string().max(256).nullable().optional(),
34
+ is_pinned: z.boolean().optional(),
35
+ is_ephemeral: z.boolean().optional(),
36
+ is_active: z.boolean().optional(),
37
+ display_order: z.coerce.number().int().min(0).max(10000).optional(),
38
+ metadata: z.record(z.string(), z.unknown()).nullable().optional(),
39
+ })
40
+
41
+ export const UpdateStoryGroupSchema = CreateStoryGroupSchema.partial()
42
+
43
+ export const CreateStoryHotspotSchema = z.object({
44
+ product_id: z.string().min(1).max(256),
45
+ variant_id: z.string().max(256).nullable().optional(),
46
+ product_title: z.string().min(1).max(256),
47
+ product_handle: z.string().max(256).nullable().optional(),
48
+ product_thumbnail: z.string().max(1024).nullable().optional(),
49
+ price_formatted: z.string().max(64).nullable().optional(),
50
+ price_amount: z.number().min(0).nullable().optional(),
51
+ currency_code: z.string().max(12).nullable().optional(),
52
+ x_percent: z.number().min(0).max(100),
53
+ y_percent: z.number().min(0).max(100),
54
+ label: z.string().max(120).nullable().optional(),
55
+ badge_style: z.enum(["pill", "card", "dot"]).optional(),
56
+ })
57
+
58
+ export const DeleteStoryHotspotSchema = z.object({
59
+ hotspot_id: z.string().min(1).max(256),
60
+ })
61
+
62
+ export const CreateStoryStickerSchema = z.object({
63
+ type: z.enum(["poll", "countdown", "coupon", "question_badge"]),
64
+ prompt: z.string().min(1).max(280),
65
+ x_percent: z.number().min(0).max(100).optional(),
66
+ y_percent: z.number().min(0).max(100).optional(),
67
+ options: z
68
+ .array(
69
+ z.object({
70
+ id: z.string().max(64).optional(),
71
+ label: z.string().min(1).max(120),
72
+ votes: z.number().int().min(0).optional(),
73
+ })
74
+ )
75
+ .max(6)
76
+ .nullable()
77
+ .optional(),
78
+ coupon_code: z.string().max(64).nullable().optional(),
79
+ countdown_until: z.string().max(64).nullable().optional(),
80
+ })
81
+
82
+ export const DeleteStoryStickerSchema = z.object({
83
+ sticker_id: z.string().min(1).max(256),
84
+ })
85
+
86
+ const safeHttpUrlSchema = z
87
+ .string()
88
+ .min(1)
89
+ .max(1024)
90
+ .refine(
91
+ (val) => {
92
+ if (val.startsWith("/")) return !val.startsWith("//")
93
+ try {
94
+ const parsed = new URL(val)
95
+ return parsed.protocol === "http:" || parsed.protocol === "https:"
96
+ } catch {
97
+ return false
98
+ }
99
+ },
100
+ { message: "URL must use http://, https://, or a relative path" }
101
+ )
102
+
103
+ export const CreateStorySlideSchema = z.object({
104
+ group_id: z.string().max(256).nullable().optional(),
105
+ title: z.string().min(1).max(200),
106
+ caption: z.string().max(500).nullable().optional(),
107
+ media_type: z.enum(["image", "video"]).optional(),
108
+ media_url: safeHttpUrlSchema,
109
+ thumbnail_url: safeHttpUrlSchema.nullable().optional(),
110
+ duration_ms: z.number().int().min(1000).max(60000).optional(),
111
+ link_url: safeHttpUrlSchema.nullable().optional(),
112
+ link_text: z.string().max(120).nullable().optional(),
113
+ is_active: z.boolean().optional(),
114
+ display_order: z.coerce.number().int().min(0).max(10000).optional(),
115
+ starts_at: z.string().max(64).nullable().optional(),
116
+ expires_at: z.string().max(64).nullable().optional(),
117
+ hotspots: z.array(CreateStoryHotspotSchema).max(20).optional(),
118
+ stickers: z.array(CreateStoryStickerSchema).max(10).optional(),
119
+ metadata: z.record(z.string(), z.unknown()).nullable().optional(),
120
+ })
121
+
122
+ export const UpdateStorySlideSchema = CreateStorySlideSchema.omit({
123
+ hotspots: true,
124
+ stickers: true,
125
+ }).partial()
126
+
127
+ export const RecordStoryEngagementSchema = z.object({
128
+ event_type: z.enum([
129
+ "impression",
130
+ "complete",
131
+ "tap_forward",
132
+ "tap_back",
133
+ "exit",
134
+ "hotspot_click",
135
+ "link_click",
136
+ "add_to_cart",
137
+ "sticker_interact",
138
+ ]),
139
+ hotspot_id: z.string().max(256).nullable().optional(),
140
+ sticker_id: z.string().max(256).nullable().optional(),
141
+ product_id: z.string().max(256).nullable().optional(),
142
+ session_id: z.string().max(256).nullable().optional(),
143
+ metadata: z.record(z.string(), z.unknown()).nullable().optional(),
144
+ })
145
+
146
+ export const InteractWithStoryStickerSchema = z.object({
147
+ sticker_id: z.string().min(1).max(256),
148
+ option_id: z.string().max(64).optional(),
149
+ response_text: z.string().max(280).optional(),
150
+ session_id: z.string().max(256).optional(),
151
+ })
152
+
153
+ export type CreateStoryGroupInput = z.infer<typeof CreateStoryGroupSchema>
154
+ export type UpdateStoryGroupInput = z.infer<typeof UpdateStoryGroupSchema>
155
+ export type CreateStorySlideInput = z.infer<typeof CreateStorySlideSchema>
156
+ export type UpdateStorySlideInput = z.infer<typeof UpdateStorySlideSchema>
157
+ export type CreateStoryHotspotInput = z.infer<typeof CreateStoryHotspotSchema>
158
+ export type DeleteStoryHotspotInput = z.infer<typeof DeleteStoryHotspotSchema>
159
+ export type CreateStoryStickerInput = z.infer<typeof CreateStoryStickerSchema>
160
+ export type DeleteStoryStickerInput = z.infer<typeof DeleteStoryStickerSchema>
161
+ export type RecordStoryEngagementInput = z.infer<
162
+ typeof RecordStoryEngagementSchema
163
+ >
164
+ export type InteractWithStoryStickerInput = z.infer<
165
+ typeof InteractWithStoryStickerSchema
166
+ >
@@ -0,0 +1,419 @@
1
+ import { useCallback, useEffect, useState } from "react"
2
+ import type {
3
+ CreateStoryGroupInput,
4
+ CreateStoryHotspotInput,
5
+ CreateStorySlideInput,
6
+ CreateStoryStickerInput,
7
+ InteractWithStoryStickerInput,
8
+ RecordStoryEngagementInput,
9
+ UpdateStoryGroupInput,
10
+ UpdateStorySlideInput,
11
+ } from "../api/validators"
12
+ import type {
13
+ StoriesAnalyticsOverview,
14
+ StoriesMutationHookOptions,
15
+ StoriesQueryHookOptions,
16
+ StoryGroupDTO,
17
+ StoryHotspotDTO,
18
+ StoryPlacementZone,
19
+ StorySlideDTO,
20
+ StoryStickerDTO,
21
+ } from "../modules/story/types"
22
+ import { defaultStoriesSdk, storiesQueryKeys } from "../sdk"
23
+ import { useStoriesMutation, useStoriesQuery } from "./use-sdk-primitives"
24
+
25
+ export * from "./use-sdk-primitives"
26
+
27
+ const SEEN_STORIES_STORAGE_KEY = "medusa_stories_seen_v1"
28
+
29
+ // ─── Admin Hooks ─────────────────────────────────────────────────────────────
30
+
31
+ export function useAdminStoriesState(
32
+ filters?: { product_id?: string },
33
+ options?: StoriesQueryHookOptions<{
34
+ stories: StorySlideDTO[]
35
+ groups: StoryGroupDTO[]
36
+ }>
37
+ ) {
38
+ return useStoriesQuery(
39
+ storiesQueryKeys.adminState(filters),
40
+ () => defaultStoriesSdk.admin.listStudioState(filters),
41
+ options
42
+ )
43
+ }
44
+
45
+ export function useStoriesAnalytics(
46
+ options?: StoriesQueryHookOptions<{ analytics: StoriesAnalyticsOverview }>
47
+ ) {
48
+ return useStoriesQuery(
49
+ storiesQueryKeys.analytics(),
50
+ () => defaultStoriesSdk.admin.getAnalytics(),
51
+ options
52
+ )
53
+ }
54
+
55
+ export function useCreateStoryGroup(
56
+ options?: StoriesMutationHookOptions<
57
+ { group: StoryGroupDTO },
58
+ CreateStoryGroupInput
59
+ >
60
+ ) {
61
+ return useStoriesMutation(
62
+ (payload) => defaultStoriesSdk.admin.createGroup(payload),
63
+ options,
64
+ storiesQueryKeys.all
65
+ )
66
+ }
67
+
68
+ export function useUpdateStoryGroup(
69
+ options?: StoriesMutationHookOptions<
70
+ { group: StoryGroupDTO },
71
+ { id: string; patch: UpdateStoryGroupInput }
72
+ >
73
+ ) {
74
+ return useStoriesMutation(
75
+ ({ id, patch }) => defaultStoriesSdk.admin.updateGroup(id, patch),
76
+ options,
77
+ storiesQueryKeys.all
78
+ )
79
+ }
80
+
81
+ export function useDeleteStoryGroup(
82
+ options?: StoriesMutationHookOptions<
83
+ { id: string; deleted: boolean },
84
+ { id: string }
85
+ >
86
+ ) {
87
+ return useStoriesMutation(
88
+ ({ id }) => defaultStoriesSdk.admin.deleteGroup(id),
89
+ options,
90
+ storiesQueryKeys.all
91
+ )
92
+ }
93
+
94
+ export function useCreateStorySlide(
95
+ options?: StoriesMutationHookOptions<
96
+ { story: StorySlideDTO },
97
+ CreateStorySlideInput
98
+ >
99
+ ) {
100
+ return useStoriesMutation(
101
+ (payload) => defaultStoriesSdk.admin.createSlide(payload),
102
+ options,
103
+ storiesQueryKeys.all
104
+ )
105
+ }
106
+
107
+ export function useUpdateStorySlide(
108
+ options?: StoriesMutationHookOptions<
109
+ { story: StorySlideDTO },
110
+ { id: string; patch: UpdateStorySlideInput }
111
+ >
112
+ ) {
113
+ return useStoriesMutation(
114
+ ({ id, patch }) => defaultStoriesSdk.admin.updateSlide(id, patch),
115
+ options,
116
+ storiesQueryKeys.all
117
+ )
118
+ }
119
+
120
+ export function useDeleteStorySlide(
121
+ options?: StoriesMutationHookOptions<
122
+ { id: string; deleted: boolean },
123
+ { id: string }
124
+ >
125
+ ) {
126
+ return useStoriesMutation(
127
+ ({ id }) => defaultStoriesSdk.admin.deleteSlide(id),
128
+ options,
129
+ storiesQueryKeys.all
130
+ )
131
+ }
132
+
133
+ export function useAddStoryHotspot(
134
+ options?: StoriesMutationHookOptions<
135
+ { hotspot: StoryHotspotDTO },
136
+ { storyId: string; hotspot: CreateStoryHotspotInput }
137
+ >
138
+ ) {
139
+ return useStoriesMutation(
140
+ ({ storyId, hotspot }) =>
141
+ defaultStoriesSdk.admin.addHotspot(storyId, hotspot),
142
+ options,
143
+ storiesQueryKeys.all
144
+ )
145
+ }
146
+
147
+ export function useRemoveStoryHotspot(
148
+ options?: StoriesMutationHookOptions<
149
+ { id: string; deleted: boolean },
150
+ { storyId: string; hotspotId: string }
151
+ >
152
+ ) {
153
+ return useStoriesMutation(
154
+ ({ storyId, hotspotId }) =>
155
+ defaultStoriesSdk.admin.removeHotspot(storyId, hotspotId),
156
+ options,
157
+ storiesQueryKeys.all
158
+ )
159
+ }
160
+
161
+ export function useAddStorySticker(
162
+ options?: StoriesMutationHookOptions<
163
+ { sticker: StoryStickerDTO },
164
+ { storyId: string; sticker: CreateStoryStickerInput }
165
+ >
166
+ ) {
167
+ return useStoriesMutation(
168
+ ({ storyId, sticker }) =>
169
+ defaultStoriesSdk.admin.addSticker(storyId, sticker),
170
+ options,
171
+ storiesQueryKeys.all
172
+ )
173
+ }
174
+
175
+ export function useRemoveStorySticker(
176
+ options?: StoriesMutationHookOptions<
177
+ { id: string; deleted: boolean },
178
+ { storyId: string; stickerId: string }
179
+ >
180
+ ) {
181
+ return useStoriesMutation(
182
+ ({ storyId, stickerId }) =>
183
+ defaultStoriesSdk.admin.removeSticker(storyId, stickerId),
184
+ options,
185
+ storiesQueryKeys.all
186
+ )
187
+ }
188
+
189
+ // ─── Storefront Hooks & Interactive 9:16 Player Engine ──────────────────────
190
+
191
+ export function useStoreStories(
192
+ filters?: {
193
+ placement?: StoryPlacementZone
194
+ target_entity_id?: string
195
+ collection_id?: string
196
+ group_id?: string
197
+ product_id?: string
198
+ },
199
+ options?: StoriesQueryHookOptions<{
200
+ stories: StorySlideDTO[]
201
+ groups: StoryGroupDTO[]
202
+ }>
203
+ ) {
204
+ return useStoriesQuery(
205
+ storiesQueryKeys.storefront(filters),
206
+ () => defaultStoriesSdk.store.listStories(filters),
207
+ options
208
+ )
209
+ }
210
+
211
+ export function useRecordStoryEvent(
212
+ options?: StoriesMutationHookOptions<
213
+ { ok: boolean; story: StorySlideDTO },
214
+ { storyId: string } & RecordStoryEngagementInput
215
+ >
216
+ ) {
217
+ return useStoriesMutation(
218
+ ({ storyId, ...payload }) =>
219
+ defaultStoriesSdk.store.recordEvent(storyId, payload),
220
+ options
221
+ )
222
+ }
223
+
224
+ export function useInteractStorySticker(
225
+ options?: StoriesMutationHookOptions<
226
+ { sticker: StoryStickerDTO },
227
+ { storyId: string } & InteractWithStoryStickerInput
228
+ >
229
+ ) {
230
+ return useStoriesMutation(
231
+ ({ storyId, ...payload }) =>
232
+ defaultStoriesSdk.store.interactWithSticker(storyId, payload),
233
+ options,
234
+ storiesQueryKeys.storefront()
235
+ )
236
+ }
237
+
238
+ /**
239
+ * Headless 9:16 Story Player state machine hook with segmented progress bar tick,
240
+ * pause-on-hold, group-to-group progression, active product sheet, and seen-ring persistence.
241
+ */
242
+ export function useStoryPlayer(params: {
243
+ groups: StoryGroupDTO[]
244
+ autoPlay?: boolean
245
+ onSlideImpression?: (slide: StorySlideDTO) => void
246
+ onSlideComplete?: (slide: StorySlideDTO) => void
247
+ }) {
248
+ const { groups, autoPlay = true, onSlideImpression, onSlideComplete } = params
249
+ const [activeGroupIndex, setActiveGroupIndex] = useState<number | null>(null)
250
+ const [activeSlideIndex, setActiveSlideIndex] = useState(0)
251
+ const [progressPct, setProgressPct] = useState(0)
252
+ const [isPaused, setIsPaused] = useState(false)
253
+ const [selectedHotspot, setSelectedHotspot] =
254
+ useState<StoryHotspotDTO | null>(null)
255
+ const [seenSlideIds, setSeenSlideIds] = useState<Set<string>>(() => {
256
+ try {
257
+ if (typeof window === "undefined") return new Set()
258
+ const raw = window.localStorage.getItem(SEEN_STORIES_STORAGE_KEY)
259
+ if (!raw) return new Set()
260
+ const parsed = JSON.parse(raw) as string[]
261
+ return new Set(Array.isArray(parsed) ? parsed : [])
262
+ } catch {
263
+ return new Set()
264
+ }
265
+ })
266
+
267
+ const activeGroup =
268
+ activeGroupIndex !== null ? (groups[activeGroupIndex] ?? null) : null
269
+ const slides = activeGroup?.slides ?? []
270
+ const activeSlide = slides[activeSlideIndex] ?? null
271
+
272
+ const markSlideSeen = useCallback((slideId: string) => {
273
+ setSeenSlideIds((prev) => {
274
+ if (prev.has(slideId)) return prev
275
+ const next = new Set(prev)
276
+ next.add(slideId)
277
+ try {
278
+ if (typeof window !== "undefined") {
279
+ window.localStorage.setItem(
280
+ SEEN_STORIES_STORAGE_KEY,
281
+ JSON.stringify(Array.from(next).slice(-500))
282
+ )
283
+ }
284
+ } catch {
285
+ // Ignore storage quota errors
286
+ }
287
+ return next
288
+ })
289
+ }, [])
290
+
291
+ const openGroup = useCallback((groupIndex: number, slideIndex = 0) => {
292
+ setActiveGroupIndex(groupIndex)
293
+ setActiveSlideIndex(slideIndex)
294
+ setProgressPct(0)
295
+ setIsPaused(false)
296
+ setSelectedHotspot(null)
297
+ }, [])
298
+
299
+ const closePlayer = useCallback(() => {
300
+ setActiveGroupIndex(null)
301
+ setActiveSlideIndex(0)
302
+ setProgressPct(0)
303
+ setIsPaused(false)
304
+ setSelectedHotspot(null)
305
+ }, [])
306
+
307
+ const nextSlide = useCallback(() => {
308
+ if (activeGroupIndex === null) return
309
+ const currentGroup = groups[activeGroupIndex]
310
+ const currentSlides = currentGroup?.slides ?? []
311
+
312
+ if (activeSlide) {
313
+ onSlideComplete?.(activeSlide)
314
+ }
315
+
316
+ if (activeSlideIndex + 1 < currentSlides.length) {
317
+ setActiveSlideIndex((prev) => prev + 1)
318
+ setProgressPct(0)
319
+ setSelectedHotspot(null)
320
+ return
321
+ }
322
+
323
+ if (activeGroupIndex + 1 < groups.length) {
324
+ setActiveGroupIndex((prev) => (prev !== null ? prev + 1 : null))
325
+ setActiveSlideIndex(0)
326
+ setProgressPct(0)
327
+ setSelectedHotspot(null)
328
+ return
329
+ }
330
+
331
+ closePlayer()
332
+ }, [
333
+ activeGroupIndex,
334
+ activeSlide,
335
+ activeSlideIndex,
336
+ closePlayer,
337
+ groups,
338
+ onSlideComplete,
339
+ ])
340
+
341
+ const prevSlide = useCallback(() => {
342
+ if (activeGroupIndex === null) return
343
+ if (activeSlideIndex > 0) {
344
+ setActiveSlideIndex((prev) => prev - 1)
345
+ setProgressPct(0)
346
+ setSelectedHotspot(null)
347
+ return
348
+ }
349
+ if (activeGroupIndex > 0) {
350
+ const prevGroupIdx = activeGroupIndex - 1
351
+ const prevGroupSlides = groups[prevGroupIdx]?.slides ?? []
352
+ setActiveGroupIndex(prevGroupIdx)
353
+ setActiveSlideIndex(Math.max(0, prevGroupSlides.length - 1))
354
+ setProgressPct(0)
355
+ setSelectedHotspot(null)
356
+ }
357
+ }, [activeGroupIndex, activeSlideIndex, groups])
358
+
359
+ useEffect(() => {
360
+ if (!activeSlide) return
361
+ markSlideSeen(activeSlide.id)
362
+ onSlideImpression?.(activeSlide)
363
+ }, [activeSlide, markSlideSeen, onSlideImpression])
364
+
365
+ useEffect(() => {
366
+ if (!autoPlay || !activeSlide || isPaused || Boolean(selectedHotspot)) {
367
+ return
368
+ }
369
+
370
+ const duration = Math.max(1000, activeSlide.duration_ms || 5000)
371
+ const tickMs = 50
372
+ const stepPct = (tickMs / duration) * 100
373
+
374
+ const interval = setInterval(() => {
375
+ setProgressPct((prev) => {
376
+ if (prev + stepPct >= 100) {
377
+ return 100
378
+ }
379
+ return prev + stepPct
380
+ })
381
+ }, tickMs)
382
+
383
+ return () => clearInterval(interval)
384
+ }, [activeSlide, autoPlay, isPaused, selectedHotspot])
385
+
386
+ useEffect(() => {
387
+ if (progressPct >= 100 && activeSlide && !isPaused && !selectedHotspot) {
388
+ nextSlide()
389
+ }
390
+ }, [progressPct, activeSlide, isPaused, selectedHotspot, nextSlide])
391
+
392
+ const isGroupSeen = useCallback(
393
+ (group: StoryGroupDTO) => {
394
+ const groupSlides = group.slides ?? []
395
+ if (!groupSlides.length) return false
396
+ return groupSlides.every((s) => seenSlideIds.has(s.id))
397
+ },
398
+ [seenSlideIds]
399
+ )
400
+
401
+ return {
402
+ isOpen: activeGroup !== null,
403
+ activeGroup,
404
+ activeGroupIndex,
405
+ activeSlide,
406
+ activeSlideIndex,
407
+ progressPct,
408
+ isPaused,
409
+ setIsPaused,
410
+ selectedHotspot,
411
+ setSelectedHotspot,
412
+ openGroup,
413
+ closePlayer,
414
+ nextSlide,
415
+ prevSlide,
416
+ isGroupSeen,
417
+ seenSlideIds,
418
+ }
419
+ }
@@ -0,0 +1,114 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react"
2
+ import type {
3
+ StoriesMutationHookOptions,
4
+ StoriesQueryHookOptions,
5
+ } from "../modules/story/types"
6
+ import { matchesStoriesQueryKeyPrefix } from "../sdk"
7
+
8
+ type Listener = (prefixKey: readonly unknown[]) => void
9
+ const listeners = new Set<Listener>()
10
+
11
+ export function invalidateStoriesQueries(prefixKey: readonly unknown[]) {
12
+ for (const listener of listeners) {
13
+ listener(prefixKey)
14
+ }
15
+ }
16
+
17
+ export function useStoriesQuery<TData>(
18
+ queryKey: readonly unknown[],
19
+ fetcher: () => Promise<TData>,
20
+ options?: StoriesQueryHookOptions<TData>
21
+ ) {
22
+ const enabled = options?.enabled ?? true
23
+ const [data, setData] = useState<TData | undefined>(options?.initialData)
24
+ const [isLoading, setIsLoading] = useState<boolean>(
25
+ enabled && !options?.initialData
26
+ )
27
+ const [error, setError] = useState<Error | null>(null)
28
+ const fetcherRef = useRef(fetcher)
29
+ fetcherRef.current = fetcher
30
+ const serializedKey = JSON.stringify(queryKey)
31
+
32
+ const refetch = useCallback(async () => {
33
+ setIsLoading(true)
34
+ setError(null)
35
+ try {
36
+ const res = await fetcherRef.current()
37
+ setData(res)
38
+ return res
39
+ } catch (err) {
40
+ const normalized = err instanceof Error ? err : new Error(String(err))
41
+ setError(normalized)
42
+ throw normalized
43
+ } finally {
44
+ setIsLoading(false)
45
+ }
46
+ }, [])
47
+
48
+ useEffect(() => {
49
+ if (!enabled) return
50
+ void refetch().catch(() => {})
51
+ }, [enabled, serializedKey, refetch])
52
+
53
+ useEffect(() => {
54
+ if (!enabled) return
55
+ const parsedKey = JSON.parse(serializedKey) as readonly unknown[]
56
+ const listener: Listener = (prefixKey) => {
57
+ if (matchesStoriesQueryKeyPrefix(parsedKey, prefixKey)) {
58
+ void refetch().catch(() => {})
59
+ }
60
+ }
61
+ listeners.add(listener)
62
+ return () => {
63
+ listeners.delete(listener)
64
+ }
65
+ }, [enabled, serializedKey, refetch])
66
+
67
+ return {
68
+ data,
69
+ isLoading,
70
+ error,
71
+ refetch,
72
+ }
73
+ }
74
+
75
+ export function useStoriesMutation<TData, TVariables>(
76
+ mutationFn: (variables: TVariables) => Promise<TData>,
77
+ options?: StoriesMutationHookOptions<TData, TVariables>,
78
+ invalidatePrefix?: readonly unknown[]
79
+ ) {
80
+ const [data, setData] = useState<TData | null>(null)
81
+ const [isPending, setIsPending] = useState(false)
82
+ const [error, setError] = useState<Error | null>(null)
83
+
84
+ const mutateAsync = useCallback(
85
+ async (variables: TVariables) => {
86
+ setIsPending(true)
87
+ setError(null)
88
+ try {
89
+ const res = await mutationFn(variables)
90
+ setData(res)
91
+ if (invalidatePrefix) {
92
+ invalidateStoriesQueries(invalidatePrefix)
93
+ }
94
+ options?.onSuccess?.(res, variables)
95
+ return res
96
+ } catch (err) {
97
+ const normalized = err instanceof Error ? err : new Error(String(err))
98
+ setError(normalized)
99
+ options?.onError?.(normalized, variables)
100
+ throw normalized
101
+ } finally {
102
+ setIsPending(false)
103
+ }
104
+ },
105
+ [mutationFn, invalidatePrefix, options]
106
+ )
107
+
108
+ return {
109
+ data,
110
+ isPending,
111
+ error,
112
+ mutateAsync,
113
+ }
114
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export * from "./modules/story/types"
2
+ export * from "./modules/story/utils/security"
3
+ export {
4
+ default as StoriesModule,
5
+ StoriesModuleService,
6
+ StoriesService,
7
+ STORIES_MODULE,
8
+ LEGACY_STORIES_MODULE,
9
+ resolveStoriesService,
10
+ type StoryServiceType,
11
+ type IStoryService,
12
+ } from "./modules/story"
13
+ export * from "./workflows"
14
+ export * from "./sdk"