@vicacha-devs/medusa-plugin-barcode 0.0.0-stage → 0.1.0

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 (176) hide show
  1. package/.medusa/server/src/admin/index.cjs +3960 -0
  2. package/.medusa/server/src/admin/index.js +3938 -0
  3. package/.medusa/server/src/api/admin/barcodes/[barcode]/route.js +43 -0
  4. package/.medusa/server/src/api/admin/barcodes/batch/route.js +9 -0
  5. package/.medusa/server/src/api/admin/barcodes/bulk-generate/route.js +9 -0
  6. package/.medusa/server/src/api/admin/barcodes/cache/route.js +25 -0
  7. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/apply/route.js +13 -0
  8. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/scan/route.js +13 -0
  9. package/.medusa/server/src/api/admin/barcodes/count-sessions/route.js +27 -0
  10. package/.medusa/server/src/api/admin/barcodes/create-from-barcode/route.js +9 -0
  11. package/.medusa/server/src/api/admin/barcodes/lookup/route.js +12 -0
  12. package/.medusa/server/src/api/admin/barcodes/print/route.js +11 -0
  13. package/.medusa/server/src/api/admin/barcodes/route.js +27 -0
  14. package/.medusa/server/src/api/admin/barcodes/scan/route.js +23 -0
  15. package/.medusa/server/src/api/admin/barcodes/tags/[id]/route.js +28 -0
  16. package/.medusa/server/src/api/admin/barcodes/tags/route.js +25 -0
  17. package/.medusa/server/src/api/admin/barcodes/templates/route.js +40 -0
  18. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  19. package/.medusa/server/src/api/admin/variants/barcode/[code]/route.js +28 -0
  20. package/.medusa/server/src/api/middlewares.js +73 -0
  21. package/.medusa/server/src/api/store/barcodes/[code]/route.js +28 -0
  22. package/.medusa/server/src/api/store/barcodes/[code]/verify/route.js +20 -0
  23. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  24. package/.medusa/server/src/api/store/variants/barcode/[code]/route.js +28 -0
  25. package/.medusa/server/src/api/validators.js +173 -0
  26. package/.medusa/server/src/hooks/index.js +202 -0
  27. package/.medusa/server/src/index.js +5 -0
  28. package/.medusa/server/src/jobs/purge-expired-barcode-cache.js +10 -0
  29. package/.medusa/server/src/modules/barcode/index.js +16 -0
  30. package/.medusa/server/src/modules/barcode/migrations/Migration20260522000000.js +165 -0
  31. package/.medusa/server/src/modules/barcode/models/index.js +162 -0
  32. package/.medusa/server/src/modules/barcode/operations/context.js +2 -0
  33. package/.medusa/server/src/modules/barcode/operations/data-service.js +10 -0
  34. package/.medusa/server/src/modules/barcode/operations/generation.js +33 -0
  35. package/.medusa/server/src/modules/barcode/operations/index.js +9 -0
  36. package/.medusa/server/src/modules/barcode/operations/labels.js +57 -0
  37. package/.medusa/server/src/modules/barcode/operations/lookup.js +135 -0
  38. package/.medusa/server/src/modules/barcode/operations/maintenance.js +37 -0
  39. package/.medusa/server/src/modules/barcode/operations/stock-count.js +129 -0
  40. package/.medusa/server/src/modules/barcode/operations/tags.js +298 -0
  41. package/.medusa/server/src/modules/barcode/providers/index.js +721 -0
  42. package/.medusa/server/src/modules/barcode/service.js +161 -0
  43. package/.medusa/server/src/modules/barcode/types.js +21 -0
  44. package/.medusa/server/src/modules/barcode/utils/barcode-parser.js +399 -0
  45. package/.medusa/server/src/modules/barcode/utils/label-printer.js +533 -0
  46. package/.medusa/server/src/modules/barcode/utils/normalize-product.js +443 -0
  47. package/.medusa/server/src/modules/barcode/utils/security.js +114 -0
  48. package/.medusa/server/src/modules/barcode/utils/svg-renderer.js +492 -0
  49. package/.medusa/server/src/rbac.js +130 -0
  50. package/.medusa/server/src/sdk/index.js +164 -0
  51. package/.medusa/server/src/subscribers/product-created.js +34 -0
  52. package/.medusa/server/src/types/index.js +2 -0
  53. package/.medusa/server/src/ui/index.js +138 -0
  54. package/.medusa/server/src/workflows/apply-inventory-count.js +68 -0
  55. package/.medusa/server/src/workflows/batch-barcode-import.js +153 -0
  56. package/.medusa/server/src/workflows/bulk-generate-variant-barcodes.js +143 -0
  57. package/.medusa/server/src/workflows/generate-barcode-tag.js +44 -0
  58. package/.medusa/server/src/workflows/helpers.js +15 -0
  59. package/.medusa/server/src/workflows/index.js +7 -0
  60. package/.medusa/server/src/workflows/lookup-and-create-product.js +215 -0
  61. package/.medusa/server/src/workflows/scan-inventory-count.js +15 -0
  62. package/LICENSE +21 -0
  63. package/README.md +844 -2
  64. package/package.json +134 -3
  65. package/src/__tests__/barcode.spec.ts +836 -0
  66. package/src/__tests__/medusa-shim.mjs +331 -0
  67. package/src/__tests__/register-ts-resolver.mjs +135 -0
  68. package/src/admin/README.md +134 -0
  69. package/src/admin/components/barcode-studio-header.tsx +73 -0
  70. package/src/admin/components/batch-import-section.tsx +99 -0
  71. package/src/admin/components/dynamic-qr-tag-drawer-form.tsx +230 -0
  72. package/src/admin/components/dynamic-qr-tag-form.tsx +129 -0
  73. package/src/admin/components/dynamic-qr-tags-table.tsx +136 -0
  74. package/src/admin/components/gtin-lookup-section.tsx +155 -0
  75. package/src/admin/components/index.tsx +14 -0
  76. package/src/admin/components/label-designer-section.tsx +151 -0
  77. package/src/admin/components/label-output-section.tsx +63 -0
  78. package/src/admin/components/overview-cards.tsx +98 -0
  79. package/src/admin/components/product-barcode-card.tsx +127 -0
  80. package/src/admin/components/providers-cache-table.tsx +61 -0
  81. package/src/admin/components/providers-overview-section.tsx +49 -0
  82. package/src/admin/components/rbac-guard.tsx +153 -0
  83. package/src/admin/components/stock-take-scanner-section.tsx +116 -0
  84. package/src/admin/components/stock-take-sessions-section.tsx +109 -0
  85. package/src/admin/hooks/use-barcode-studio.ts +466 -0
  86. package/src/admin/hooks/use-product-barcodes.ts +139 -0
  87. package/src/admin/i18n/README.md +130 -0
  88. package/src/admin/i18n/index.ts +36 -0
  89. package/src/admin/i18n/json/ar.json +157 -0
  90. package/src/admin/i18n/json/de.json +157 -0
  91. package/src/admin/i18n/json/en.json +157 -0
  92. package/src/admin/i18n/json/es.json +157 -0
  93. package/src/admin/i18n/json/fr.json +157 -0
  94. package/src/admin/i18n/json/it.json +157 -0
  95. package/src/admin/i18n/json/nl.json +157 -0
  96. package/src/admin/i18n/json/pl.json +157 -0
  97. package/src/admin/i18n/json/pt.json +157 -0
  98. package/src/admin/i18n/json/tr.json +157 -0
  99. package/src/admin/routes/barcodes/lookup/page.tsx +75 -0
  100. package/src/admin/routes/barcodes/page.tsx +212 -0
  101. package/src/admin/routes/barcodes/print/page.tsx +67 -0
  102. package/src/admin/routes/barcodes/providers/page.tsx +66 -0
  103. package/src/admin/routes/barcodes/stock-take/page.tsx +74 -0
  104. package/src/admin/routes/barcodes/tags/@create/page.tsx +56 -0
  105. package/src/admin/routes/barcodes/tags/page.tsx +72 -0
  106. package/src/admin/tsconfig.json +20 -0
  107. package/src/admin/vite-env.d.ts +1 -0
  108. package/src/admin/widgets/product-barcode-widget.tsx +43 -0
  109. package/src/api/README.md +117 -0
  110. package/src/api/admin/barcodes/[barcode]/route.ts +64 -0
  111. package/src/api/admin/barcodes/batch/route.ts +12 -0
  112. package/src/api/admin/barcodes/bulk-generate/route.ts +12 -0
  113. package/src/api/admin/barcodes/cache/route.ts +35 -0
  114. package/src/api/admin/barcodes/count-sessions/[id]/apply/route.ts +17 -0
  115. package/src/api/admin/barcodes/count-sessions/[id]/scan/route.ts +17 -0
  116. package/src/api/admin/barcodes/count-sessions/route.ts +43 -0
  117. package/src/api/admin/barcodes/create-from-barcode/route.ts +16 -0
  118. package/src/api/admin/barcodes/lookup/route.ts +19 -0
  119. package/src/api/admin/barcodes/print/route.ts +18 -0
  120. package/src/api/admin/barcodes/route.ts +36 -0
  121. package/src/api/admin/barcodes/scan/route.ts +32 -0
  122. package/src/api/admin/barcodes/tags/[id]/route.ts +41 -0
  123. package/src/api/admin/barcodes/tags/route.ts +36 -0
  124. package/src/api/admin/barcodes/templates/route.ts +51 -0
  125. package/src/api/admin/plugin/route.ts +5 -0
  126. package/src/api/admin/variants/barcode/[code]/route.ts +36 -0
  127. package/src/api/middlewares.ts +91 -0
  128. package/src/api/store/barcodes/[code]/route.ts +43 -0
  129. package/src/api/store/barcodes/[code]/verify/route.ts +35 -0
  130. package/src/api/store/plugin/route.ts +5 -0
  131. package/src/api/store/variants/barcode/[code]/route.ts +36 -0
  132. package/src/api/validators.ts +217 -0
  133. package/src/hooks/index.ts +270 -0
  134. package/src/index.ts +4 -0
  135. package/src/jobs/README.md +54 -0
  136. package/src/jobs/purge-expired-barcode-cache.ts +18 -0
  137. package/src/links/README.md +44 -0
  138. package/src/modules/README.md +85 -0
  139. package/src/modules/barcode/index.ts +18 -0
  140. package/src/modules/barcode/migrations/Migration20260522000000.ts +225 -0
  141. package/src/modules/barcode/models/index.ts +168 -0
  142. package/src/modules/barcode/operations/context.ts +14 -0
  143. package/src/modules/barcode/operations/data-service.ts +10 -0
  144. package/src/modules/barcode/operations/generation.ts +57 -0
  145. package/src/modules/barcode/operations/index.ts +8 -0
  146. package/src/modules/barcode/operations/labels.ts +90 -0
  147. package/src/modules/barcode/operations/lookup.ts +202 -0
  148. package/src/modules/barcode/operations/maintenance.ts +53 -0
  149. package/src/modules/barcode/operations/stock-count.ts +217 -0
  150. package/src/modules/barcode/operations/tags.ts +425 -0
  151. package/src/modules/barcode/providers/index.ts +958 -0
  152. package/src/modules/barcode/service.ts +243 -0
  153. package/src/modules/barcode/types.ts +297 -0
  154. package/src/modules/barcode/utils/barcode-parser.ts +453 -0
  155. package/src/modules/barcode/utils/label-printer.ts +641 -0
  156. package/src/modules/barcode/utils/normalize-product.ts +524 -0
  157. package/src/modules/barcode/utils/security.ts +136 -0
  158. package/src/modules/barcode/utils/svg-renderer.ts +574 -0
  159. package/src/providers/README.md +105 -0
  160. package/src/rbac.ts +144 -0
  161. package/src/sdk/index.ts +253 -0
  162. package/src/subscribers/README.md +73 -0
  163. package/src/subscribers/product-created.ts +45 -0
  164. package/src/types/index.ts +1 -0
  165. package/src/types/medusa-ui.d.ts +118 -0
  166. package/src/ui/index.tsx +311 -0
  167. package/src/workflows/README.md +56 -0
  168. package/src/workflows/apply-inventory-count.ts +115 -0
  169. package/src/workflows/batch-barcode-import.ts +218 -0
  170. package/src/workflows/bulk-generate-variant-barcodes.ts +214 -0
  171. package/src/workflows/generate-barcode-tag.ts +85 -0
  172. package/src/workflows/helpers.ts +21 -0
  173. package/src/workflows/index.ts +6 -0
  174. package/src/workflows/lookup-and-create-product.ts +298 -0
  175. package/src/workflows/scan-inventory-count.ts +45 -0
  176. package/tsconfig.json +40 -0
