@asteby/metacore-runtime-react 23.7.1 → 23.8.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.
package/src/index.ts CHANGED
@@ -54,6 +54,11 @@ export {
54
54
  type CustomStageFilterOp,
55
55
  type UseCustomStagesResult,
56
56
  } from './custom-stages'
57
+ export {
58
+ useStageLayout,
59
+ type StageLayout,
60
+ type UseStageLayoutResult,
61
+ } from './stage-layout'
57
62
  export {
58
63
  DynamicView,
59
64
  resolveViewRenderer,
@@ -0,0 +1,93 @@
1
+ // Stage layout — per-org persistence of the DynamicKanban lane order. Lets a
2
+ // user drag the board's columns (declared stages, custom stages and smart
3
+ // lanes alike) into any order; the chosen order is saved server-side and
4
+ // re-applied on load (the ops backend stamps it onto `metadata.stages[].order`
5
+ // / `smart_lanes[].order`, so the board already paints ordered — this hook only
6
+ // owns the drag + the optimistic PUT).
7
+ //
8
+ // Non-intrusive by design: mirrors `useStageAutomations`. If the host wires no
9
+ // `/stage-layout` endpoint, the GET 404s, `available` stays false, and the lane
10
+ // drag simply never turns on — the kanban keeps working untouched.
11
+ //
12
+ // Contract (matches the ops backend; envelope is {success, data} → read .data):
13
+ // GET /stage-layout?model=<m> → { model, stage_order: string[] } | null
14
+ // PUT /stage-layout { model, stage_order: string[] } (full order)
15
+ // DELETE /stage-layout?model=<m> → reset to the declared order
16
+ import { useCallback, useEffect, useState } from 'react'
17
+ import { useApi } from './api-context'
18
+
19
+ export interface StageLayout {
20
+ model: string
21
+ /** The full lane order — every lane key (stages + smart lanes) in order. */
22
+ stage_order: string[]
23
+ }
24
+
25
+ export interface UseStageLayoutResult {
26
+ /** True only after a successful GET — gates the lane drag + reset affordance. */
27
+ available: boolean
28
+ /** True when a custom order is stored server-side (drives the reset affordance). */
29
+ hasCustomLayout: boolean
30
+ /** Persist the full lane order (the new order of every lane key). Throws on failure. */
31
+ save: (order: string[]) => Promise<void>
32
+ /** Drop the stored order, reverting to the declared stage order. */
33
+ reset: () => Promise<void>
34
+ }
35
+
36
+ function unwrap(res: { data: any }): any {
37
+ const body = res?.data
38
+ if (body && typeof body === 'object' && 'data' in body) return body.data
39
+ return body
40
+ }
41
+
42
+ /**
43
+ * Loads a model's saved lane order (only to learn availability + whether a
44
+ * custom order exists) and exposes save/reset. A missing endpoint degrades to
45
+ * `available: false` so the board's lane drag stays off; a real save failure
46
+ * re-throws so the caller can revert its optimistic reorder.
47
+ */
48
+ export function useStageLayout(model: string): UseStageLayoutResult {
49
+ const api = useApi()
50
+ const [available, setAvailable] = useState(false)
51
+ const [hasCustomLayout, setHasCustomLayout] = useState(false)
52
+
53
+ useEffect(() => {
54
+ let cancelled = false
55
+ api
56
+ .get(`/stage-layout?model=${encodeURIComponent(model)}`)
57
+ .then((res) => {
58
+ if (cancelled) return
59
+ setAvailable(true)
60
+ const data = unwrap(res)
61
+ const order = data?.stage_order
62
+ setHasCustomLayout(Array.isArray(order) && order.length > 0)
63
+ })
64
+ .catch(() => {
65
+ // Endpoint absent or errored — leave lane drag off.
66
+ if (!cancelled) setAvailable(false)
67
+ })
68
+ return () => {
69
+ cancelled = true
70
+ }
71
+ }, [api, model])
72
+
73
+ const save = useCallback(
74
+ async (order: string[]) => {
75
+ const res = (await api.put('/stage-layout', {
76
+ model,
77
+ stage_order: order,
78
+ })) as { data?: { success?: boolean; message?: string } }
79
+ if (res?.data && res.data.success === false) {
80
+ throw new Error(res.data.message || 'stage_layout_save_failed')
81
+ }
82
+ setHasCustomLayout(true)
83
+ },
84
+ [api, model],
85
+ )
86
+
87
+ const reset = useCallback(async () => {
88
+ await api.delete(`/stage-layout?model=${encodeURIComponent(model)}`)
89
+ setHasCustomLayout(false)
90
+ }, [api, model])
91
+
92
+ return { available, hasCustomLayout, save, reset }
93
+ }