@startsimpli/ui 0.4.62 → 0.4.63

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startsimpli/ui",
3
- "version": "0.4.62",
3
+ "version": "0.4.63",
4
4
  "description": "Shared UI components package for StartSimpli applications",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -0,0 +1,109 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * Inline per-row review actions — the fast-triage cluster for a status table (bd
5
+ * startsim-768w.18.15). Renders the (up to) three decisions from the schema as
6
+ * compact icon buttons ✕ reject · ✓ good · ✎ edit; a click writes status AND
7
+ * team_verdict together (the coherent pair, same as ReviewDrawer) IN PLACE —
8
+ * optimistic, no drawer. The current verdict shows as the active button. Meant to
9
+ * live in a table's trailing "Actions" column; it stops click propagation so
10
+ * acting never triggers the row's drawer-open.
11
+ *
12
+ * Generic + client-injected: pass a CollectionClient + the record; defaults match
13
+ * the OGMC content schema but everything routes through resolveReviewConfig.
14
+ */
15
+ import { useMemo, useState } from 'react';
16
+ import { Check, X, PencilLine } from 'lucide-react';
17
+
18
+ import { notify } from '../toast/use-notify';
19
+ import { cn } from '../../lib/utils';
20
+ import type { CollectionClient, EntityRecord, EntityTypeDef } from './types';
21
+ import {
22
+ applyDecisionToData,
23
+ fieldStr,
24
+ resolveReviewConfig,
25
+ reviewDecisions,
26
+ type ReviewConfig,
27
+ type ReviewDecision,
28
+ type VerdictTone,
29
+ } from './review';
30
+
31
+ const ICON: Record<VerdictTone, typeof Check> = { good: Check, bad: X, neutral: PencilLine };
32
+ const ACTIVE: Record<VerdictTone, string> = {
33
+ good: 'bg-emerald-600 text-white border-emerald-600',
34
+ bad: 'bg-rose-600 text-white border-rose-600',
35
+ neutral: 'bg-amber-500 text-white border-amber-500',
36
+ };
37
+ const IDLE: Record<VerdictTone, string> = {
38
+ good: 'border-border bg-card text-emerald-600 hover:bg-emerald-50',
39
+ bad: 'border-border bg-card text-rose-600 hover:bg-rose-50',
40
+ neutral: 'border-border bg-card text-amber-600 hover:bg-amber-50',
41
+ };
42
+
43
+ // Display order: ✕ reject · ✓ good · ✎ edit — a scan-left-to-right pass/fail/fix.
44
+ const ORDER = ['reject', 'approve', 'needs_work'];
45
+
46
+ export interface InlineReviewActionsProps {
47
+ client: CollectionClient;
48
+ type: EntityTypeDef;
49
+ record: EntityRecord;
50
+ /** Field-map + transition overrides; defaults match the OGMC content schema. */
51
+ config?: ReviewConfig;
52
+ /** Fired after a successful save so the parent can refetch/invalidate. */
53
+ onSaved?: () => void;
54
+ }
55
+
56
+ export function InlineReviewActions({ client, type, record, config, onSaved }: InlineReviewActionsProps) {
57
+ const cfg = useMemo(() => resolveReviewConfig(type, config), [type, config]);
58
+ const decisions = useMemo(
59
+ () => [...reviewDecisions(cfg)].sort((a, b) => ORDER.indexOf(a.key) - ORDER.indexOf(b.key)),
60
+ [cfg],
61
+ );
62
+ const [verdict, setVerdict] = useState(() => fieldStr(record.data, cfg.verdictAttr));
63
+ const [busy, setBusy] = useState<string | null>(null);
64
+
65
+ async function apply(d: ReviewDecision) {
66
+ const prev = verdict;
67
+ setVerdict(d.verdict ?? ''); // optimistic
68
+ setBusy(d.key);
69
+ try {
70
+ await client.updateEntity(record.id, { data: applyDecisionToData(record.data, d, cfg) });
71
+ onSaved?.();
72
+ } catch (err) {
73
+ setVerdict(prev); // roll back
74
+ notify.error(err instanceof Error ? err.message : 'Could not save.');
75
+ } finally {
76
+ setBusy(null);
77
+ }
78
+ }
79
+
80
+ if (decisions.length === 0) return null;
81
+ return (
82
+ <div className="flex items-center justify-end gap-1" onClick={(e) => e.stopPropagation()}>
83
+ {decisions.map((d) => {
84
+ const Icon = ICON[d.tone];
85
+ const active = !!d.verdict && d.verdict === verdict;
86
+ return (
87
+ <button
88
+ key={d.key}
89
+ type="button"
90
+ title={d.label}
91
+ aria-label={d.label}
92
+ aria-pressed={active}
93
+ disabled={busy === d.key}
94
+ onClick={(e) => {
95
+ e.stopPropagation();
96
+ void apply(d);
97
+ }}
98
+ className={cn(
99
+ 'flex h-8 w-8 items-center justify-center rounded-md border transition disabled:opacity-50',
100
+ active ? ACTIVE[d.tone] : IDLE[d.tone],
101
+ )}
102
+ >
103
+ <Icon className="h-4 w-4" />
104
+ </button>
105
+ );
106
+ })}
107
+ </div>
108
+ );
109
+ }
@@ -2,6 +2,7 @@ import {
2
2
  deriveReviewTransitions,
3
3
  resolveReviewConfig,
4
4
  reviewDecisions,
5
+ applyDecisionToData,
5
6
  pickStatusAttr,
6
7
  shouldIgnoreShortcut,
7
8
  nextReviewIndex,
@@ -139,6 +140,34 @@ describe('reviewDecisions', () => {
139
140
  });
140
141
  });
