@blackbelt-technology/pi-dashboard-client-utils 0.5.3

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.
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Type contracts for `MinimalChatView`.
3
+ *
4
+ * Producer adapters (e.g. `SubagentDetailView`, `FlowAgentDetail` shims)
5
+ * convert their plugin-specific state shapes into these structural types
6
+ * before passing them to `MinimalChatView`.
7
+ *
8
+ * See change: extract-minimal-chat-view.
9
+ */
10
+ import type { ReactNode } from "react";
11
+
12
+ /** Layout mode of `MinimalChatView`. */
13
+ export type MinimalChatMode = "inline" | "popout" | "row";
14
+
15
+ /**
16
+ * Normalized status union. Producer adapters MUST map their plugin-specific
17
+ * status enum into this union; the view never sees producer-specific tokens.
18
+ *
19
+ * Mapping tables (see design §Decision 2):
20
+ * - SubagentState: created → pending, running → running, completed → complete, failed → error
21
+ * - FlowAgentState: identity (already uses these tokens)
22
+ */
23
+ export type MinimalChatStatus =
24
+ | "pending"
25
+ | "running"
26
+ | "complete"
27
+ | "error"
28
+ | "blocked";
29
+
30
+ /**
31
+ * One timeline entry. The discriminated union is the structural intersection
32
+ * of `SubagentTimelineEntry` and `FlowDetailEntry`. Producer-specific extras
33
+ * (e.g. `ts: number` on `SubagentTimelineEntry`) are dropped at the shim
34
+ * boundary.
35
+ */
36
+ export type MinimalChatEntry =
37
+ | {
38
+ kind: "tool";
39
+ toolName: string;
40
+ input: unknown;
41
+ output?: unknown;
42
+ isError?: boolean;
43
+ }
44
+ | { kind: "text"; text: string }
45
+ | { kind: "thinking"; text: string }
46
+ | { kind: "error"; text: string };
47
+
48
+ /**
49
+ * Header-right meta. Renders only when fields are present; no placeholder
50
+ * dashes for missing values.
51
+ */
52
+ export interface MinimalChatMeta {
53
+ modelName?: string;
54
+ tokens?: { input?: number; output?: number };
55
+ durationMs?: number;
56
+ }
57
+
58
+ export interface MinimalChatViewProps {
59
+ /** Header title (e.g. agent name, "code-reviewer"). */
60
+ title: string;
61
+ /** Optional read-only path / sub-title under the title (e.g. agentMdPath). */
62
+ subtitle?: string;
63
+ status: MinimalChatStatus;
64
+ entries: MinimalChatEntry[];
65
+ /** Optional header-right meta block. Rendered only when supplied. */
66
+ meta?: MinimalChatMeta;
67
+ /** Default: "inline". */
68
+ mode?: MinimalChatMode;
69
+ /** Back button handler (renders the chevron-left when set). */
70
+ onBack?: () => void;
71
+ /** Optional empty-state message override; default is mode-aware. */
72
+ emptyMessage?: string;
73
+ /** Optional footer (e.g. flow's "Summary" markdown block). */
74
+ footer?: ReactNode;
75
+ /**
76
+ * Optional live activity string. Rendered under the title in
77
+ * `inline`/`popout` modes only when status is `running`. In `row` mode it
78
+ * is shown inline next to the title.
79
+ */
80
+ activity?: string;
81
+ /**
82
+ * Optional session id. Forwarded to the registered `toolCallStep` UI
83
+ * primitive so its per-tool renderers can build session-scoped links
84
+ * (e.g. subagent popouts). When omitted, the renderers fall back to
85
+ * non-session-scoped behavior.
86
+ *
87
+ * See change: fix-flows-plugin-polish (chat-view parity).
88
+ */
89
+ sessionId?: string;
90
+ }
@@ -0,0 +1,23 @@
1
+ import { useState, useEffect } from "react";
2
+
3
+ /**
4
+ * Reactive hook that tracks a CSS media query.
5
+ * Returns true when the query matches, updates on viewport changes.
6
+ */
7
+ export function useMediaQuery(query: string): boolean {
8
+ const [matches, setMatches] = useState(() => {
9
+ if (typeof window === "undefined") return false;
10
+ return window.matchMedia(query).matches;
11
+ });
12
+
13
+ useEffect(() => {
14
+ const mql = window.matchMedia(query);
15
+ setMatches(mql.matches);
16
+
17
+ const handler = (e: MediaQueryListEvent) => setMatches(e.matches);
18
+ mql.addEventListener("change", handler);
19
+ return () => mql.removeEventListener("change", handler);
20
+ }, [query]);
21
+
22
+ return matches;
23
+ }
@@ -0,0 +1,30 @@
1
+ import React, { createContext, useContext, type ReactNode } from "react";
2
+ import { useMediaQuery } from "./useMediaQuery.js";
3
+
4
+ const MobileContext = createContext(false);
5
+
6
+ /**
7
+ * Provides isMobile boolean (viewport width < 768px OR height < 600px) to the component tree.
8
+ */
9
+ export function MobileProvider({ children }: { children: ReactNode }) {
10
+ // Comma in a CSS media query string is OR. We treat the layout as mobile
11
+ // whenever EITHER the width is < 768px OR the height is < 600px so that
12
+ // landscape phones (e.g. 844x390, 915x412) flip to the single-panel mobile
13
+ // layout instead of getting the cramped desktop two-panel one.
14
+ // See change: fix-mobile-header-and-orientation.
15
+ const isMobile = useMediaQuery("(max-width: 767px), (max-height: 599px)");
16
+ return (
17
+ <MobileContext.Provider value={isMobile}>
18
+ {children}
19
+ </MobileContext.Provider>
20
+ );
21
+ }
22
+
23
+ /**
24
+ * Returns true when the viewport width is < 768px OR the viewport height is < 600px.
25
+ * The height arm is what catches landscape phones (~390-430px tall) so they get the
26
+ * single-panel mobile layout instead of the cramped desktop two-panel one.
27
+ */
28
+ export function useMobile(): boolean {
29
+ return useContext(MobileContext);
30
+ }
@@ -0,0 +1,157 @@
1
+ import { useRef, useCallback, useState } from "react";
2
+
3
+ export interface ZoomPanState {
4
+ scale: number;
5
+ translateX: number;
6
+ translateY: number;
7
+ }
8
+
9
+ export interface UseZoomPanOptions {
10
+ minScale?: number;
11
+ maxScale?: number;
12
+ zoomStep?: number;
13
+ }
14
+
15
+ const DEFAULT_MIN = 0.5;
16
+ const DEFAULT_MAX = 4;
17
+ const DEFAULT_STEP = 1.2;
18
+ const WHEEL_SENSITIVITY = 0.002;
19
+
20
+ export function useZoomPan(options?: UseZoomPanOptions) {
21
+ const minScale = options?.minScale ?? DEFAULT_MIN;
22
+ const maxScale = options?.maxScale ?? DEFAULT_MAX;
23
+ const zoomStep = options?.zoomStep ?? DEFAULT_STEP;
24
+
25
+ const [state, setState] = useState<ZoomPanState>({ scale: 1, translateX: 0, translateY: 0 });
26
+
27
+ // Refs for drag tracking (avoid re-renders during drag)
28
+ const dragging = useRef(false);
29
+ const lastPointer = useRef({ x: 0, y: 0 });
30
+ // Pinch tracking
31
+ const pinchDist = useRef<number | null>(null);
32
+ const pinchMid = useRef({ x: 0, y: 0 });
33
+
34
+ const clampScale = useCallback(
35
+ (s: number) => Math.min(maxScale, Math.max(minScale, s)),
36
+ [minScale, maxScale],
37
+ );
38
+
39
+ /**
40
+ * Zoom centered on a point (in container-local coords).
41
+ */
42
+ const zoomAt = useCallback(
43
+ (cx: number, cy: number, factor: number) => {
44
+ setState((prev) => {
45
+ const newScale = clampScale(prev.scale * factor);
46
+ if (newScale === prev.scale) return prev;
47
+ const ratio = newScale / prev.scale;
48
+ return {
49
+ scale: newScale,
50
+ translateX: cx - ratio * (cx - prev.translateX),
51
+ translateY: cy - ratio * (cy - prev.translateY),
52
+ };
53
+ });
54
+ },
55
+ [clampScale],
56
+ );
57
+
58
+ // ── Wheel handler ──────────────────────────────────────────────────
59
+ const onWheel = useCallback(
60
+ (e: React.WheelEvent | WheelEvent) => {
61
+ e.preventDefault();
62
+ const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
63
+ const cx = e.clientX - rect.left;
64
+ const cy = e.clientY - rect.top;
65
+ const factor = Math.exp(-e.deltaY * WHEEL_SENSITIVITY);
66
+ zoomAt(cx, cy, factor);
67
+ },
68
+ [zoomAt],
69
+ );
70
+
71
+ // ── Pointer (drag) handlers ────────────────────────────────────────
72
+ const onPointerDown = useCallback((e: React.PointerEvent | PointerEvent) => {
73
+ // Only primary button
74
+ if (e.button !== 0) return;
75
+ dragging.current = true;
76
+ lastPointer.current = { x: e.clientX, y: e.clientY };
77
+ (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
78
+ }, []);
79
+
80
+ const onPointerMove = useCallback((e: React.PointerEvent | PointerEvent) => {
81
+ if (!dragging.current) return;
82
+ const dx = e.clientX - lastPointer.current.x;
83
+ const dy = e.clientY - lastPointer.current.y;
84
+ lastPointer.current = { x: e.clientX, y: e.clientY };
85
+ setState((prev) => ({
86
+ ...prev,
87
+ translateX: prev.translateX + dx,
88
+ translateY: prev.translateY + dy,
89
+ }));
90
+ }, []);
91
+
92
+ const onPointerUp = useCallback(() => {
93
+ dragging.current = false;
94
+ }, []);
95
+
96
+ // ── Touch (pinch) handler ──────────────────────────────────────────
97
+ const onTouchMove = useCallback(
98
+ (e: React.TouchEvent | TouchEvent) => {
99
+ if (e.touches.length !== 2) return;
100
+ e.preventDefault();
101
+ const t0 = e.touches[0];
102
+ const t1 = e.touches[1];
103
+ const dist = Math.hypot(t1.clientX - t0.clientX, t1.clientY - t0.clientY);
104
+ const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
105
+ const mx = (t0.clientX + t1.clientX) / 2 - rect.left;
106
+ const my = (t0.clientY + t1.clientY) / 2 - rect.top;
107
+
108
+ if (pinchDist.current != null) {
109
+ const factor = dist / pinchDist.current;
110
+ zoomAt(mx, my, factor);
111
+ }
112
+ pinchDist.current = dist;
113
+ pinchMid.current = { x: mx, y: my };
114
+ },
115
+ [zoomAt],
116
+ );
117
+
118
+ const onTouchEnd = useCallback(() => {
119
+ pinchDist.current = null;
120
+ }, []);
121
+
122
+ // ── Double-click reset ─────────────────────────────────────────────
123
+ const onDoubleClick = useCallback(() => {
124
+ setState({ scale: 1, translateX: 0, translateY: 0 });
125
+ }, []);
126
+
127
+ // ── Button controls ────────────────────────────────────────────────
128
+ const zoomIn = useCallback(() => {
129
+ setState((prev) => {
130
+ const newScale = clampScale(prev.scale * zoomStep);
131
+ return { ...prev, scale: newScale };
132
+ });
133
+ }, [clampScale, zoomStep]);
134
+
135
+ const zoomOut = useCallback(() => {
136
+ setState((prev) => {
137
+ const newScale = clampScale(prev.scale / zoomStep);
138
+ return { ...prev, scale: newScale };
139
+ });
140
+ }, [clampScale, zoomStep]);
141
+
142
+ const reset = useCallback(() => {
143
+ setState({ scale: 1, translateX: 0, translateY: 0 });
144
+ }, []);
145
+
146
+ const handlers = {
147
+ onWheel,
148
+ onPointerDown,
149
+ onPointerMove,
150
+ onPointerUp,
151
+ onTouchMove,
152
+ onTouchEnd,
153
+ onDoubleClick,
154
+ };
155
+
156
+ return { state, handlers, zoomIn, zoomOut, reset };
157
+ }