@7365admin1/layer-common 4.28.1-staging.360 → 4.28.1-staging.362

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,160 @@
1
+ <template>
2
+ <!--
3
+ One estate's robots in the HERO look: the shared shell header (hidden when
4
+ the host draws its own, e.g. the console's drill-down), then the site
5
+ fleet overview, or one robot's detail when `robotId` is set. Every app
6
+ passes only its scope (org, site, the robot types it presents) and what
7
+ this user's role may do; the API narrows again on its own side.
8
+ -->
9
+ <RobotHero class="hero-screen">
10
+ <RobotSiteHeader
11
+ v-if="!hideHeader"
12
+ :org-name="orgName || names.clientName"
13
+ :sites="sites"
14
+ :site="site"
15
+ :types="types"
16
+ :type="type"
17
+ :connected="connected"
18
+ :faults="faults"
19
+ :role="role"
20
+ @update:site="(id) => emit('update:site', id)"
21
+ @update:type="setType"
22
+ @bell="emit('bell')"
23
+ />
24
+ <main class="hero-main">
25
+ <RobotDetail
26
+ v-if="robotId"
27
+ :key="robotId"
28
+ :robot-id="robotId"
29
+ :back-label="siteName || names.siteName ? `${siteName || names.siteName} fleet` : ''"
30
+ :can-edit="canEdit"
31
+ :can-command="canCommand"
32
+ :can-delete="canDelete"
33
+ @back="emit('back')"
34
+ @removed="onRemoved"
35
+ @status="(s) => (connected = s.connected)"
36
+ />
37
+ <RobotSiteFleet
38
+ v-else
39
+ :organization="organization"
40
+ :site="site"
41
+ :site-name="siteName"
42
+ :client-name="clientName"
43
+ :type="type"
44
+ :can-setup="canSetup"
45
+ :back-to-network="backToNetwork"
46
+ :reload-key="reloadKey"
47
+ @open="(r) => emit('open', String(r._id))"
48
+ @add="addOpen = true"
49
+ @network="emit('network')"
50
+ @status="onFleetStatus"
51
+ />
52
+ </main>
53
+
54
+ <v-dialog v-model="addOpen" width="460" persistent>
55
+ <RobotForm
56
+ v-if="addOpen"
57
+ :organization="organization"
58
+ :site="site"
59
+ :type="type"
60
+ @cancel="addOpen = false"
61
+ @created="onCreated"
62
+ />
63
+ </v-dialog>
64
+ <RobotToast v-model="toast" />
65
+ </RobotHero>
66
+ </template>
67
+
68
+ <script setup lang="ts">
69
+ import { computed, ref, watch } from "vue";
70
+ import RobotHero from "./Hero.vue";
71
+ import RobotSiteHeader from "./SiteHeader.vue";
72
+ import RobotSiteFleet from "./SiteFleet.vue";
73
+ import RobotDetail from "./Detail.vue";
74
+ import RobotForm from "./Form.vue";
75
+ import RobotToast from "./Toast.vue";
76
+ import { roleLabel, type TRobotType } from "../../utils/robot-display";
77
+
78
+ const props = withDefaults(
79
+ defineProps<{
80
+ organization: string;
81
+ site: string;
82
+ siteName?: string;
83
+ clientName?: string;
84
+ /** The header's org line ("Marina Bay Residences · MCST 3821"). */
85
+ orgName?: string;
86
+ /** The sites this person may pick; the Site select shows only with more than one. */
87
+ sites?: Array<{ _id: string; name: string }>;
88
+ /** The robot types this app presents (scope isolation): both only for Property Management. */
89
+ types?: TRobotType[];
90
+ /** The type shown now (v-model:type); defaults to the first allowed. */
91
+ type?: TRobotType;
92
+ /** One robot open, or none (the fleet). */
93
+ robotId?: string;
94
+ canSetup?: boolean;
95
+ canEdit?: boolean;
96
+ canCommand?: boolean;
97
+ canDelete?: boolean;
98
+ superAdmin?: boolean;
99
+ hideHeader?: boolean;
100
+ backToNetwork?: boolean;
101
+ }>(),
102
+ {
103
+ siteName: "",
104
+ clientName: "",
105
+ orgName: "",
106
+ sites: () => [],
107
+ types: () => ["cleaning"],
108
+ type: undefined,
109
+ robotId: "",
110
+ canSetup: false,
111
+ canEdit: false,
112
+ canCommand: false,
113
+ canDelete: false,
114
+ superAdmin: false,
115
+ hideHeader: false,
116
+ backToNetwork: false,
117
+ },
118
+ );
119
+ const emit = defineEmits<{
120
+ "update:site": [id: string];
121
+ "update:type": [type: TRobotType];
122
+ open: [id: string];
123
+ back: [];
124
+ network: [];
125
+ bell: [];
126
+ /** Live feed health + open faults, for a host drawing its own header. */
127
+ status: [status: { connected: boolean; faults: number }];
128
+ }>();
129
+
130
+ /** Never a type this app does not present, whatever the URL asked for. */
131
+ const type = computed<TRobotType>(() => (props.type && props.types.includes(props.type) ? props.type : props.types[0] ?? "cleaning"));
132
+ function setType(t: TRobotType) {
133
+ if (props.types.includes(t)) emit("update:type", t);
134
+ }
135
+
136
+ const role = computed(() => roleLabel({ superAdmin: props.superAdmin, canCommand: props.canCommand, canSetup: props.canSetup }));
137
+ const connected = ref(true);
138
+ const faults = ref(0);
139
+ const names = ref({ siteName: "", clientName: "" });
140
+ function onFleetStatus(s: { connected: boolean; faults: number; siteName: string; clientName: string }) {
141
+ connected.value = s.connected;
142
+ faults.value = s.faults;
143
+ if (s.siteName) names.value = { siteName: s.siteName, clientName: s.clientName };
144
+ }
145
+ watch([connected, faults], ([c, f]) => emit("status", { connected: c, faults: f }));
146
+
147
+ const addOpen = ref(false);
148
+ const toast = ref("");
149
+ const reloadKey = ref(0);
150
+ function onCreated() {
151
+ addOpen.value = false;
152
+ toast.value = "Robot saved · credentials hidden";
153
+ reloadKey.value++;
154
+ }
155
+ function onRemoved(name: string) {
156
+ toast.value = `${name} removed`;
157
+ reloadKey.value++;
158
+ emit("back");
159
+ }
160
+ </script>
@@ -3,7 +3,7 @@
3
3
  <span class="hero-cell__corner hero-cell__corner--tl" aria-hidden="true" />
