@peers-app/peers-ui 0.33.8 → 0.33.9

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,311 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AppsHome = AppsHome;
7
+ exports.homeSections = homeSections;
8
+ const jsx_runtime_1 = require("react/jsx-runtime");
9
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
10
+ const react_1 = require("react");
11
+ const sortablejs_1 = __importDefault(require("sortablejs"));
12
+ const use_observable_1 = require("../hooks/use-observable");
13
+ const use_promise_1 = require("../hooks/use-promise");
14
+ const local_network_offer_1 = require("../local-network/local-network-offer");
15
+ const package_helpers_1 = require("../screens/packages/package-helpers");
16
+ const packages_ipc_1 = require("../screens/packages/packages-ipc");
17
+ const color_mode_dropdown_1 = require("../screens/settings/color-mode-dropdown");
18
+ const tabs_state_1 = require("../tabs-layout/tabs-state");
19
+ const app_catalog_1 = require("../ui-router/app-catalog");
20
+ const routes_loader_1 = require("../ui-router/routes-loader");
21
+ const app_menu_1 = require("./app-menu");
22
+ const app_status_1 = require("./app-status");
23
+ const app_tile_1 = require("./app-tile");
24
+ const get_apps_sheet_1 = require("./get-apps-sheet");
25
+ const pinned_apps_1 = require("./pinned-apps");
26
+ const remove_app_dialog_1 = require("./remove-app-dialog");
27
+ /** Whether the System apps section is open on this device. Unset: open on desktop only. */
28
+ const showSystemAppsVar = (0, peers_sdk_1.deviceVar)("appsShowSystemApps");
29
+ /** How long after the screen opens to count the apps that can be added: after startup's work. */
30
+ const COUNT_DELAY_MS = 1500;
31
+ /**
32
+ * The Apps screen: the group's installed apps as a home screen. The user's pinned apps come
33
+ * first, in their order, then the group's other apps by name, then the built-in apps. Edit mode
34
+ * pins, unpins and reorders; each app's menu opens, pins, updates, shows details or removes it.
35
+ *
36
+ * @param isMobile - Use the phone layout: 4-column grids and folded built-in apps.
37
+ */
38
+ function AppsHome({ isMobile }) {
39
+ const catalog = (0, app_catalog_1.useAppCatalog)();
40
+ const [packages] = (0, use_observable_1.useObservable)(routes_loader_1.allPackages);
41
+ const { pins, loaded: pinsLoaded } = (0, pinned_apps_1.usePinnedApps)();
42
+ const [storedShowSystem, setShowSystem] = (0, use_observable_1.useObservable)(showSystemAppsVar);
43
+ const [mode] = (0, use_observable_1.useObservable)(color_mode_dropdown_1.colorMode);
44
+ const runtimeStates = (0, packages_ipc_1.usePackageRuntimeStates)();
45
+ const groupName = useCurrentGroupName();
46
+ const canManage = (0, use_promise_1.usePromise)(package_helpers_1.isGroupAdmin, false, []) ?? false;
47
+ const [query, setQuery] = (0, react_1.useState)("");
48
+ const [editing, setEditing] = (0, react_1.useState)(false);
49
+ const [menu, setMenu] = (0, react_1.useState)();
50
+ const [removing, setRemoving] = (0, react_1.useState)();
51
+ const [sheetOpen, setSheetOpen] = (0, react_1.useState)(false);
52
+ const toAdd = usePackagesToAdd();
53
+ const pinnedGridRef = (0, react_1.useRef)(null);
54
+ const getAppsRef = (0, react_1.useRef)(null);
55
+ const showSystem = storedShowSystem ?? !isMobile;
56
+ const searching = query.trim().length > 0;
57
+ const packagesById = (0, react_1.useMemo)(() => new Map(packages.map((pkg) => [pkg.packageId, pkg])), [packages]);
58
+ const sections = (0, react_1.useMemo)(() => homeSections(catalog, pins, query), [catalog, pins, query]);
59
+ const addableCount = new Set((toAdd.packages ?? [])
60
+ .filter((entry) => !entry.unsupportedHere)
61
+ .map((entry) => entry.packageId)).size;
62
+ const openSheet = () => {
63
+ setEditing(false);
64
+ setSheetOpen(true);
65
+ toAdd.refresh();
66
+ };
67
+ const shownPinPaths = sections.pinned.map((entry) => entry.path);
68
+ // Drag reorders the pins, in edit mode and not while searching.
69
+ const dragPinsRef = (0, react_1.useRef)(() => undefined);
70
+ dragPinsRef.current = (from, to) => {
71
+ const reordered = [...shownPinPaths];
72
+ const [moved] = reordered.splice(from, 1);
73
+ reordered.splice(to, 0, moved);
74
+ (0, pinned_apps_1.reorderPins)(shownPinPaths, reordered);
75
+ };
76
+ (0, react_1.useEffect)(() => {
77
+ const grid = pinnedGridRef.current;
78
+ if (!editing || searching || !grid)
79
+ return;
80
+ const sortable = sortablejs_1.default.create(grid, {
81
+ animation: 150,
82
+ draggable: ".peers-app-tile-wrap",
83
+ filter: ".peers-app-edit-badge",
84
+ preventOnFilter: false,
85
+ delay: 150,
86
+ delayOnTouchOnly: true,
87
+ onEnd: (event) => {
88
+ const { oldIndex, newIndex, item, from } = event;
89
+ if (oldIndex === undefined || newIndex === undefined || oldIndex === newIndex)
90
+ return;
91
+ // Put the element back, and let React render the new order from the pins.
92
+ from.removeChild(item);
93
+ from.insertBefore(item, from.children[oldIndex] ?? null);
94
+ dragPinsRef.current(oldIndex, newIndex);
95
+ },
96
+ });
97
+ return () => sortable.destroy();
98
+ }, [editing, searching, sections.pinned.length > 0]);
99
+ // Escape leaves edit mode when no menu or dialog is open.
100
+ (0, react_1.useEffect)(() => {
101
+ if (!editing || menu || removing)
102
+ return;
103
+ const onKey = (event) => {
104
+ if (event.key === "Escape")
105
+ setEditing(false);
106
+ };
107
+ document.addEventListener("keydown", onKey);
108
+ return () => document.removeEventListener("keydown", onKey);
109
+ }, [editing, menu, removing]);
110
+ const togglePin = (entry) => {
111
+ if (pins.includes(entry.path))
112
+ (0, pinned_apps_1.unpinApp)(entry.path);
113
+ else
114
+ (0, pinned_apps_1.pinApp)(entry.path);
115
+ };
116
+ const closeMenu = () => {
117
+ const focus = menu?.returnFocus;
118
+ setMenu(undefined);
119
+ focus?.focus();
120
+ };
121
+ const menuActions = (open) => {
122
+ const { entry, status } = open;
123
+ const pinned = pins.includes(entry.path);
124
+ const index = shownPinPaths.indexOf(entry.path);
125
+ const actions = [
126
+ {
127
+ key: "open",
128
+ label: "Open",
129
+ iconClassName: "bi-box-arrow-up-right",
130
+ run: () => openApp(entry),
131
+ },
132
+ {
133
+ key: "pin",
134
+ label: pinned ? "Unpin" : "Pin",
135
+ iconClassName: pinned ? "bi-pin-angle" : "bi-pin-angle-fill",
136
+ run: () => togglePin(entry),
137
+ },
138
+ ];
139
+ if (editing && index >= 0 && !searching) {
140
+ if (index > 0) {
141
+ actions.push({
142
+ key: "move-earlier",
143
+ label: "Move earlier",
144
+ iconClassName: "bi-arrow-left",
145
+ run: () => (0, pinned_apps_1.movePin)(shownPinPaths, entry.path, -1),
146
+ });
147
+ }
148
+ if (index < shownPinPaths.length - 1) {
149
+ actions.push({
150
+ key: "move-later",
151
+ label: "Move later",
152
+ iconClassName: "bi-arrow-right",
153
+ run: () => (0, pinned_apps_1.movePin)(shownPinPaths, entry.path, 1),
154
+ });
155
+ }
156
+ }
157
+ if (entry.kind === "system")
158
+ return actions;
159
+ const update = status.updatePv;
160
+ if (update) {
161
+ actions.push({
162
+ key: "update",
163
+ label: "Update",
164
+ iconClassName: "bi-arrow-repeat",
165
+ detail: `v${update.version}${update.versionTag ? ` (${update.versionTag})` : ""} is ready`,
166
+ emphasis: true,
167
+ run: () => void updatePackage(packagesById.get(entry.packageId)),
168
+ });
169
+ }
170
+ actions.push({
171
+ key: "details",
172
+ label: "Details and versions",
173
+ iconClassName: "bi-box-fill",
174
+ run: () => (0, tabs_state_1.navigateTo)(`packages/${entry.packageId}`),
175
+ });
176
+ if (canManage) {
177
+ actions.push({
178
+ key: "remove",
179
+ label: `Remove from ${groupName}…`,
180
+ iconClassName: "bi-trash3",
181
+ danger: true,
182
+ run: () => setRemoving(entry),
183
+ });
184
+ }
185
+ return actions;
186
+ };
187
+ const tile = (entry, size) => ((0, jsx_runtime_1.jsx)(app_tile_1.AppTile, { entry: entry, pkg: entry.kind === "package" ? packagesById.get(entry.packageId) : undefined, size: size, pinned: pins.includes(entry.path), editing: editing, failure: entry.kind === "package" ? (0, app_status_1.packageFailure)(runtimeStates?.[entry.packageId]) : undefined, onOpen: openApp, onMenu: (menuEntry, anchor, status, returnFocus) => setMenu({ entry: menuEntry, anchor, status, returnFocus }), onTogglePin: togglePin }, entry.path));
188
+ const nothingMatches = searching && !sections.pinned.length && !sections.more.length && !sections.system.length;
189
+ // Until the pins load, a pinned app would show under More and then jump up.
190
+ const showSections = pinsLoaded;
191
+ const systemOpen = searching || showSystem;
192
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "h-100 overflow-auto", "data-testid": "apps-home", children: [(0, jsx_runtime_1.jsxs)("div", { className: `peers-apps-home ${isMobile ? "peers-apps-phone" : ""} ${editing ? "peers-apps-editing" : ""}`, children: [(0, jsx_runtime_1.jsx)(local_network_offer_1.LocalNetworkHomeCards, {}), (0, jsx_runtime_1.jsxs)("div", { className: "peers-apps-toolbar", children: [(0, jsx_runtime_1.jsxs)("div", { className: "peers-apps-search", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-search", "aria-hidden": "true" }), (0, jsx_runtime_1.jsx)("input", { type: "search", className: "form-control", placeholder: isMobile ? "Search" : "Search apps", "aria-label": "Search apps", value: query, onChange: (event) => setQuery(event.target.value), "data-testid": "apps-search" })] }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-apps-toolbar-actions", children: [(0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn peers-apps-ghost peers-apps-pill", "aria-pressed": editing, onClick: () => setEditing(!editing), "data-testid": "apps-edit", children: editing ? "Done" : "Edit" }), (0, jsx_runtime_1.jsxs)("button", { ref: getAppsRef, type: "button", className: "btn btn-primary peers-apps-pill fw-semibold", "aria-label": addableCount ? `Get apps, ${addableCount} to add` : "Get apps", onClick: openSheet, "data-testid": "apps-get", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-plus-lg", "aria-hidden": "true" }), "Get apps", addableCount > 0 && ((0, jsx_runtime_1.jsx)("span", { className: "peers-apps-count", "aria-hidden": "true", children: addableCount }))] })] })] }), !showSections ? null : nothingMatches ? ((0, jsx_runtime_1.jsxs)("div", { className: "peers-apps-empty", children: [(0, jsx_runtime_1.jsx)("p", { className: "mb-3", children: "No installed app matches." }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn peers-apps-ghost peers-apps-pill", onClick: openSheet, "data-testid": "apps-empty-get", children: "Get apps" })] })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [sections.pinned.length > 0 ? ((0, jsx_runtime_1.jsx)("section", { ref: pinnedGridRef, "aria-label": "Pinned apps", className: "peers-apps-grid peers-apps-grid-pinned", "data-testid": "apps-pinned", children: sections.pinned.map((entry) => tile(entry, "pinned")) })) : (!searching && ((0, jsx_runtime_1.jsx)("p", { className: "peers-apps-empty", "data-testid": "apps-pinned-empty", children: editing
193
+ ? "Choose + on an app to pin it here."
194
+ : "Pin the apps you use most. Choose Edit to start." }))), sections.more.length > 0 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "peers-apps-divider", children: (0, jsx_runtime_1.jsxs)("h2", { children: ["More in ", groupName] }) }), (0, jsx_runtime_1.jsx)("section", { "aria-label": `More in ${groupName}`, className: "peers-apps-grid peers-apps-grid-more", "data-testid": "apps-more", children: sections.more.map((entry) => tile(entry, "more")) })] })), sections.system.length > 0 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [!searching && ((0, jsx_runtime_1.jsx)("div", { className: "peers-apps-system-toggle", children: (0, jsx_runtime_1.jsxs)("button", { type: "button", className: "btn peers-apps-ghost peers-apps-pill", "aria-expanded": showSystem, "aria-controls": "peers-apps-system", onClick: () => setShowSystem(!showSystem), "data-testid": "apps-system-toggle", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-gear-fill", "aria-hidden": "true" }), "System apps", (0, jsx_runtime_1.jsx)("i", { className: showSystem ? "bi-chevron-up" : "bi-chevron-down", "aria-hidden": "true" })] }) })), systemOpen && ((0, jsx_runtime_1.jsx)("section", { id: "peers-apps-system", "aria-label": "System apps", className: "peers-apps-grid-system", "data-testid": "apps-system", children: sections.system.map((entry) => tile(entry, "system")) }))] }))] }))] }), menu && ((0, jsx_runtime_1.jsx)(app_menu_1.AppMenu, { entry: menu.entry, status: menu.status, failure: menu.entry.kind === "package"
195
+ ? (0, app_status_1.packageFailure)(runtimeStates?.[menu.entry.packageId])
196
+ : undefined, actions: menuActions(menu), anchor: menu.anchor, colorMode: mode, onClose: closeMenu })), removing && ((0, jsx_runtime_1.jsx)(remove_app_dialog_1.RemoveAppDialog, { entry: removing, otherApps: catalog.filter((entry) => entry.packageId === removing.packageId && entry.path !== removing.path), groupName: groupName, onCancel: () => setRemoving(undefined), onConfirm: async () => {
197
+ await (await (0, packages_ipc_1.packagesTools)()).uninstallPackage({ packageId: removing.packageId });
198
+ setRemoving(undefined);
199
+ toAdd.refresh();
200
+ } })), sheetOpen && ((0, jsx_runtime_1.jsx)(get_apps_sheet_1.GetAppsSheet, { groupName: groupName, packages: toAdd.packages, loadError: toAdd.error, canAdd: canManage, isMobile: isMobile, onAdd: async (entry) => {
201
+ await (await (0, packages_ipc_1.packagesTools)()).installPackage({
202
+ packageId: entry.packageId,
203
+ fromDataContextId: entry.sourceDataContextId,
204
+ });
205
+ }, onClose: () => {
206
+ setSheetOpen(false);
207
+ toAdd.refresh();
208
+ },
209
+ // The toolbar's Get apps button is always here; the empty search's one can go.
210
+ fallbackFocusRef: getAppsRef }))] }));
211
+ }
212
+ /**
213
+ * Split the catalog into the home's sections. A pin whose app isn't installed is left out (and
214
+ * kept in the pins), and each app appears once: a pinned app leaves its usual section.
215
+ *
216
+ * @param catalog - Every app here, packages first, then built-in apps in order.
217
+ * @param pins - Pinned app paths, in order.
218
+ * @param query - The search; empty shows everything.
219
+ */
220
+ function homeSections(catalog, pins, query) {
221
+ const byPath = new Map(catalog.map((entry) => [entry.path, entry]));
222
+ const pinnedPaths = new Set();
223
+ const pinned = [];
224
+ for (const path of pins) {
225
+ const entry = byPath.get(path);
226
+ if (!entry || pinnedPaths.has(path))
227
+ continue;
228
+ pinnedPaths.add(path);
229
+ if ((0, app_catalog_1.appMatchesQuery)(entry, query))
230
+ pinned.push(entry);
231
+ }
232
+ const rest = catalog.filter((entry) => !pinnedPaths.has(entry.path) && (0, app_catalog_1.appMatchesQuery)(entry, query));
233
+ return {
234
+ pinned,
235
+ more: rest
236
+ .filter((entry) => entry.kind === "package")
237
+ .sort((a, b) => a.displayName.localeCompare(b.displayName)),
238
+ system: rest.filter((entry) => entry.kind === "system"),
239
+ };
240
+ }
241
+ /**
242
+ * Open an app in its tab. Mobile Settings in the React Native shell is a native screen, so it's
243
+ * handed to the shell instead.
244
+ *
245
+ * @param entry - The app.
246
+ */
247
+ function openApp(entry) {
248
+ const w = window;
249
+ if (entry.path === "mobile-settings" && typeof w.__NATIVE_THEME !== "undefined") {
250
+ if (w.ReactNativeWebView) {
251
+ w.ReactNativeWebView.postMessage(JSON.stringify({ type: "navigate", path: "mobile-settings" }));
252
+ }
253
+ else {
254
+ window.location.href = "peers://mobile-settings";
255
+ }
256
+ return;
257
+ }
258
+ (0, tabs_state_1.goToTabPath)(entry.path);
259
+ }
260
+ /**
261
+ * Select the newest release the package follows, checked again now: the menu's copy may be
262
+ * older than a version selected since it opened.
263
+ *
264
+ * @param pkg - The installed package; nothing happens without one.
265
+ */
266
+ async function updatePackage(pkg) {
267
+ if (!pkg)
268
+ return;
269
+ try {
270
+ const update = await (0, app_status_1.currentPackageUpdate)(pkg);
271
+ if (update)
272
+ await (0, package_helpers_1.activatePackageVersion)(pkg.packageId, update);
273
+ }
274
+ catch (error) {
275
+ reportError(error);
276
+ }
277
+ }
278
+ function reportError(error) {
279
+ console.error("Apps screen action failed", error);
280
+ }
281
+ /**
282
+ * The packages the user's other contexts have that this one doesn't, first read a moment after
283
+ * the screen opens and again whenever `refresh` is called.
284
+ */
285
+ function usePackagesToAdd() {
286
+ const [state, setState] = (0, react_1.useState)({});
287
+ const refresh = (0, react_1.useCallback)(() => {
288
+ (0, packages_ipc_1.packagesTools)()
289
+ .then((tools) => tools.listPackagesToAdd())
290
+ .then(({ packages }) => setState({ packages }), (error) => setState((current) => ({
291
+ ...current,
292
+ error: error instanceof Error ? error.message : String(error),
293
+ })));
294
+ }, []);
295
+ (0, react_1.useEffect)(() => {
296
+ const timer = setTimeout(refresh, COUNT_DELAY_MS);
297
+ return () => clearTimeout(timer);
298
+ }, [refresh]);
299
+ return { ...state, refresh };
300
+ }
301
+ /** The current group's name, or "Personal" in the personal context. */
302
+ function useCurrentGroupName() {
303
+ return ((0, use_promise_1.usePromise)(async () => {
304
+ const userContext = await (0, peers_sdk_1.getUserContext)();
305
+ const groupId = userContext.currentlyActiveGroupId();
306
+ if (!groupId || groupId === userContext.userId)
307
+ return "Personal";
308
+ const group = await (0, peers_sdk_1.Groups)(userContext.userDataContext).get(groupId);
309
+ return group?.name || "this group";
310
+ }, "", []) ?? "");
311
+ }
@@ -0,0 +1,52 @@
1
+ import { type IPackageToAdd } from "@peers-app/peers-sdk";
2
+ import type React from "react";
3
+ interface IGetAppsSheetProps {
4
+ /** The group (or "Personal") apps are added to. */
5
+ groupName: string;
6
+ /** Packages in the user's other contexts that this one lacks; `undefined` while loading. */
7
+ packages: readonly IPackageToAdd[] | undefined;
8
+ /** Why the list couldn't load, if it failed. */
9
+ loadError?: string;
10
+ /** Whether the user may add apps here (Admin). */
11
+ canAdd: boolean;
12
+ /** Use the phone's bottom sheet instead of the centered dialog. */
13
+ isMobile: boolean;
14
+ /** Add one: copies the version its source context runs. A rejection is shown on its row. */
15
+ onAdd: (entry: IPackageToAdd) => Promise<void>;
16
+ /** Close the sheet. */
17
+ onClose: () => void;
18
+ /** Where focus goes on close if what opened the sheet is gone by then. A stable ref. */
19
+ fallbackFocusRef?: React.RefObject<HTMLElement | null>;
20
+ }
21
+ /** One source context's packages, in the order the listing gave them. */
22
+ interface ISourceGroup {
23
+ sourceDataContextId: string;
24
+ sourceName: string;
25
+ packages: IPackageToAdd[];
26
+ }
27
+ /**
28
+ * Group packages by the context they come from, keeping the listing's order of contexts, and
29
+ * sort each group by the name its rows show (a one-app package shows its app's name).
30
+ *
31
+ * @param packages - The listing, sorted by source.
32
+ */
33
+ export declare function groupBySource(packages: readonly IPackageToAdd[]): ISourceGroup[];
34
+ /**
35
+ * How a package to add is shown: its one app's name and icon when it adds exactly one app,
36
+ * otherwise the package's name; the color the Apps screen will give it.
37
+ *
38
+ * @param entry - The package.
39
+ */
40
+ export declare function packageToAddDisplay(entry: IPackageToAdd): {
41
+ title: string;
42
+ iconClassName: string;
43
+ color: string;
44
+ noScreen: boolean;
45
+ };
46
+ /**
47
+ * The Get apps sheet: the packages the user's other groups (and Personal) use that this group
48
+ * doesn't, by group, each with an Add button. A centered dialog on desktop and a bottom sheet
49
+ * on a phone. Escape or the close button closes it; focus stays inside while it's open.
50
+ */
51
+ export declare function GetAppsSheet({ groupName, packages, loadError, canAdd, isMobile, onAdd, onClose, fallbackFocusRef, }: IGetAppsSheetProps): React.ReactPortal;
52
+ export {};
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupBySource = groupBySource;
4
+ exports.packageToAddDisplay = packageToAddDisplay;
5
+ exports.GetAppsSheet = GetAppsSheet;
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const peers_sdk_1 = require("@peers-app/peers-sdk");
8
+ const react_1 = require("react");
9
+ const react_dom_1 = require("react-dom");
10
+ /**
11
+ * Group packages by the context they come from, keeping the listing's order of contexts, and
12
+ * sort each group by the name its rows show (a one-app package shows its app's name).
13
+ *
14
+ * @param packages - The listing, sorted by source.
15
+ */
16
+ function groupBySource(packages) {
17
+ const groups = [];
18
+ for (const entry of packages) {
19
+ let group = groups.find((each) => each.sourceDataContextId === entry.sourceDataContextId);
20
+ if (!group) {
21
+ group = {
22
+ sourceDataContextId: entry.sourceDataContextId,
23
+ sourceName: entry.sourceName,
24
+ packages: [],
25
+ };
26
+ groups.push(group);
27
+ }
28
+ group.packages.push(entry);
29
+ }
30
+ for (const group of groups) {
31
+ group.packages.sort((a, b) => packageToAddDisplay(a).title.localeCompare(packageToAddDisplay(b).title));
32
+ }
33
+ return groups;
34
+ }
35
+ /**
36
+ * How a package to add is shown: its one app's name and icon when it adds exactly one app,
37
+ * otherwise the package's name; the color the Apps screen will give it.
38
+ *
39
+ * @param entry - The package.
40
+ */
41
+ function packageToAddDisplay(entry) {
42
+ const [first] = entry.appNavs;
43
+ const single = entry.appNavs.length === 1 ? first : undefined;
44
+ return {
45
+ title: single ? single.displayName || single.name : entry.name,
46
+ iconClassName: first?.iconClassName || "bi-box-seam",
47
+ color: (0, peers_sdk_1.resolveAppColor)(entry.packageId, first ?? { name: entry.name }),
48
+ noScreen: entry.appNavs.length === 0,
49
+ };
50
+ }
51
+ /**
52
+ * The Get apps sheet: the packages the user's other groups (and Personal) use that this group
53
+ * doesn't, by group, each with an Add button. A centered dialog on desktop and a bottom sheet
54
+ * on a phone. Escape or the close button closes it; focus stays inside while it's open.
55
+ */
56
+ function GetAppsSheet({ groupName, packages, loadError, canAdd, isMobile, onAdd, onClose, fallbackFocusRef, }) {
57
+ const dialogRef = (0, react_1.useRef)(null);
58
+ const closeRef = (0, react_1.useRef)(null);
59
+ // Keyed by source and package: the same package can be listed under two groups.
60
+ const [adds, setAdds] = (0, react_1.useState)({});
61
+ (0, react_1.useEffect)(() => {
62
+ // Give focus back to what opened the sheet (a Get apps button) when it closes. An add can
63
+ // remove that button (search's empty state), so then focus goes to the fallback.
64
+ const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null;
65
+ closeRef.current?.focus();
66
+ return () => (opener?.isConnected ? opener : fallbackFocusRef?.current)?.focus();
67
+ }, [fallbackFocusRef]);
68
+ const onKeyDown = (event) => {
69
+ if (event.key === "Escape") {
70
+ event.preventDefault();
71
+ onClose();
72
+ return;
73
+ }
74
+ if (event.key !== "Tab")
75
+ return;
76
+ const focusable = Array.from(dialogRef.current?.querySelectorAll("button:not(:disabled)") ?? []);
77
+ if (!focusable.length)
78
+ return;
79
+ const first = focusable[0];
80
+ const last = focusable[focusable.length - 1];
81
+ if (event.shiftKey && document.activeElement === first) {
82
+ event.preventDefault();
83
+ last.focus();
84
+ }
85
+ else if (!event.shiftKey && document.activeElement === last) {
86
+ event.preventDefault();
87
+ first.focus();
88
+ }
89
+ };
90
+ // A package added from one group counts as installed under every group that lists it.
91
+ const addedIds = new Set(Object.entries(adds)
92
+ .filter(([, state]) => state.status === "added")
93
+ .map(([key]) => key.split("/")[1]));
94
+ const add = async (entry) => {
95
+ const key = `${entry.sourceDataContextId}/${entry.packageId}`;
96
+ // The Add button becomes disabled, so keep keyboard focus on an enabled control.
97
+ closeRef.current?.focus();
98
+ setAdds((current) => ({ ...current, [key]: { status: "adding" } }));
99
+ try {
100
+ await onAdd(entry);
101
+ // Success removes every copy of this package's Add button, including one the user
102
+ // may have focused under another source while the install was running.
103
+ if (document.activeElement?.getAttribute("data-package-id") === entry.packageId) {
104
+ closeRef.current?.focus();
105
+ }
106
+ setAdds((current) => ({ ...current, [key]: { status: "added" } }));
107
+ }
108
+ catch (error) {
109
+ const message = error instanceof Error ? error.message : String(error);
110
+ setAdds((current) => ({ ...current, [key]: { status: "failed", error: message } }));
111
+ }
112
+ };
113
+ const groups = packages ? groupBySource(packages) : [];
114
+ return (0, react_dom_1.createPortal)(
115
+ // biome-ignore lint/a11y/noStaticElementInteractions: a pointer-only backdrop; Escape closes
116
+ (0, jsx_runtime_1.jsx)("div", { className: `peers-get-apps-backdrop ${isMobile ? "peers-get-apps-phone" : ""}`, onMouseDown: (event) => {
117
+ if (event.target === event.currentTarget)
118
+ onClose();
119
+ }, children: (0, jsx_runtime_1.jsxs)("div", { ref: dialogRef, className: "peers-get-apps text-body", role: "dialog", "aria-modal": "true", "aria-labelledby": "peers-get-apps-title", onKeyDown: onKeyDown, "data-testid": "get-apps-sheet", children: [isMobile && (0, jsx_runtime_1.jsx)("span", { className: "peers-get-apps-grabber", "aria-hidden": "true" }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-head", children: [(0, jsx_runtime_1.jsxs)("h2", { id: "peers-get-apps-title", children: ["Get apps for ", groupName] }), (0, jsx_runtime_1.jsx)("button", { ref: closeRef, type: "button", className: "peers-get-apps-close", "aria-label": "Close", onClick: onClose, "data-testid": "get-apps-close", children: (0, jsx_runtime_1.jsx)("i", { className: "bi-x-lg", "aria-hidden": "true" }) })] }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-body", children: [(0, jsx_runtime_1.jsx)("p", { className: "peers-get-apps-intro", children: "Used in your other groups. Adding one copies the version that group runs." }), !canAdd && ((0, jsx_runtime_1.jsxs)("p", { className: "peers-get-apps-intro", "data-testid": "get-apps-admin-only", children: ["Only an admin of ", groupName, " can add apps here."] })), loadError ? ((0, jsx_runtime_1.jsx)("div", { className: "alert alert-danger py-2 px-3 small", role: "alert", children: loadError })) : !packages ? ((0, jsx_runtime_1.jsx)("div", { className: "text-center py-4", children: (0, jsx_runtime_1.jsx)("output", { className: "spinner-border spinner-border-sm text-secondary", children: (0, jsx_runtime_1.jsx)("span", { className: "visually-hidden", children: "Loading" }) }) })) : groups.length === 0 ? ((0, jsx_runtime_1.jsxs)("p", { className: "peers-apps-empty", "data-testid": "get-apps-empty", children: [groupName, " already has every app your other groups use."] })) : (groups.map((group) => ((0, jsx_runtime_1.jsxs)("section", { className: "peers-get-apps-source", "aria-labelledby": `peers-get-apps-${group.sourceDataContextId}`, children: [(0, jsx_runtime_1.jsxs)("h3", { id: `peers-get-apps-${group.sourceDataContextId}`, children: [(0, jsx_runtime_1.jsx)("i", { className: group.sourceName === "Personal" ? "bi-person-fill" : "bi-people-fill", "aria-hidden": "true" }), group.sourceName] }), group.packages.map((entry) => {
120
+ const key = `${entry.sourceDataContextId}/${entry.packageId}`;
121
+ const state = addedIds.has(entry.packageId)
122
+ ? { status: "added" }
123
+ : adds[key];
124
+ return ((0, jsx_runtime_1.jsx)(PackageRow, { entry: entry, state: state, canAdd: canAdd, onAdd: () => void add(entry) }, key));
125
+ })] }, group.sourceDataContextId))))] })] }) }), document.body);
126
+ }
127
+ function PackageRow({ entry, state, canAdd, onAdd, }) {
128
+ const display = packageToAddDisplay(entry);
129
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-row", "data-testid": `get-apps-row-${entry.packageId}`, children: [(0, jsx_runtime_1.jsx)("span", { className: `peers-app-icon peers-app-color-${display.color}`, "aria-hidden": "true", children: (0, jsx_runtime_1.jsx)("i", { className: display.iconClassName }) }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-text", children: [(0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-name", children: display.title }), (0, jsx_runtime_1.jsxs)("div", { className: "peers-get-apps-desc", children: [display.noScreen ? (0, jsx_runtime_1.jsx)("span", { className: "peers-get-apps-tag", children: "No screen" }) : null, entry.description] }), state?.status === "failed" && ((0, jsx_runtime_1.jsx)("div", { className: "small text-danger mt-1", role: "alert", children: state.error }))] }), (0, jsx_runtime_1.jsx)("div", { className: "peers-get-apps-action", "aria-live": "polite", children: state?.status === "added" ? ((0, jsx_runtime_1.jsxs)("span", { className: "peers-get-apps-done", children: [(0, jsx_runtime_1.jsx)("i", { className: "bi-check-lg", "aria-hidden": "true" }), "Installed"] })) : entry.unsupportedHere ? ((0, jsx_runtime_1.jsx)("span", { className: "peers-get-apps-note", children: "Not on this device" })) : canAdd ? ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn peers-get-apps-add", onClick: onAdd, disabled: state?.status === "adding", "aria-label": `Add ${display.title}`, "data-package-id": entry.packageId, "data-testid": `get-apps-add-${entry.packageId}`, children: state?.status === "adding" ? "Adding…" : "Add" })) : null })] }));
130
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * The app path a stored recent path belongs to: a built-in app's first segment, or a package's
3
+ * `package-nav/<packageId>/<nav>`. Legacy identity routes map onto Identity, and the routes of
4
+ * removed built-in apps name nothing.
5
+ *
6
+ * @param path - A path from the old recents list, possibly a deep link or a legacy alias.
7
+ * @returns The app's path, or `undefined` when the path names no app.
8
+ */
9
+ export declare function appPathOfRecent(path: string): string | undefined;
10
+ /**
11
+ * The pins a group starts with when they were never set: the first four distinct apps of the
12
+ * old recents list.
13
+ *
14
+ * @param recents - The old recents, newest first.
15
+ */
16
+ export declare function pinsFromRecents(recents: readonly string[]): string[];
17
+ /**
18
+ * Load the stored pins and follow changes made on the user's other devices. Safe to call more
19
+ * than once.
20
+ */
21
+ export declare function pinnedAppsReady(): Promise<void>;
22
+ /**
23
+ * The pins in effect: the stored list, or, until one is stored, the pins seeded from the old
24
+ * recents. The seed isn't written, so a device that hasn't synced yet can't replace pins set on
25
+ * another device.
26
+ */
27
+ export declare function currentPins(): string[];
28
+ /**
29
+ * The pinned app paths in order, including apps that aren't installed now (the screen hides
30
+ * those, and they come back if the app is installed again). `loaded` is false until the stored
31
+ * pins have loaded; `pins` stays empty until then so an early recents load can't show the seed.
32
+ */
33
+ export declare function usePinnedApps(): {
34
+ pins: string[];
35
+ loaded: boolean;
36
+ };
37
+ /**
38
+ * Pin an app at the end of the list. Pinning a pinned app changes nothing. Before the stored
39
+ * pins have loaded, this waits for them.
40
+ *
41
+ * @param path - The app's canonical path.
42
+ */
43
+ export declare function pinApp(path: string): void;
44
+ /**
45
+ * Unpin an app. Before the stored pins have loaded, this waits for them.
46
+ *
47
+ * @param path - The app's canonical path.
48
+ */
49
+ export declare function unpinApp(path: string): void;
50
+ /**
51
+ * Put the shown pins in a new order. Pins that aren't shown (apps not installed now) keep their
52
+ * places in the stored list. Before the stored pins have loaded, this waits for them.
53
+ *
54
+ * @param shown - The pins on screen, in their current order.
55
+ * @param reordered - The same pins in the new order.
56
+ */
57
+ export declare function reorderPins(shown: readonly string[], reordered: readonly string[]): void;
58
+ /**
59
+ * Replace the shown pins' slots in `pins` with `reordered`, leaving the other pins in place.
60
+ *
61
+ * @param pins - Every pin, in stored order.
62
+ * @param shown - The pins on screen, in their current order; each one is in `pins`.
63
+ * @param reordered - `shown` in its new order.
64
+ */
65
+ export declare function withShownReordered(pins: readonly string[], shown: readonly string[], reordered: readonly string[]): string[];
66
+ /**
67
+ * Move a shown pin one place earlier or later among the shown pins.
68
+ *
69
+ * @param shown - The pins on screen, in order.
70
+ * @param path - The pin to move.
71
+ * @param step - -1 for earlier, 1 for later.
72
+ */
73
+ export declare function movePin(shown: readonly string[], path: string, step: -1 | 1): void;