@seedtactics/insight-client 17.0.1 → 17.0.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.
@@ -34,8 +34,10 @@ import { JobsBackend, LogBackend } from "../network/backend.js";
34
34
  import { InProcessMaterial, LogType, LocType, ActiveJob, WorkorderComment, ActiveWorkorder, InProcessMaterialLocation, } from "../network/api.js";
35
35
  import { last30JobComment } from "./scheduled-jobs.js";
36
36
  import { atom } from "jotai";
37
+ import { HashSet } from "@seedtactics/immutable-collections";
37
38
  import { currentOperator } from "../data/operators.js";
38
39
  const currentStatusRW = atom({
40
+ revision: 0,
39
41
  timeOfCurrentStatusUTC: new Date(),
40
42
  jobs: {},
41
43
  pallets: {},
@@ -100,59 +102,50 @@ export const updateCurrentStatus = atom(null, (_, set, { evt }) => {
100
102
  });
101
103
  function processEventsIntoCurrentStatus(entry) {
102
104
  return (curSt) => {
103
- const mats = new Map();
104
- function adjustMat(id, f) {
105
- if (mats.size === 0) {
106
- for (const m of curSt.material) {
107
- mats.set(m.materialID, m);
108
- }
109
- }
110
- const oldMat = mats.get(id);
111
- if (oldMat !== undefined) {
112
- mats.set(id, new InProcessMaterial(f(oldMat)));
113
- }
114
- }
115
- switch (entry.type) {
116
- case LogType.PartMark:
117
- for (const m of entry.material) {
118
- adjustMat(m.id, (inmat) => ({ ...inmat, serial: entry.result }));
119
- }
120
- break;
121
- case LogType.OrderAssignment:
122
- for (const m of entry.material) {
123
- adjustMat(m.id, (inmat) => ({ ...inmat, workorderId: entry.result }));
124
- }
125
- break;
126
- case LogType.Inspection:
127
- case LogType.InspectionForce:
128
- if (entry.result.toLowerCase() === "true" || entry.result === "1") {
129
- let inspType;
130
- if (entry.type === LogType.InspectionForce) {
131
- inspType = entry.program;
132
- }
133
- else {
134
- inspType = (entry.details || {}).InspectionType;
135
- }
136
- if (inspType) {
137
- for (const m of entry.material) {
138
- adjustMat(m.id, (inmat) => ({
139
- ...inmat,
140
- signaledInspections: [...inmat.signaledInspections, inspType],
141
- }));
105
+ if (entry.type !== LogType.PartMark &&
106
+ entry.type !== LogType.OrderAssignment &&
107
+ entry.type !== LogType.Inspection &&
108
+ entry.type !== LogType.InspectionForce)
109
+ return curSt;
110
+ const materialIds = HashSet.from(entry.material.map((m) => m.id));
111
+ // Anonymous raw material can repeat ID -1, including several pieces in the same slot. A log
112
+ // updates identified material in place; it must never rebuild these targets through an ID map.
113
+ const material = curSt.material.map((mat) => {
114
+ if (mat.materialID < 0 || !materialIds.has(mat.materialID))
115
+ return mat;
116
+ const data = mat;
117
+ switch (entry.type) {
118
+ case LogType.PartMark:
119
+ return mat.serial === entry.result
120
+ ? mat
121
+ : new InProcessMaterial({ ...data, serial: entry.result });
122
+ case LogType.OrderAssignment:
123
+ return mat.workorderId === entry.result
124
+ ? mat
125
+ : new InProcessMaterial({ ...data, workorderId: entry.result });
126
+ case LogType.Inspection:
127
+ case LogType.InspectionForce:
128
+ if (entry.result.toLowerCase() === "true" || entry.result === "1") {
129
+ let inspType;
130
+ if (entry.type === LogType.InspectionForce) {
131
+ inspType = entry.program;
132
+ }
133
+ else {
134
+ inspType = (entry.details || {}).InspectionType;
142
135
  }
136
+ if (inspType && !mat.signaledInspections.includes(inspType))
137
+ return new InProcessMaterial({
138
+ ...data,
139
+ signaledInspections: [...mat.signaledInspections, inspType],
140
+ });
143
141
  }
144
- }
145
- break;
146
- }
147
- if (mats.size === 0) {
148
- return curSt;
149
- }
150
- else {
151
- return {
152
- ...curSt,
153
- material: Array.from(mats.values()),
154
- };
155
- }
142
+ break;
143
+ }
144
+ return mat;
145
+ });
146
+ return material.every((mat, idx) => mat === curSt.material[idx])
147
+ ? curSt
148
+ : { ...curSt, material };
156
149
  };
157
150
  }
158
151
  export const reorderQueuedMatInCurrentStatus = atom(null, (_, set, { queue, matId, newIdx }) => {
@@ -1,4 +1,5 @@
1
- import { ICurrentStatus, IHistoricData, ILogEntry, IRecentHistoricData, IServerEvent } from "../network/api.js";
1
+ import { IHistoricData, ILogEntry, IRecentHistoricData, IServerEvent } from "../network/api.js";
2
+ export { onLoadCurrentSt } from "../network/current-status-session.js";
2
3
  import * as currentSt from "./current-status.js";
3
4
  import { Atom } from "jotai";
4
5
  export interface ServerEventAndTime {
@@ -22,9 +23,6 @@ export declare const onLoadLast30Jobs: import("jotai").WritableAtom<null, [histo
22
23
  export declare const onLoadLast30Log: import("jotai").WritableAtom<null, [log: readonly Readonly<ILogEntry>[]], void> & {
23
24
  init: null;
24
25
  };
25
- export declare const onLoadCurrentSt: import("jotai").WritableAtom<null, [curSt: Readonly<ICurrentStatus>], void> & {
26
- init: null;
27
- };
28
26
  export interface QueuedMatMove extends currentSt.QueueReordering {
29
27
  readonly operator: string | null;
30
28
  }
@@ -31,6 +31,8 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
31
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
32
  */
33
33
  import { QueuePosition, } from "../network/api.js";
34
+ import { acceptCurrentStatusSnapshot, currentStatusSessionAtom, onLoadCurrentSt, } from "../network/current-status-session.js";
35
+ export { onLoadCurrentSt } from "../network/current-status-session.js";
34
36
  import { JobsBackend } from "../network/backend.js";
35
37
  import { LazySeq } from "@seedtactics/immutable-collections";
36
38
  import * as simProd from "./sim-production.js";
@@ -109,11 +111,6 @@ export const onLoadLast30Log = atom(null, (_, set, log) => {
109
111
  const newCntr = LazySeq.of(log).maxBy((x) => x.counter)?.counter ?? null;
110
112
  set(lastEventCounterRW, (oldCntr) => oldCntr === null ? newCntr : Math.max(oldCntr, newCntr ?? -1));
111
113
  });
112
- export const onLoadCurrentSt = atom(null, (_, set, curSt) => {
113
- set(currentSt.setCurrentStatus, curSt);
114
- set(customSt.replaceCustomState, curSt.customState ?? null);
115
- set(names.setNamesFromCurrentStatus, curSt);
116
- });
117
114
  // Show queue drops immediately. Recovery must not replace intervening live updates or drags.
118
115
  export const moveQueuedMatInCurrentStatus = atom(null, async (get, set, { operator, ...move }) => {
119
116
  const before = get(currentSt.currentStatus);
@@ -123,15 +120,17 @@ export const moveQueuedMatInCurrentStatus = atom(null, async (get, set, { operat
123
120
  await JobsBackend.setMaterialInQueue(move.matId, operator, new QueuePosition({ queue: move.queue, position: move.newIdx }));
124
121
  }
125
122
  catch (error) {
123
+ const session = get(currentStatusSessionAtom).id;
126
124
  const beforeReload = get(currentSt.currentStatus);
127
125
  try {
128
126
  const refreshed = await JobsBackend.currentStatus();
129
127
  if (get(currentSt.currentStatus) === beforeReload)
130
- set(onLoadCurrentSt, refreshed);
128
+ set(acceptCurrentStatusSnapshot, session, refreshed, "http");
131
129
  }
132
130
  catch {
133
131
  // If the server is unreachable, undo only this move's untouched optimistic snapshot.
134
- if (get(currentSt.currentStatus) === optimistic)
132
+ if (get(currentStatusSessionAtom).id === session &&
133
+ get(currentSt.currentStatus) === optimistic)
135
134
  set(onLoadCurrentSt, before);
136
135
  }
137
136
  throw error;
@@ -31,10 +31,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
31
  import { useMemo, useState } from "react";
32
32
  import { Alert, Box, Button, Stack, Typography } from "@mui/material";
33
33
  import { LazySeq } from "@seedtactics/immutable-collections";
34
+ import { useSetAtom } from "jotai";
34
35
  import { basketSlotLabel } from "../../cell-status/station-cycles.js";
35
36
  import * as api from "../../network/api.js";
36
37
  import { serverErrorMessage } from "../../network/api-errors.js";
37
38
  import { JobsBackend } from "../../network/backend.js";
39
+ import { refreshCurrentStatus } from "../../network/websocket.js";
38
40
  import { InProcMaterial } from "./Material.js";
39
41
  import { MoveMaterialArrowNode } from "./MoveMaterialArrows.js";
40
42
  import { MoveMaterialNodeKindType } from "../../data/move-arrows.js";
@@ -121,6 +123,8 @@ function SlotMaterial({ material, basketId, fsize, }) {
121
123
  }
122
124
  export function BasketLoadStationWorkflow({ basket, material, fsize, title, elapsed, }) {
123
125
  const [submission, setSubmission] = useState();
126
+ const refresh = useSetAtom(refreshCurrentStatus);
127
+ const [refreshError, setRefreshError] = useState();
124
128
  const workState = useMemo(() => stationWork(material, basket), [basket, material]);
125
129
  const blockedReason = basket.loadStationWork?.confirmationBlockedReason?.trim();
126
130
  const instructionWarning = basket.loadStationWork?.instructionWarning?.trim();
@@ -152,6 +156,19 @@ export function BasketLoadStationWorkflow({ basket, material, fsize, title, elap
152
156
  .concat(loadsBySlot.keysToLazySeq())
153
157
  .distinctBy((slot) => slot)
154
158
  .toSortedArray((slot) => slot), [basket.emptySlots, basket.unknownSlots, materialBySlot, loadsBySlot]);
159
+ async function refreshStatus(workId) {
160
+ setRefreshError(undefined);
161
+ try {
162
+ if (!(await refresh(AbortSignal.timeout(5000))))
163
+ setRefreshError({ workId, message: "The connection changed while refreshing status." });
164
+ }
165
+ catch (error) {
166
+ setRefreshError({
167
+ workId,
168
+ message: serverErrorMessage(error) ?? "Unable to refresh status.",
169
+ });
170
+ }
171
+ }
155
172
  async function submit() {
156
173
  if (work === undefined || !work.ready)
157
174
  return;
@@ -174,6 +191,7 @@ export function BasketLoadStationWorkflow({ basket, material, fsize, title, elap
174
191
  const reason = serverErrorMessage(error);
175
192
  setSubmission((current) => (current === pending ? { ...pending, state, reason } : current));
176
193
  }
194
+ await refreshStatus(submittedWorkId);
177
195
  }
178
196
  const submissionDisabled = !work?.ready ||
179
197
  submissionState === "submitting" ||
@@ -192,7 +210,7 @@ export function BasketLoadStationWorkflow({ basket, material, fsize, title, elap
192
210
  minWidth: "8ch",
193
211
  textAlign: "right",
194
212
  fontVariantNumeric: "tabular-nums",
195
- }, children: elapsed })] }), _jsxs(Stack, { spacing: 2, sx: { ml: "4em", mr: "4em", mt: 2 }, children: [work?.confirmEmpty && work.ready && (_jsxs(Typography, { children: ["Confirm basket ", basket.basketId, " is empty."] })), instructionWarning && _jsx(Alert, { severity: "warning", children: instructionWarning }), work && !work.ready && (_jsx(Alert, { severity: "info", children: blockedReason ||
213
+ }, children: elapsed })] }), _jsxs(Stack, { spacing: 2, sx: { ml: "4em", mr: "4em", mt: 2 }, children: [refreshError?.workId === work?.workId && refreshError && (_jsxs(Alert, { severity: "warning", action: _jsx(Button, { onClick: () => void refreshStatus(refreshError.workId), children: "Refresh status" }), children: [refreshError.message, " Refresh current status to review the latest work."] })), work?.confirmEmpty && work.ready && (_jsxs(Typography, { children: ["Confirm basket ", basket.basketId, " is empty."] })), instructionWarning && _jsx(Alert, { severity: "warning", children: instructionWarning }), work && !work.ready && (_jsx(Alert, { severity: "info", children: blockedReason ||
196
214
  (work.awaitingSlots.length > 0
197
215
  ? `Waiting for material for slots ${work.awaitingSlots.map(basketSlotLabel).join(", ")}.`
198
216
  : "Basket work is not ready for confirmation.") })), !work && workState.type === "inconsistent" ? (_jsx(Alert, { severity: "warning", children: "Basket work is inconsistent. Wait for refreshed material actions before confirming." })) : null, submissionState === "accepted" ? (_jsx(Alert, { severity: "success", children: "Confirmation accepted. Waiting for refreshed work." })) : submissionState === "conflict" ? (_jsx(Alert, { severity: "warning", children: submissionReason ??
@@ -4,7 +4,7 @@ import type { AppProps } from "./components/App.js";
4
4
  */
5
5
  export declare function bootstrapInsight(appProps?: AppProps, rootElement?: HTMLElement): Promise<void>;
6
6
  export { customState } from "./cell-status/custom-state.js";
7
- export { currentStatusIsLive } from "./network/websocket.js";
7
+ export { currentStatusIsLive, refreshCurrentStatus } from "./network/websocket.js";
8
8
  export type { AppProps };
9
9
  export { defaultChooseModes } from "./components/ChooseMode.js";
10
10
  export type { ChooseModeItem } from "./components/ChooseMode.js";
package/dist/extension.js CHANGED
@@ -16,7 +16,7 @@ export async function bootstrapInsight(appProps = {}, rootElement) {
16
16
  render(appProps, root, store);
17
17
  }
18
18
  export { customState } from "./cell-status/custom-state.js";
19
- export { currentStatusIsLive } from "./network/websocket.js";
19
+ export { currentStatusIsLive, refreshCurrentStatus } from "./network/websocket.js";
20
20
  export { defaultChooseModes } from "./components/ChooseMode.js";
21
21
  export { RouteLocation } from "./components/routes.js";
22
22
  export { authenticatedFetch } from "./network/backend.js";