4
4
  <span class="hero-cell__corner hero-cell__corner--br" aria-hidden="true" />
5
5
  <div class="hero-cell__label"><span class="hero-cell__square" aria-hidden="true" />{{ label }}</div>
6
- <div class="hero-cell__value" :aria-label="`${label}: ${value}`">{{ shown }}</div>
6
+ <div class="hero-cell__value" :aria-label="`${label}: ${unknown ? 'not known yet' : value}`">{{ unknown ? "—" : shown }}</div>
7
7
  <div class="hero-cell__ticks" aria-hidden="true">
8
8
  <span
9
9
  v-for="i in 20"
@@ -24,7 +24,8 @@ import { countUpValue, summaryAlert, summaryTicks } from "../../utils/robot-disp
24
24
  const props = withDefaults(
25
25
  defineProps<{
26
26
  label: string;
27
- value: number;
27
+ /** null = not known (e.g. live counts before the first sweep): shows "—", never 0. */
28
+ value: number | null;
28
29
  /** What the tick bar is a ratio of. */
29
30
  total?: number;
30
31
  /** "faults" / "offline" turn accent when above zero. */
@@ -35,12 +36,13 @@ const props = withDefaults(
35
36
  { total: 0, kind: "", split: "", duration: 900 },
36
37
  );
37
38
 
39
+ const unknown = computed(() => typeof props.value !== "number");
38
40
  const filled = computed(() => summaryTicks(props.value, props.total));
39
41
  const alert = computed(() => summaryAlert(props.kind, props.value));
40
42
 
41
43
  // The count-up and the tick reveal share one clock.
42
44
  const elapsed = ref(0);
43
- const shown = computed(() => countUpValue(props.value, elapsed.value, props.duration));
45
+ const shown = computed(() => countUpValue(props.value ?? 0, elapsed.value, props.duration));
44
46
  const progress = computed(() => Math.min(1, elapsed.value / props.duration));
45
47
 
46
48
  let frame = 0;
@@ -11,7 +11,7 @@ import RobotSummaryCell from "./SummaryCell.vue";
11
11
 
12
12
  withDefaults(
13
13
  defineProps<{
14
- cells?: Array<{ label: string; value: number; total?: number; kind?: string; split?: string }>;
14
+ cells?: Array<{ label: string; value: number | null; total?: number; kind?: string; split?: string }>;
15
15
  label?: string;
16
16
  }>(),
17
17
  { cells: () => [], label: "Fleet summary" },
@@ -49,7 +49,7 @@
49
49
  </template>
50
50
  </v-data-table-server>
51
51
 
52
- <div v-if="!loading && range.total" class="hero-table__foot">
52
+ <div v-if="!loading && range.total && (!pagedFoot || range.pages > 1)" class="hero-table__foot">
53
53
  <span>{{ range.text }}</span>
54
54
  <div v-if="range.pages > 1" class="hero-table__pager">
55
55
  <v-btn variant="outlined" :disabled="!range.hasPrev" @click="emit('update:page', range.page - 1)">Prev</v-btn>
@@ -82,6 +82,8 @@ const props = withDefaults(
82
82
  skeletonRows?: number;
83
83
  /** 12.5px rows, for the Network table. */
84
84
  compact?: boolean;
85
+ /** Footer ("Showing 1–25 of 60" + pager) only when there is more than one page (the site fleet). */
86
+ pagedFoot?: boolean;
85
87
  }>(),
86
88
  {
87
89
  page: 1,
@@ -93,6 +95,7 @@ const props = withDefaults(
93
95
  emptyHint: "",
94
96
  skeletonRows: 6,
95
97
  compact: false,
98
+ pagedFoot: false,
96
99
  },
97
100
  );
98
101
  const emit = defineEmits<{ "update:page": [page: number] }>();
@@ -0,0 +1,139 @@
1
+ import { onBeforeUnmount, ref, watch, type Ref } from "vue";
2
+ import { mergeLiveStates, type TLiveRobotState } from "../utils/robot-display";
3
+
4
+ /**
5
+ * Live robot state for the robots ON SCREEN - one store, one swappable feed.
6
+ *
7
+ * ## The interface a feed implements
8
+ *
9
+ * type TRobotLiveFeed = {
10
+ * // Start delivering states for these robot ids. `push` may be called any
11
+ * // number of times with any subset; each item is a `TLiveRobotState`
12
+ * // (`{ id, ok: false }` = "no answer, show Reconnecting").
13
+ * // `connected(false)` = the feed itself is down ("Connection lost").
14
+ * // Returns a stop function.
15
+ * watch(ids: string[], push: (items) => void, connected: (up: boolean) => void): () => void;
16
+ * };
17
+ *
18
+ * Today's feed is `robotPollingFeed`: `GET /api/robots/network/states?ids=...`
19
+ * every 4 s, paused while the tab is hidden (owner decision 2026-09-23: poll
20
+ * on-screen robots 3-5 s). A site-wide push feed (socket.io room per site - a
21
+ * backend follow-up) only has to implement `watch` the same way; components
22
+ * read `states[id]` / `connected` and never know which feed is behind them.
23
+ */
24
+ export type TRobotLiveFeed = {
25
+ watch(
26
+ ids: string[],
27
+ push: (items: Array<Partial<TLiveRobotState> & { id: string }>) => void,
28
+ connected: (up: boolean) => void,
29
+ ): () => void;
30
+ };
31
+
32
+ /**
33
+ * The polling feed. `fetchStates` returns `{ items }` for up to 50 ids.
34
+ * One request in flight at a time; a failed request marks the feed down (the
35
+ * robots' last states are replaced by "Reconnecting" - never left frozen).
36
+ */
37
+ export function robotPollingFeed(
38
+ fetchStates: (ids: string[]) => Promise<{ items?: Array<Partial<TLiveRobotState> & { id: string }> }>,
39
+ intervalMs = 4000,
40
+ ): TRobotLiveFeed {
41
+ return {
42
+ watch(ids, push, connected) {
43
+ let stopped = false;
44
+ let timer: ReturnType<typeof setTimeout> | undefined;
45
+ const hidden = () => typeof document !== "undefined" && document.visibilityState === "hidden";
46
+
47
+ async function tick() {
48
+ if (stopped) return;
49
+ if (!hidden() && ids.length) {
50
+ try {
51
+ const res = await fetchStates(ids);
52
+ if (stopped) return;
53
+ push(res?.items ?? []);
54
+ connected(true);
55
+ } catch {
56
+ if (stopped) return;
57
+ push(ids.map((id) => ({ id, ok: false })));
58
+ connected(false);
59
+ }
60
+ }
61
+ if (!stopped) timer = setTimeout(tick, intervalMs);
62
+ }
63
+ // Coming back to the tab refreshes at once instead of waiting a tick.
64
+ const onVisible = () => {
65
+ if (hidden() || stopped) return;
66
+ clearTimeout(timer);
67
+ tick();
68
+ };
69
+ if (typeof document !== "undefined") document.addEventListener("visibilitychange", onVisible);
70
+ tick();
71
+ return () => {
72
+ stopped = true;
73
+ clearTimeout(timer);
74
+ if (typeof document !== "undefined") document.removeEventListener("visibilitychange", onVisible);
75
+ };
76
+ },
77
+ };
78
+ }
79
+
80
+ /** The Super Admin network feed: `/api/robots/network/states`. */
81
+ export function networkStatesFeed(intervalMs = 4000): TRobotLiveFeed {
82
+ return robotPollingFeed(
83
+ (ids) =>
84
+ useNuxtApp().$api<{ items: any[] }>("/api/robots/network/states", {
85
+ method: "GET",
86
+ query: { ids: ids.join(",") },
87
+ }),
88
+ intervalMs,
89
+ );
90
+ }
91
+
92
+ /**
93
+ * Any estate screen's feed (Property Management, Hygiene, Security, the
94
+ * console's site drill-down, robot detail): `GET /api/robots/states?ids=...`.
95
+ * The API checks every robot against its own site and narrows by the app's
96
+ * `appSlug`, so a robot this screen may not see simply never arrives.
97
+ */
98
+ export function siteStatesFeed(intervalMs = 4000): TRobotLiveFeed {
99
+ const appSlug = useRuntimeConfig().public?.appSlug as string | undefined;
100
+ return robotPollingFeed(
101
+ (ids) =>
102
+ useNuxtApp().$api<{ items: any[] }>("/api/robots/states", {
103
+ method: "GET",
104
+ query: { ids: ids.join(","), appSlug },
105
+ }),
106
+ intervalMs,
107
+ );
108
+ }
109
+
110
+ /**
111
+ * `states[id]` for every robot in `ids`, kept live by `feed`. Re-watches when
112
+ * the ids change (a new page, a new filter); stops on unmount.
113
+ */
114
+ export default function useRobotLive(ids: Ref<string[]>, feed: TRobotLiveFeed) {
115
+ const states = ref<Record<string, TLiveRobotState>>({});
116
+ const connected = ref(true);
117
+ const lastAt = ref<Date | null>(null);
118
+ let stop: (() => void) | null = null;
119
+
120
+ watch(
121
+ () => ids.value.join(","),
122
+ () => {
123
+ stop?.();
124
+ const list = [...ids.value];
125
+ stop = feed.watch(
126
+ list,
127
+ (items) => {
128
+ states.value = mergeLiveStates(states.value, items);
129
+ lastAt.value = new Date();
130
+ },
131
+ (up) => (connected.value = up),
132
+ );
133
+ },
134
+ { immediate: true },
135
+ );
136
+ onBeforeUnmount(() => stop?.());
137
+
138
+ return { states, connected, lastAt };
139
+ }
@@ -32,6 +32,50 @@ export default function useRobots() {
32
32
  );
33
33
  }
34
34
 
35
+ /**
36
+ * One site's robots for the HERO fleet screen: the stored record plus each
37
+ * robot's open fault / OFFLINE record and last-seen, paged. The answer has
38
+ * `pages` and `total` (read them with `listMeta` - never `totalPages`).
39
+ */
40
+ function getSiteFleet(
41
+ organization: string,
42
+ site: string,
43
+ { page = 1, limit = 25, type = "", search = "" }: { page?: number; limit?: number; type?: string; search?: string } = {},
44
+ ) {
45
+ return useNuxtApp().$api<Record<string, any>>(
46
+ `/api/robots/organization/${organization}/site/${site}/fleet`,
47
+ { method: "GET", query: { page, limit, type: type || undefined, search: search || undefined, appSlug } },
48
+ );
49
+ }
50
+
51
+ /** The site's six summary counts (Total · Online · Working · Charging · Faults · Offline). */
52
+ function getSiteSummary(organization: string, site: string, { type = "" }: { type?: string } = {}) {
53
+ return useNuxtApp().$api<Record<string, any>>(
54
+ `/api/robots/organization/${organization}/site/${site}/summary`,
55
+ { method: "GET", query: { type: type || undefined, appSlug } },
56
+ );
57
+ }
58
+
59
+ /** One robot's record for its detail screen: names, serial, open fault. Never a credential. */
60
+ function getRobot(id: string) {
61
+ return useNuxtApp().$api<Record<string, any>>(`/api/robots/${id}`, {
62
+ method: "GET",
63
+ query: { appSlug },
64
+ });
65
+ }
66
+
67
+ /**
68
+ * A security robot's patrol routes (ordered, named checkpoints; the one it is
69
+ * heading for) and their schedule. `supported: false` = the brand has no
70
+ * patrol interface - show "Monitoring not available", never an invented route.
71
+ */
72
+ function getPatrolRoutes(id: string) {
73
+ return useNuxtApp().$api<Record<string, any>>(`/api/robots/${id}/patrol-routes`, {
74
+ method: "GET",
75
+ query: { appSlug },
76
+ });
77
+ }
78
+
35
79
  /**
36
80
  * Register a robot.
37
81
  *
@@ -212,7 +256,10 @@ export default function useRobots() {
212
256
  | { kind: "return" }
213
257
  | { kind: "full"; cleanTimes?: number; modeFlag?: number; subModeFlag?: number; levelFlag?: number }
214
258
  | { kind: "area"; areas: string[]; cleanTimes?: number; modeFlag?: number; subModeFlag?: number; levelFlag?: number }
215
- | { kind: "spot"; points: Array<{ x: number; y: number }>; cleanTimes?: number; modeFlag?: number; subModeFlag?: number; levelFlag?: number },
259
+ | { kind: "spot"; points: Array<{ x: number; y: number }>; cleanTimes?: number; modeFlag?: number; subModeFlag?: number; levelFlag?: number }
260
+ | { kind: "pause" | "resume" | "cancel" }
261
+ | { kind: "navigate"; x: number; y: number; theta?: number }
262
+ | { kind: "relocalize"; floorNo: number; x: number; y: number; theta: number },
216
263
  ) {
217
264
  return useNuxtApp().$api<Record<string, any>>(
218
265
  `/api/robots/${id}/command`,
@@ -310,6 +357,10 @@ export default function useRobots() {
310
357
  releaseLiftSession,
311
358
  getRobotLiftSession,
312
359
  getRobots,
360
+ getSiteFleet,
361
+ getSiteSummary,
362
+ getRobot,
363
+ getPatrolRoutes,
313
364
  getRobotBrands,
314
365
  trackRobot,
315
366
  createRobot,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.28.1-staging.360",
5
+ "version": "4.28.1-staging.362",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",