@@ -0,0 +1,49 @@
1
+ import { SectionRow } from "@medusajs/dashboard/components"
2
+ import { Container, Heading, StatusBadge } from "@medusajs/ui"
3
+ import { useTranslation } from "react-i18next"
4
+
5
+ import { ProvidersCacheSection } from "./providers-cache-table"
6
+
7
+ export interface ProvidersCacheTabProps {
8
+ overview: Record<string, unknown>
9
+ cacheEntries: Array<Record<string, unknown>>
10
+ onPurgeExpired: () => void
11
+ onClearAllCache: () => void
12
+ }
13
+
14
+ export const ProvidersRegistrySection = ({
15
+ overview,
16
+ }: Pick<ProvidersCacheTabProps, "overview">) => {
17
+ const { t } = useTranslation()
18
+ const providers = Array.isArray(overview.active_providers)
19
+ ? (overview.active_providers as string[])
20
+ : []
21
+
22
+ return (
23
+ <Container className="divide-y p-0">
24
+ <div className="px-6 py-4">
25
+ <Heading level="h2">{t("providersTab.providersTitle")}</Heading>
26
+ </div>
27
+ {providers.map((p) => (
28
+ <SectionRow
29
+ key={p}
30
+ title={p}
31
+ value={<StatusBadge color="green">{p}</StatusBadge>}
32
+ />
33
+ ))}
34
+ </Container>
35
+ )
36
+ }
37
+
38
+ export const ProvidersCacheTab = (props: ProvidersCacheTabProps) => {
39
+ return (
40
+ <div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
41
+ <ProvidersRegistrySection overview={props.overview} />
42
+ <ProvidersCacheSection
43
+ cacheEntries={props.cacheEntries}
44
+ onPurgeExpired={props.onPurgeExpired}
45
+ onClearAllCache={props.onClearAllCache}
46
+ />
47
+ </div>
48
+ )
49
+ }
@@ -0,0 +1,153 @@
1
+ import { Container, Heading, Text } from "@medusajs/ui"
2
+ import { useEffect, useState, type JSX, type ReactNode } from "react"
3
+
4
+ interface CachedRbacResult {
5
+ rbacActive: boolean
6
+ isSuperAdmin: boolean
7
+ permissions: string[]
8
+ fetchedAt: number
9
+ }
10
+
11
+ const CACHE_TTL_MS = 30_000
12
+ let cachedResult: CachedRbacResult | null = null
13
+ let inflightPromise: Promise<CachedRbacResult> | null = null
14
+
15
+ function matchRbacPermission(
16
+ granted: readonly string[],
17
+ required: string,
18
+ isSuperAdmin = false
19
+ ): boolean {
20
+ if (isSuperAdmin || !required) return true
21
+ if (
22
+ granted.includes("*") ||
23
+ granted.includes("*:*") ||
24
+ granted.includes(required)
25
+ ) {
26
+ return true
27
+ }
28
+ const sep = required.includes(".") ? "." : required.includes(":") ? ":" : null
29
+ if (sep) {
30
+ const ns = required.split(sep)[0]
31
+ if (granted.includes(`${ns}.*`) || granted.includes(`${ns}:*`)) {
32
+ return true
33
+ }
34
+ }
35
+ return false
36
+ }
37
+
38
+ async function fetchRbacMe(): Promise<CachedRbacResult> {
39
+ if (cachedResult && Date.now() - cachedResult.fetchedAt < CACHE_TTL_MS) {
40
+ return cachedResult
41
+ }
42
+ if (inflightPromise) return inflightPromise
43
+
44
+ inflightPromise = (async () => {
45
+ try {
46
+ const res = await fetch("/admin/rbac/me", {
47
+ credentials: "include",
48
+ headers: { Accept: "application/json" },
49
+ })
50
+ if (res.status === 404) {
51
+ cachedResult = {
52
+ rbacActive: false,
53
+ isSuperAdmin: false,
54
+ permissions: [],
55
+ fetchedAt: Date.now(),
56
+ }
57
+ return cachedResult
58
+ }
59
+ if (!res.ok) {
60
+ cachedResult = {
61
+ rbacActive: true,
62
+ isSuperAdmin: false,
63
+ permissions: [],
64
+ fetchedAt: Date.now(),
65
+ }
66
+ return cachedResult
67
+ }
68
+ const data = (await res.json()) as {
69
+ isSuperAdmin?: boolean
70
+ permissions?: string[]
71
+ }
72
+ cachedResult = {
73
+ rbacActive: true,
74
+ isSuperAdmin: Boolean(data?.isSuperAdmin),
75
+ permissions: Array.isArray(data?.permissions) ? data.permissions : [],
76
+ fetchedAt: Date.now(),
77
+ }
78
+ return cachedResult
79
+ } catch {
80
+ cachedResult = {
81
+ rbacActive: false,
82
+ isSuperAdmin: false,
83
+ permissions: [],
84
+ fetchedAt: Date.now(),
85
+ }
86
+ return cachedResult
87
+ } finally {
88
+ inflightPromise = null
89
+ }
90
+ })()
91
+
92
+ return inflightPromise
93
+ }
94
+
95
+ export function useRbacAccess() {
96
+ const [state, setState] = useState<CachedRbacResult | null>(() =>
97
+ cachedResult && Date.now() - cachedResult.fetchedAt < CACHE_TTL_MS
98
+ ? cachedResult
99
+ : null
100
+ )
101
+
102
+ useEffect(() => {
103
+ let active = true
104
+ fetchRbacMe().then((res) => {
105
+ if (active) setState(res)
106
+ })
107
+ return () => {
108
+ active = false
109
+ }
110
+ }, [])
111
+
112
+ if (!state) {
113
+ return { isLoading: true, rbacActive: false, hasPermission: () => false }
114
+ }
115
+
116
+ return {
117
+ isLoading: false,
118
+ rbacActive: state.rbacActive,
119
+ hasPermission: (permission?: string) => {
120
+ if (!state.rbacActive || !permission) return true
121
+ return matchRbacPermission(
122
+ state.permissions,
123
+ permission,
124
+ state.isSuperAdmin
125
+ )
126
+ },
127
+ }
128
+ }
129
+
130
+ export const RbacGuard = ({
131
+ permission,
132
+ children,
133
+ hideOnDenied = false,
134
+ }: {
135
+ permission: string
136
+ children: ReactNode
137
+ hideOnDenied?: boolean
138
+ }): JSX.Element | null => {
139
+ const { isLoading, hasPermission } = useRbacAccess()
140
+ if (isLoading) return null
141
+ if (!hasPermission(permission)) {
142
+ if (hideOnDenied) return null
143
+ return (
144
+ <Container className="flex flex-col gap-y-2 p-6">
145
+ <Heading level="h2">Access denied</Heading>
146
+ <Text size="small" className="text-ui-fg-subtle">
147
+ You do not have permission to view this section. ({permission})
148
+ </Text>
149
+ </Container>
150
+ )
151
+ }
152
+ return <>{children}</>
153
+ }
@@ -0,0 +1,116 @@
1
+ import { Button, Container, Heading, Input } from "@medusajs/ui"
2
+ import { useTranslation } from "react-i18next"
3
+
4
+ import type { CountSessionFormState } from "../hooks/use-barcode-studio"
5
+ import { StockTakeSessionsSection } from "./stock-take-sessions-section"
6
+
7
+ export interface WarehouseStockTakeTabProps {
8
+ form: CountSessionFormState
9
+ onFormChange: React.Dispatch<React.SetStateAction<CountSessionFormState>>
10
+ sessions: Array<Record<string, unknown>>
11
+ selectedSessionId: string | null
12
+ onSelectSession: (id: string) => void
13
+ onCreateSession: () => void
14
+ onScanDelta: (delta: number) => void
15
+ onApplySession: (id: string) => void
16
+ }
17
+
18
+ export const StockTakeControlsSection = ({
19
+ form,
20
+ onFormChange,
21
+ onCreateSession,
22
+ onScanDelta,
23
+ }: Pick<
24
+ WarehouseStockTakeTabProps,
25
+ "form" | "onFormChange" | "onCreateSession" | "onScanDelta"
26
+ >) => {
27
+ const { t } = useTranslation()
28
+
29
+ return (
30
+ <Container className="divide-y p-0">
31
+ <div className="px-6 py-4">
32
+ <Heading level="h2">{t("stockTakeTab.createTitle")}</Heading>
33
+ </div>
34
+ <div className="flex flex-col gap-3 px-6 py-4">
35
+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
36
+ <Input
37
+ value={form.name}
38
+ onChange={(e) =>
39
+ onFormChange((prev) => ({ ...prev, name: e.target.value }))
40
+ }
41
+ placeholder={t("stockTakeTab.sessionNamePlaceholder")}
42
+ />
43
+ <Input
44
+ value={form.location_id}
45
+ onChange={(e) =>
46
+ onFormChange((prev) => ({ ...prev, location_id: e.target.value }))
47
+ }
48
+ placeholder={t("stockTakeTab.locationIdPlaceholder")}
49
+ />
50
+ </div>
51
+ <div>
52
+ <Button variant="secondary" size="small" onClick={onCreateSession}>
53
+ {t("stockTakeTab.startSessionButton")}
54
+ </Button>
55
+ </div>
56
+ </div>
57
+
58
+ <div className="px-6 py-4">
59
+ <Heading level="h3">{t("stockTakeTab.scannerTitle")}</Heading>
60
+ <div className="mt-3 grid grid-cols-1 gap-2 sm:grid-cols-2">
61
+ <Input
62
+ value={form.scan_barcode}
63
+ onChange={(e) =>
64
+ onFormChange((prev) => ({
65
+ ...prev,
66
+ scan_barcode: e.target.value,
67
+ }))
68
+ }
69
+ placeholder={t("stockTakeTab.scannedBarcodePlaceholder")}
70
+ />
71
+ <Input
72
+ value={form.scan_title}
73
+ onChange={(e) =>
74
+ onFormChange((prev) => ({
75
+ ...prev,
76
+ scan_title: e.target.value,
77
+ }))
78
+ }
79
+ placeholder={t("stockTakeTab.itemTitlePlaceholder")}
80
+ />
81
+ </div>
82
+ <div className="mt-3 flex gap-2">
83
+ <Button variant="primary" size="small" onClick={() => onScanDelta(1)}>
84
+ {t("stockTakeTab.scanPlusOneButton")}
85
+ </Button>
86
+ <Button
87
+ variant="secondary"
88
+ size="small"
89
+ onClick={() => onScanDelta(-1)}
90
+ >
91
+ {t("stockTakeTab.adjustMinusOneButton")}
92
+ </Button>
93
+ </div>
94
+ </div>
95
+ </Container>
96
+ )
97
+ }
98
+
99
+ export const WarehouseStockTakeTab = (props: WarehouseStockTakeTabProps) => {
100
+ return (
101
+ <div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
102
+ <StockTakeControlsSection
103
+ form={props.form}
104
+ onFormChange={props.onFormChange}
105
+ onCreateSession={props.onCreateSession}
106
+ onScanDelta={props.onScanDelta}
107
+ />
108
+ <StockTakeSessionsSection
109
+ sessions={props.sessions}
110
+ selectedSessionId={props.selectedSessionId}
111
+ onSelectSession={props.onSelectSession}
112
+ onApplySession={props.onApplySession}
113
+ />
114
+ </div>
115
+ )
116
+ }
@@ -0,0 +1,109 @@
1
+ import { SectionRow } from "@medusajs/dashboard/components"
2
+ import { Button, Container, Heading, StatusBadge, Table } from "@medusajs/ui"
3
+ import { useTranslation } from "react-i18next"
4
+
5
+ export interface StockTakeSessionsSectionProps {
6
+ sessions: Array<Record<string, unknown>>
7
+ selectedSessionId: string | null
8
+ onSelectSession: (id: string) => void
9
+ onApplySession: (id: string) => void
10
+ }
11
+
12
+ export const StockTakeSessionsSection = ({
13
+ sessions,
14
+ selectedSessionId,
15
+ onSelectSession,
16
+ onApplySession,
17
+ }: StockTakeSessionsSectionProps) => {
18
+ const { t } = useTranslation()
19
+ const activeSession = sessions.find((s) => String(s.id) === selectedSessionId)
20
+ const activeItems = Array.isArray(activeSession?.items)
21
+ ? (activeSession.items as Array<Record<string, unknown>>)
22
+ : []
23
+
24
+ return (
25
+ <Container className="divide-y p-0">
26
+ <div className="px-6 py-4">
27
+ <Heading level="h2">
28
+ {t("stockTakeTab.sessionsTitle", { count: sessions.length })}
29
+ </Heading>
30
+ <div className="mt-3 flex flex-wrap gap-2">
31
+ {sessions.map((s) => (
32
+ <Button
33
+ key={String(s.id)}
34
+ variant={
35
+ String(s.id) === selectedSessionId ? "primary" : "secondary"
36
+ }
37
+ size="small"
38
+ onClick={() => onSelectSession(String(s.id))}
39
+ >
40
+ {String(s.name)} ({String(s.status)})
41
+ </Button>
42
+ ))}
43
+ </div>
44
+ </div>
45
+
46
+ {activeSession ? (
47
+ <>
48
+ <SectionRow
49
+ title={String(activeSession.name)}
50
+ value={
51
+ activeSession.status === "in_progress" ? (
52
+ <Button
53
+ variant="primary"
54
+ size="small"
55
+ onClick={() => onApplySession(String(activeSession.id))}
56
+ >
57
+ {t("stockTakeTab.completeApplyButton")}
58
+ </Button>
59
+ ) : (
60
+ <StatusBadge color="green">
61
+ {t("stockTakeTab.completedBadge")}
62
+ </StatusBadge>
63
+ )
64
+ }
65
+ />
66
+ <SectionRow
67
+ title={t("stockTakeTab.countedShort")}
68
+ value={t("stockTakeTab.sessionSummary", {
69
+ expected: String(activeSession.total_expected ?? 0),
70
+ counted: String(activeSession.total_counted ?? 0),
71
+ variance: String(activeSession.total_variance ?? 0),
72
+ })}
73
+ />
74
+ <Table>
75
+ <Table.Header>
76
+ <Table.Row>
77
+ <Table.HeaderCell>Barcode</Table.HeaderCell>
78
+ <Table.HeaderCell>Item</Table.HeaderCell>
79
+ <Table.HeaderCell>
80
+ {t("stockTakeTab.countedShort")}
81
+ </Table.HeaderCell>
82
+ </Table.Row>
83
+ </Table.Header>
84
+ <Table.Body>
85
+ {activeItems.map((item) => (
86
+ <Table.Row key={String(item.id)}>
87
+ <Table.Cell className="font-mono font-semibold">
88
+ {String(item.barcode)}
89
+ </Table.Cell>
90
+ <Table.Cell>{String(item.title)}</Table.Cell>
91
+ <Table.Cell>
92
+ {t("stockTakeTab.expectedShort", {
93
+ expected: String(item.expected_quantity),
94
+ })}{" "}
95
+ | {t("stockTakeTab.countedShort")}{" "}
96
+ <strong>{String(item.counted_quantity)}</strong>{" "}
97
+ {t("stockTakeTab.varianceShort", {
98
+ variance: String(item.variance),
99
+ })}
100
+ </Table.Cell>
101
+ </Table.Row>
102
+ ))}
103
+ </Table.Body>
104
+ </Table>
105
+ </>
106
+ ) : null}
107
+ </Container>
108
+ )
109
+ }