Skip to content

Commit 4d105eb

Browse files
authored
refactor(ui): share board provider helpers (#115082)
1 parent c1dcabc commit 4d105eb

4 files changed

Lines changed: 23 additions & 49 deletions

File tree

‎ui/src/lib/board/gateway-provider.ts‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import type {
99
import type { GatewayBrowserClient } from "../../api/gateway.ts";
1010
import { normalizeSessionKeyForUiComparison } from "../sessions/session-key.ts";
1111
import { BoardMcpAppViewCache } from "./mcp-app-view-cache.ts";
12+
import { emptyBoardSnapshot, normalizeBoardWidgetTitle } from "./provider-helpers.ts";
1213
import {
1314
EventStream,
1415
ValueSignal,
1516
type BoardEventStream,
1617
type BoardSnapshotSignal,
1718
} from "./provider-signals.ts";
18-
import type { BoardProvider } from "./provider-types.ts";
19+
import type { BoardPinMcpAppInput, BoardPinWidgetInput, BoardProvider } from "./provider-types.ts";
1920
import type { BoardWidgetAppViewState } from "./view-types.ts";
2021
import { canvasWidgetNameForDocument, mcpAppWidgetNameForViewId } from "./widget-names.ts";
2122
import {
@@ -24,24 +25,6 @@ import {
2425
} from "./widget-ticket-lifetime.ts";
2526

2627
type BoardGatewayClient = Pick<GatewayBrowserClient, "request" | "addEventListener">;
27-
type BoardPinPlacement = {
28-
title?: string;
29-
name?: string;
30-
tabId?: string;
31-
size?: "sm" | "md" | "lg" | "xl" | "full";
32-
after?: string;
33-
};
34-
type BoardPinWidgetInput = BoardPinPlacement & { docId: string };
35-
type BoardPinMcpAppInput = BoardPinPlacement & { viewId: string };
36-
37-
function emptySnapshot(sessionKey: string): BoardSnapshot {
38-
return { sessionKey, revision: 0, tabs: [], widgets: [] };
39-
}
40-
41-
function boardWidgetTitle(title: string | undefined): string | undefined {
42-
const normalized = title?.trim() ?? "";
43-
return normalized ? Array.from(normalized).slice(0, 80).join("") : undefined;
44-
}
4528

4629
export class GatewayBoardProvider implements BoardProvider {
4730
readonly snapshot$: BoardSnapshotSignal<BoardSnapshot>;
@@ -70,7 +53,7 @@ export class GatewayBoardProvider implements BoardProvider {
7053
public canMutate = true,
7154
public canGrant = true,
7255
) {
73-
this.snapshotSignal = new ValueSignal(emptySnapshot(sessionKey));
56+
this.snapshotSignal = new ValueSignal(emptyBoardSnapshot(sessionKey));
7457
this.snapshot$ = this.snapshotSignal;
7558
this.events = this.eventStream;
7659
this.client = client;
@@ -111,7 +94,7 @@ export class GatewayBoardProvider implements BoardProvider {
11194
this.changedWidgets.clear();
11295
this.appViews.clear();
11396
this.snapshotLoaded = false;
114-
this.snapshotSignal.set(emptySnapshot(this.sessionKey));
97+
this.snapshotSignal.set(emptyBoardSnapshot(this.sessionKey));
11598
this.subscribe(client);
11699
if (connected) {
117100
void this.activate();
@@ -166,7 +149,7 @@ export class GatewayBoardProvider implements BoardProvider {
166149

167150
async pinWidget(input: BoardPinWidgetInput): Promise<void> {
168151
const name = input.name ?? canvasWidgetNameForDocument(input.docId);
169-
const title = boardWidgetTitle(input.title);
152+
const title = normalizeBoardWidgetTitle(input.title);
170153
await this.mutate(
171154
"board.widget.put",
172155
{
@@ -190,7 +173,7 @@ export class GatewayBoardProvider implements BoardProvider {
190173

191174
async pinMcpApp(input: BoardPinMcpAppInput): Promise<void> {
192175
const name = input.name ?? mcpAppWidgetNameForViewId(input.viewId);
193-
const title = boardWidgetTitle(input.title);
176+
const title = normalizeBoardWidgetTitle(input.title);
194177
await this.mutate(
195178
"board.widget.put",
196179
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { BoardSnapshot } from "@openclaw/gateway-protocol";
2+
3+
export function emptyBoardSnapshot(sessionKey: string): BoardSnapshot {
4+
return { sessionKey, revision: 0, tabs: [], widgets: [] };
5+
}
6+
7+
export function normalizeBoardWidgetTitle(title: string | undefined): string | undefined {
8+
const normalized = title?.trim() ?? "";
9+
return normalized ? Array.from(normalized).slice(0, 80).join("") : undefined;
10+
}

‎ui/src/lib/board/provider-types.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type BoardPinPlacement = {
1010
after?: string;
1111
};
1212

13-
type BoardPinWidgetInput = BoardPinPlacement & { docId: string };
14-
type BoardPinMcpAppInput = BoardPinPlacement & { viewId: string };
13+
export type BoardPinWidgetInput = BoardPinPlacement & { docId: string };
14+
export type BoardPinMcpAppInput = BoardPinPlacement & { viewId: string };
1515

1616
export type BoardProvider = {
1717
readonly sessionKey: string;

‎ui/src/lib/board/provider.ts‎

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
} from "../sessions/session-key.ts";
1313
import { GatewayBoardProvider } from "./gateway-provider.ts";
1414
import { applyMockBoardOp, normalizeMockBoardSnapshot } from "./mock-ops.ts";
15+
import { emptyBoardSnapshot, normalizeBoardWidgetTitle } from "./provider-helpers.ts";
1516
import {
1617
EventStream,
1718
ValueSignal,
1819
type BoardEventStream,
1920
type BoardSnapshotSignal,
2021
} from "./provider-signals.ts";
21-
import type { BoardProvider } from "./provider-types.ts";
22+
import type { BoardPinMcpAppInput, BoardPinWidgetInput, BoardProvider } from "./provider-types.ts";
2223
import type { BoardWidgetAppViewState } from "./view-types.ts";
2324
import { canvasWidgetNameForDocument, mcpAppWidgetNameForViewId } from "./widget-names.ts";
2425
export type { BoardCommandEvent };
@@ -29,26 +30,6 @@ export { GatewayBoardProvider } from "./gateway-provider.ts";
2930

3031
type BoardGatewayClient = Pick<GatewayBrowserClient, "request" | "addEventListener">;
3132

32-
type BoardPinPlacement = {
33-
title?: string;
34-
name?: string;
35-
tabId?: string;
36-
size?: "sm" | "md" | "lg" | "xl" | "full";
37-
after?: string;
38-
};
39-
40-
type BoardPinWidgetInput = BoardPinPlacement & { docId: string };
41-
type BoardPinMcpAppInput = BoardPinPlacement & { viewId: string };
42-
43-
function emptySnapshot(sessionKey: string): BoardSnapshot {
44-
return { sessionKey, revision: 0, tabs: [], widgets: [] };
45-
}
46-
47-
function boardWidgetTitle(title: string | undefined): string | undefined {
48-
const normalized = title?.trim() ?? "";
49-
return normalized ? Array.from(normalized).slice(0, 80).join("") : undefined;
50-
}
51-
5233
function mockSnapshot(sessionKey: string): BoardSnapshot {
5334
return {
5435
sessionKey,
@@ -113,7 +94,7 @@ class NullProvider implements BoardProvider {
11394
readonly events: BoardEventStream<BoardCommandEvent> = new EventStream<BoardCommandEvent>();
11495

11596
constructor(readonly sessionKey = "") {
116-
this.snapshot$ = new ValueSignal(emptySnapshot(sessionKey));
97+
this.snapshot$ = new ValueSignal(emptyBoardSnapshot(sessionKey));
11798
}
11899

119100
async applyOps(_ops: BoardOp[]): Promise<void> {}
@@ -185,7 +166,7 @@ class MockBoardProvider implements BoardProvider {
185166
async pinWidget(input: BoardPinWidgetInput): Promise<void> {
186167
const snapshot = this.snapshotSignal.value;
187168
const name = input.name ?? canvasWidgetNameForDocument(input.docId);
188-
const title = boardWidgetTitle(input.title);
169+
const title = normalizeBoardWidgetTitle(input.title);
189170
const tabId = input.tabId ?? snapshot.tabs[0]?.tabId ?? "main";
190171
const tabs = snapshot.tabs.length
191172
? snapshot.tabs
@@ -219,7 +200,7 @@ class MockBoardProvider implements BoardProvider {
219200
async pinMcpApp(input: BoardPinMcpAppInput): Promise<void> {
220201
const snapshot = this.snapshotSignal.value;
221202
const name = input.name ?? mcpAppWidgetNameForViewId(input.viewId);
222-
const title = boardWidgetTitle(input.title);
203+
const title = normalizeBoardWidgetTitle(input.title);
223204
const tabId = input.tabId ?? snapshot.tabs[0]?.tabId ?? "main";
224205
const tabs = snapshot.tabs.length
225206
? snapshot.tabs

0 commit comments

Comments
 (0)