@peers-app/peers-ui 0.33.13 → 0.33.14

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/dist/index.d.ts CHANGED
@@ -26,3 +26,4 @@ export { DEFAULT_ROOT_LAYOUT_ID, getRootLayout, getRootLayouts, } from "./root-l
26
26
  export { selectedRootLayoutId, setRootLayout } from "./root-layout/root-layout-state";
27
27
  export * from "./tabs-layout/tabs-layout";
28
28
  export { activeTabId, activeTabs, closeCurrentTab, goToTabPath, TabState, updateActiveTabTitle, } from "./tabs-layout/tabs-state";
29
+ export * from "./ui-contracts";
package/dist/index.js CHANGED
@@ -71,3 +71,5 @@ Object.defineProperty(exports, "activeTabs", { enumerable: true, get: function (
71
71
  Object.defineProperty(exports, "closeCurrentTab", { enumerable: true, get: function () { return tabs_state_1.closeCurrentTab; } });
72
72
  Object.defineProperty(exports, "goToTabPath", { enumerable: true, get: function () { return tabs_state_1.goToTabPath; } });
73
73
  Object.defineProperty(exports, "updateActiveTabTitle", { enumerable: true, get: function () { return tabs_state_1.updateActiveTabTitle; } });
74
+ // UI contracts: embed another package's UI pieces by contract (docs/Packages/ui-contracts.md).
75
+ __exportStar(require("./ui-contracts"), exports);
@@ -1,4 +1,4 @@
1
- import type { IMentionLink, IMentionPlace } from "@peers-app/peers-sdk";
1
+ import { type IMentionLink, type IMentionPlace } from "@peers-app/peers-sdk";
2
2
  /**
3
3
  * The group of the document a mention is in. Mentions resolve there first, and the picker marks
4
4
  * results from other groups. Unset, the record is looked for wherever it was last found.
@@ -4,6 +4,7 @@ exports.MentionGroupContext = void 0;
4
4
  exports.useMentionPlace = useMentionPlace;
5
5
  exports.MentionChip = MentionChip;
6
6
  const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
7
8
  const react_1 = require("react");
8
9
  const use_observable_1 = require("../hooks/use-observable");
9
10
  const mentions_client_1 = require("./mentions-client");
@@ -72,5 +73,5 @@ function MentionChip(props) {
72
73
  e.preventDefault();
73
74
  open();
74
75
  }
75
- }, children: [(place?.kind ?? kind) === "user" ? ("@") : ((0, jsx_runtime_1.jsx)("i", { className: `${place?.iconClassName ?? "bi bi-link-45deg"} peers-mention-icon` })), title] }));
76
+ }, children: [(place?.kind ?? kind) === "user" ? ("@") : ((0, jsx_runtime_1.jsx)("i", { className: `${place?.iconClassName ?? "bi bi-link-45deg"} peers-mention-icon`, ...((0, peers_sdk_1.isAppColor)(place?.color) ? { "data-color": place.color } : {}) })), title] }));
76
77
  }
package/dist/theme.css CHANGED
@@ -159,6 +159,38 @@
159
159
  margin-right: 3px;
160
160
  }
161
161
 
162
+ /* A mention source's palette color (a label's, say), from its `color` setting. */
163
+ .peers-mention-icon[data-color="blue"] {
164
+ color: var(--peers-app-blue-bg);
165
+ }
166
+ .peers-mention-icon[data-color="teal"] {
167
+ color: var(--peers-app-teal-bg);
168
+ }
169
+ .peers-mention-icon[data-color="red"] {
170
+ color: var(--peers-app-red-bg);
171
+ }
172
+ .peers-mention-icon[data-color="amber"] {
173
+ color: var(--peers-app-amber-bg);
174
+ }
175
+ .peers-mention-icon[data-color="purple"] {
176
+ color: var(--peers-app-purple-bg);
177
+ }
178
+ .peers-mention-icon[data-color="orange"] {
179
+ color: var(--peers-app-orange-bg);
180
+ }
181
+ .peers-mention-icon[data-color="green"] {
182
+ color: var(--peers-app-green-bg);
183
+ }
184
+ .peers-mention-icon[data-color="pink"] {
185
+ color: var(--peers-app-pink-bg);
186
+ }
187
+ .peers-mention-icon[data-color="violet"] {
188
+ color: var(--peers-app-violet-bg);
189
+ }
190
+ .peers-mention-icon[data-color="slate"] {
191
+ color: var(--peers-app-slate-bg);
192
+ }
193
+
162
194
  .peers-mention-done {
163
195
  opacity: 0.7;
164
196
  text-decoration: line-through;
@@ -0,0 +1,28 @@
1
+ import type { IUIContractDeclaration } from "@peers-app/peers-sdk";
2
+ import type { IHostUIContractClient, IHostUIContractOptions, IPieceHandle } from "./types";
3
+ /**
4
+ * A client for a UI contract, to embed its pieces: `<Piece of={client.pieces.picker} …>`. It's
5
+ * made like an API contract client, and its group and provider apply to every piece.
6
+ *
7
+ * ```tsx
8
+ * import type { LabelPickerUI } from "../contracts/label-picker.v1"; // types only
9
+ * const labelPicker = hostUIContract<typeof LabelPickerUI>(labelPickerUIId, 1, { dataContextId: task.groupId });
10
+ * <Piece of={labelPicker.pieces.picker} props={{ itemId: task.taskId }} anchor={plus} open={open} onClose={close} />
11
+ * ```
12
+ *
13
+ * The package must consume the UI contract (`pkg.consumes(LabelPickerUI, { optional: true })`).
14
+ * Making a client loads nothing; a piece's provider UI loads when it first shows.
15
+ *
16
+ * @param contractId - The UI contract's id, or its declaration.
17
+ * @param version - The version the screen consumes (omit when passing a declaration).
18
+ * @param options - The group (default: the active one) and, for a hub, one provider.
19
+ */
20
+ export declare function hostUIContract<T extends IUIContractDeclaration<any, any, any>>(contract: T, options?: IHostUIContractOptions): IHostUIContractClient<T>;
21
+ export declare function hostUIContract<T = unknown>(contractId: string, version: number, options?: IHostUIContractOptions): IHostUIContractClient<T>;
22
+ /**
23
+ * A stable string for a handle, for effects and caches: its UI contract version, piece, provider
24
+ * and named group.
25
+ *
26
+ * @param handle - A piece handle.
27
+ */
28
+ export declare function pieceHandleKey(handle: IPieceHandle): string;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hostUIContract = hostUIContract;
4
+ exports.pieceHandleKey = pieceHandleKey;
5
+ function hostUIContract(contractOrId, versionOrOptions, maybeOptions) {
6
+ const declared = typeof contractOrId === "object" ? contractOrId : undefined;
7
+ const contractId = declared ? declared.contractId : contractOrId;
8
+ const version = declared ? declared.version : versionOrOptions;
9
+ const options = (declared ? versionOrOptions : maybeOptions);
10
+ if (typeof contractId !== "string" || !Number.isInteger(version) || version < 1) {
11
+ throw new Error("hostUIContract takes a UI contract's id and version, or its declaration");
12
+ }
13
+ const handles = new Map();
14
+ const handle = (name) => {
15
+ let found = handles.get(name);
16
+ if (!found) {
17
+ found = Object.freeze({
18
+ kind: "piece-handle",
19
+ contractId,
20
+ version,
21
+ name,
22
+ ...(options?.dataContextId === undefined ? {} : { dataContextId: options.dataContextId }),
23
+ ...(options?.providerPackageId ? { providerPackageId: options.providerPackageId } : {}),
24
+ });
25
+ handles.set(name, found);
26
+ }
27
+ return found;
28
+ };
29
+ // Piece names come from the declaration's type, so any name gets a handle; a piece the
30
+ // provider doesn't declare shows the `failed` fallback.
31
+ const pieces = new Proxy({}, {
32
+ get: (_target, name) => (typeof name === "string" ? handle(name) : undefined),
33
+ });
34
+ return Object.freeze({ contractId, version, pieces });
35
+ }
36
+ /**
37
+ * A stable string for a handle, for effects and caches: its UI contract version, piece, provider
38
+ * and named group.
39
+ *
40
+ * @param handle - A piece handle.
41
+ */
42
+ function pieceHandleKey(handle) {
43
+ const group = typeof handle.dataContextId === "string" ? handle.dataContextId : "";
44
+ return `${handle.contractId}@${handle.version}/${handle.name}|${handle.providerPackageId ?? ""}|${group}`;
45
+ }
@@ -0,0 +1,24 @@
1
+ import type { IPieceImplementation, IUIContractDeclaration, UIContractPieceNamesOf } from "@peers-app/peers-sdk";
2
+ import type React from "react";
3
+ import type { PieceComponentProps } from "./types";
4
+ /**
5
+ * Implement one piece of a UI contract the package provides, for its UI bundle. Pass the result
6
+ * in `exportUIs({ uis, pieces: [...] })` in `src/uis.ts`; the package's own screens can render
7
+ * the same component directly, so there's one copy.
8
+ *
9
+ * ```tsx
10
+ * import type { LabelPickerUI } from "../ui-contracts"; // types only
11
+ * function LabelPicker({ props, emit, close, groupId }: PieceComponentProps<typeof LabelPickerUI, "picker">) { … }
12
+ * export const PickerPiece = implementPiece<typeof LabelPickerUI, "picker">(labelPickerUIId, 1, "picker", LabelPicker);
13
+ * ```
14
+ *
15
+ * Import the UI contract's declaration as a type only, so its code stays out of the UI bundle.
16
+ *
17
+ * @param contractId - The UI contract's id.
18
+ * @param version - The UI contract version this implements (the one the package provides).
19
+ * @param name - The piece's name.
20
+ * @param component - The component: it gets the embedder's props, `emit`, `close`, and the group.
21
+ */
22
+ export declare function implementPiece<T extends IUIContractDeclaration<any, any, any>, N extends UIContractPieceNamesOf<T> = UIContractPieceNamesOf<T>>(contractId: string, version: number, name: N, component: React.ComponentType<PieceComponentProps<T, N>>): IPieceImplementation;
23
+ /** The key a piece's component is registered under: its UI contract version and its name. */
24
+ export declare function pieceKey(contractId: string, version: number, name: string): string;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.implementPiece = implementPiece;
4
+ exports.pieceKey = pieceKey;
5
+ /**
6
+ * Implement one piece of a UI contract the package provides, for its UI bundle. Pass the result
7
+ * in `exportUIs({ uis, pieces: [...] })` in `src/uis.ts`; the package's own screens can render
8
+ * the same component directly, so there's one copy.
9
+ *
10
+ * ```tsx
11
+ * import type { LabelPickerUI } from "../ui-contracts"; // types only
12
+ * function LabelPicker({ props, emit, close, groupId }: PieceComponentProps<typeof LabelPickerUI, "picker">) { … }
13
+ * export const PickerPiece = implementPiece<typeof LabelPickerUI, "picker">(labelPickerUIId, 1, "picker", LabelPicker);
14
+ * ```
15
+ *
16
+ * Import the UI contract's declaration as a type only, so its code stays out of the UI bundle.
17
+ *
18
+ * @param contractId - The UI contract's id.
19
+ * @param version - The UI contract version this implements (the one the package provides).
20
+ * @param name - The piece's name.
21
+ * @param component - The component: it gets the embedder's props, `emit`, `close`, and the group.
22
+ */
23
+ function implementPiece(contractId, version, name, component) {
24
+ if (typeof contractId !== "string" || !Number.isInteger(version) || version < 1) {
25
+ throw new Error("implementPiece takes a UI contract's id and a positive integer version");
26
+ }
27
+ if (typeof name !== "string" || !name) {
28
+ throw new Error("implementPiece takes the piece's name");
29
+ }
30
+ if (typeof component !== "function" && typeof component !== "object") {
31
+ throw new Error(`implementPiece: piece "${name}" needs a component`);
32
+ }
33
+ return Object.freeze({
34
+ kind: "piece-implementation",
35
+ contractId,
36
+ version,
37
+ name,
38
+ component: component,
39
+ });
40
+ }
41
+ /** The key a piece's component is registered under: its UI contract version and its name. */
42
+ function pieceKey(contractId, version, name) {
43
+ return `${contractId}@${version}/${name}`;
44
+ }
@@ -0,0 +1,6 @@
1
+ export { hostUIContract } from "./host-ui-contract";
2
+ export { implementPiece } from "./implement-piece";
3
+ export { type IMockPiece, mockPiece } from "./mock-piece";
4
+ export { type IPieceProps, type IPieceSize, Piece, type PieceState, usePiece, usePieceSize, } from "./piece";
5
+ export type { PopoverAnchor } from "./popover-surface";
6
+ export type { IHostUIContractClient, IHostUIContractOptions, IPieceHandle, IPieceOpenTarget, IPieceUnavailable, PieceComponentProps, PieceEmitters, PieceHandlers, PieceUnavailableReason, } from "./types";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePieceSize = exports.usePiece = exports.Piece = exports.mockPiece = exports.implementPiece = exports.hostUIContract = void 0;
4
+ // UI contracts: a package's screens embed another package's UI pieces by contract, not by
5
+ // component id. The provider implements its pieces in its UI bundle (`implementPiece`); a
6
+ // consumer embeds them (`hostUIContract`, `<Piece>`) and gets a fallback when they can't show.
7
+ // See docs/Packages/ui-contracts.md.
8
+ var host_ui_contract_1 = require("./host-ui-contract");
9
+ Object.defineProperty(exports, "hostUIContract", { enumerable: true, get: function () { return host_ui_contract_1.hostUIContract; } });
10
+ var implement_piece_1 = require("./implement-piece");
11
+ Object.defineProperty(exports, "implementPiece", { enumerable: true, get: function () { return implement_piece_1.implementPiece; } });
12
+ var mock_piece_1 = require("./mock-piece");
13
+ Object.defineProperty(exports, "mockPiece", { enumerable: true, get: function () { return mock_piece_1.mockPiece; } });
14
+ var piece_1 = require("./piece");
15
+ Object.defineProperty(exports, "Piece", { enumerable: true, get: function () { return piece_1.Piece; } });
16
+ Object.defineProperty(exports, "usePiece", { enumerable: true, get: function () { return piece_1.usePiece; } });
17
+ Object.defineProperty(exports, "usePieceSize", { enumerable: true, get: function () { return piece_1.usePieceSize; } });
@@ -0,0 +1,36 @@
1
+ import type { ContractPieceKind, IField } from "@peers-app/peers-sdk";
2
+ import type { IPieceHandle, PieceComponent } from "./types";
3
+ /** What a test can do with a stand-in piece from {@link mockPiece}. */
4
+ export interface IMockPiece {
5
+ /** The props it was last drawn with, or undefined before it drew. */
6
+ readonly props: Record<string, unknown> | undefined;
7
+ /** How many times it has drawn. */
8
+ readonly renders: number;
9
+ /** Send one of its emits to the embedder, as the real piece would. */
10
+ emit(name: string, payload?: unknown): void;
11
+ /** Ask the embedder to close it (a popover's `close`). */
12
+ close(): void;
13
+ /** Remove the stand-in. */
14
+ restore(): void;
15
+ }
16
+ /**
17
+ * Stand in for a piece in a consumer's tests: `<Piece of={handle}>` draws a small placeholder
18
+ * (`data-testid="mock-piece-<name>"`) instead of loading a provider, and the test sends the
19
+ * piece's emits and reads the props it got. Declare the emits the test sends, and the props to
20
+ * check, as the real UI contract declares them.
21
+ *
22
+ * ```tsx
23
+ * const picker = mockPiece(labelPicker.pieces.picker, { kind: "popover", emits: ["chosen"] });
24
+ * render(<TaskRow task={task} />);
25
+ * picker.emit("chosen", { labelId: "l1" });
26
+ * ```
27
+ *
28
+ * @param handle - The piece, from the consumer's UI contract client.
29
+ * @param options - Its kind, props fields, emit names, and an optional component to draw.
30
+ */
31
+ export declare function mockPiece(handle: IPieceHandle, options?: {
32
+ kind?: ContractPieceKind;
33
+ propsFields?: IField[];
34
+ emits?: string[];
35
+ component?: PieceComponent;
36
+ }): IMockPiece;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mockPiece = mockPiece;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const piece_resolution_1 = require("./piece-resolution");
6
+ /**
7
+ * Stand in for a piece in a consumer's tests: `<Piece of={handle}>` draws a small placeholder
8
+ * (`data-testid="mock-piece-<name>"`) instead of loading a provider, and the test sends the
9
+ * piece's emits and reads the props it got. Declare the emits the test sends, and the props to
10
+ * check, as the real UI contract declares them.
11
+ *
12
+ * ```tsx
13
+ * const picker = mockPiece(labelPicker.pieces.picker, { kind: "popover", emits: ["chosen"] });
14
+ * render(<TaskRow task={task} />);
15
+ * picker.emit("chosen", { labelId: "l1" });
16
+ * ```
17
+ *
18
+ * @param handle - The piece, from the consumer's UI contract client.
19
+ * @param options - Its kind, props fields, emit names, and an optional component to draw.
20
+ */
21
+ function mockPiece(handle, options = {}) {
22
+ const piece = {
23
+ name: handle.name,
24
+ kind: options.kind ?? "view",
25
+ description: "A stand-in for tests.",
26
+ propsFields: options.propsFields ?? [],
27
+ emits: (options.emits ?? []).map((name) => ({ name, description: "", payloadFields: [] })),
28
+ };
29
+ const state = { renders: 0 };
30
+ const Custom = options.component;
31
+ const Stand = (props) => {
32
+ state.props = props.props;
33
+ state.renders += 1;
34
+ state.emit = props.emit;
35
+ state.close = props.close;
36
+ return Custom ? ((0, jsx_runtime_1.jsx)(Custom, { ...props })) : ((0, jsx_runtime_1.jsx)("div", { "data-testid": `mock-piece-${handle.name}`, children: handle.name }));
37
+ };
38
+ const restore = (0, piece_resolution_1.setPieceMock)(handle.contractId, handle.version, piece, Stand);
39
+ return {
40
+ get props() {
41
+ return state.props;
42
+ },
43
+ get renders() {
44
+ return state.renders;
45
+ },
46
+ emit(name, payload) {
47
+ if (!state.emit)
48
+ throw new Error(`Piece "${handle.name}" hasn't drawn yet`);
49
+ state.emit[name]?.(payload);
50
+ },
51
+ close() {
52
+ state.close?.();
53
+ },
54
+ restore,
55
+ };
56
+ }
@@ -0,0 +1,99 @@
1
+ import { type IContractDefinition, type IContractProviderInfo, type IPeersPackageUIs } from "@peers-app/peers-sdk";
2
+ import type { PieceComponent } from "./types";
3
+ /** The active group's id, or the user's for Personal: where a piece shows when none is named. */
4
+ export declare function defaultDataContextId(): Promise<string>;
5
+ /**
6
+ * The registered providers of a contract version in a group, with which one is active and, for
7
+ * one that serves it through `alsoImplements`, the version it implements.
8
+ *
9
+ * @param dataContextId - The group, or the user's id for Personal.
10
+ * @param contractId - The UI contract's id.
11
+ * @param version - Its version.
12
+ */
13
+ export declare function listProviders(dataContextId: string, contractId: string, version: number): Promise<IContractProviderInfo[]>;
14
+ /**
15
+ * A contract version's definition as the group registered it: for a UI contract, its pieces.
16
+ *
17
+ * @param dataContextId - The group.
18
+ * @param contractId - The contract's id.
19
+ * @param version - Its version.
20
+ */
21
+ export declare function contractDefinition(dataContextId: string, contractId: string, version: number): Promise<IContractDefinition>;
22
+ /** A provider's runtime in a group, on this device: running (with its version) or why not. */
23
+ export type ProviderRuntime = {
24
+ running: true;
25
+ packageVersionId: string;
26
+ } | {
27
+ running: false;
28
+ reason: string;
29
+ };
30
+ /**
31
+ * Whether a provider runs in a group on this device, and which version. Pieces draw with the
32
+ * version that registered their UI contract there.
33
+ *
34
+ * @param dataContextId - The group.
35
+ * @param packageId - The provider.
36
+ */
37
+ export declare function providerRuntime(dataContextId: string, packageId: string): Promise<ProviderRuntime>;
38
+ /**
39
+ * Call `onChange` when which packages run in a group, or which versions, changes on this device:
40
+ * a package starting at startup registers its UI contracts, so a piece looked up before it
41
+ * was missing then. Changes that leave every package's status and version as they were are
42
+ * ignored. A new connection to the host (Electron's reconnects after sleep) ends the old
43
+ * subscription, so watching starts again on it.
44
+ *
45
+ * @param dataContextId - The group.
46
+ * @param onChange - Called after each such change.
47
+ */
48
+ export declare function watchProviders(dataContextId: string, onChange: () => void): void;
49
+ /** A package that provides a UI contract version in a group but doesn't serve it here. */
50
+ export interface IInstalledProvider {
51
+ /** Its id. */
52
+ packageId: string;
53
+ /** Its name. */
54
+ name: string;
55
+ /** Why it doesn't serve the contract here. */
56
+ reason: string;
57
+ /** Set when it provides only older versions of the UI contract. */
58
+ older?: true;
59
+ }
60
+ /**
61
+ * A package installed in a group that provides a UI contract version but doesn't run on this
62
+ * device: it waits for a contract it requires, is turned off, isn't approved here, or can't run
63
+ * on this host. Only a running package registers its UI contracts, so the catalog doesn't list
64
+ * it. This reads the group's packages and, for each one not running, the manifest this device
65
+ * stored for its selected version; a version this device never read has none, and isn't found.
66
+ * One that provides the version comes first; else one that provides an older version, marked
67
+ * `older`.
68
+ *
69
+ * @param dataContextId - The group.
70
+ * @param contractId - The UI contract's id.
71
+ * @param version - Its version.
72
+ * @param providerPackageId - Look at this package only, as a handle that names one does.
73
+ */
74
+ export declare function installedProvider(dataContextId: string, contractId: string, version: number, providerPackageId?: string): Promise<IInstalledProvider | undefined>;
75
+ /** The pieces a UI bundle's `exportUIs` call registered, by piece key. */
76
+ export declare function piecesFromExport(exported: IPeersPackageUIs | undefined): Map<string, PieceComponent>;
77
+ /**
78
+ * Record the pieces a screen's load of a UI bundle registered, so a piece of the same version
79
+ * doesn't run the bundle again. Called by the UI loader, which runs a package's bundle for the
80
+ * active group's screens.
81
+ *
82
+ * @param packageVersionId - The version whose bundle ran.
83
+ * @param uiBundleFileId - Its UI bundle's file.
84
+ * @param exported - What the bundle passed `exportUIs`.
85
+ */
86
+ export declare function recordLoadedPieces(packageVersionId: string, uiBundleFileId: string, exported: IPeersPackageUIs | undefined): void;
87
+ /**
88
+ * The pieces a package version's UI bundle registers, running the bundle once per version and
89
+ * file when no screen already did. The version's row is read in the piece's group, and in a group
90
+ * the bundle must match its hash. A bundle run for pieces registers no screens, so another
91
+ * version's pieces never replace what a screen shows.
92
+ *
93
+ * @param dataContextId - The group the piece is embedded for.
94
+ * @param packageId - The provider.
95
+ * @param packageVersionId - The version it runs there.
96
+ */
97
+ export declare function loadVersionPieces(dataContextId: string, packageId: string, packageVersionId: string): Promise<Map<string, PieceComponent>>;
98
+ /** Forget loaded bundles: for tests. */
99
+ export declare function resetPieceBundles(): void;