141
142
 
143
+ describe('applyDecisionToData', () => {
144
+ const topic = type([
145
+ enumAttr('status', ['suggested', 'ready', 'rejected', 'written']),
146
+ textAttr('team_verdict'),
147
+ ]);
148
+ const cfg = resolveReviewConfig(topic);
149
+ const decisions = reviewDecisions(cfg);
150
+ const byKey = (k: string) => decisions.find((d) => d.key === k)!;
151
+
152
+ it('writes status + verdict together (camelCased), preserving other keys', () => {
153
+ const out = applyDecisionToData({ title: 'T', status: 'suggested', teamVerdict: '' }, byKey('approve'), cfg);
154
+ expect(out.status).toBe('ready');
155
+ expect(out.teamVerdict).toBe('good');
156
+ expect(out.title).toBe('T');
157
+ });
158
+
159
+ it('reject writes rejected + bad', () => {
160
+ const out = applyDecisionToData({}, byKey('reject'), cfg);
161
+ expect(out).toEqual({ status: 'rejected', teamVerdict: 'bad' });
162
+ });
163
+
164
+ it('drops a snake-case duplicate of a camelCased key', () => {
165
+ const out = applyDecisionToData({ team_verdict: 'old' }, byKey('approve'), cfg);
166
+ expect(out.teamVerdict).toBe('good');
167
+ expect(out.team_verdict).toBeUndefined();
168
+ });
169
+ });
170
+
142
171
  describe('shouldIgnoreShortcut', () => {
143
172
  it('ignores while typing in a form control', () => {
144
173
  expect(shouldIgnoreShortcut({ key: 'a', target: { tagName: 'INPUT' } })).toBe(true);
@@ -8,10 +8,12 @@
8
8
  export { TopicReviewWorkspace, type TopicReviewWorkspaceProps } from './TopicReviewWorkspace';
9
9
  export { DraftReviewWorkspace, type DraftReviewWorkspaceProps } from './DraftReviewWorkspace';
10
10
  export { ReviewDrawer, type ReviewDrawerProps } from './ReviewDrawer';
11
+ export { InlineReviewActions, type InlineReviewActionsProps } from './InlineReviewActions';
11
12
  export {
12
13
  resolveReviewConfig,
13
14
  deriveReviewTransitions,
14
15
  reviewDecisions,
16
+ applyDecisionToData,
15
17
  type ReviewDecision,
16
18
  pickStatusAttr,
17
19
  shouldIgnoreShortcut,
@@ -15,7 +15,7 @@
15
15
  * Nothing here is tenant-specific — the defaults are conventions, not requirements.
16
16
  */
17
17
  import type { AttributeDef, EntityTypeDef } from './types';
18
- import { readData } from './data';
18
+ import { readData, toCamelKey } from './data';
19
19
 
20
20
  // ---- keyboard guard (lifted so the drawer + any review surface share it) ----
21
21
 
@@ -297,6 +297,29 @@ export function reviewDecisions(r: ResolvedReview): ReviewDecision[] {
297
297
  return out;
298
298
  }
299
299
 
300
+ /**
301
+ * Merge a decision's status + verdict into a record's (camelCased) data blob and
302
+ * return the FULL next blob — the backend PATCH replaces `data`, so callers must
303
+ * send the whole thing. Pure, so both the drawer and the inline row actions apply
304
+ * a decision identically (and it's unit-tested). Null status/verdict are skipped.
305
+ */
306
+ export function applyDecisionToData(
307
+ data: Record<string, unknown> | undefined,
308
+ decision: ReviewDecision,
309
+ cfg: ResolvedReview,
310
+ ): Record<string, unknown> {
311
+ const next: Record<string, unknown> = { ...(data ?? {}) };
312
+ const set = (attr: string, value: string | null) => {
313
+ if (value == null || value === '') return;
314
+ const camel = toCamelKey(attr);
315
+ next[camel] = value;
316
+ if (camel !== attr) delete next[attr];
317
+ };
318
+ set(cfg.statusName, decision.status);
319
+ set(cfg.verdictAttr, decision.verdict);
320
+ return next;
321
+ }
322
+
300
323
  // ---- small read helpers used by the component (kept here so they're tested) ----
301
324
 
302
325
  /** String read of an attr off a record's data blob ('' when missing). */