@stapel/alerts-react 0.1.1
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/CHANGELOG.md +109 -0
- package/LICENSE +21 -0
- package/MODULE.md +140 -0
- package/README.md +147 -0
- package/dist/analytics/events.d.ts +29 -0
- package/dist/analytics/events.d.ts.map +1 -0
- package/dist/analytics/events.js +28 -0
- package/dist/analytics/events.js.map +1 -0
- package/dist/api/alertsApi.d.ts +84 -0
- package/dist/api/alertsApi.d.ts.map +1 -0
- package/dist/api/alertsApi.js +127 -0
- package/dist/api/alertsApi.js.map +1 -0
- package/dist/api/generated/schema.d.ts +453 -0
- package/dist/api/generated/schema.d.ts.map +1 -0
- package/dist/api/generated/schema.js +2 -0
- package/dist/api/generated/schema.js.map +1 -0
- package/dist/api/types.d.ts +78 -0
- package/dist/api/types.d.ts.map +1 -0
- package/dist/api/types.js +44 -0
- package/dist/api/types.js.map +1 -0
- package/dist/default/FixIssueDialog.d.ts +15 -0
- package/dist/default/FixIssueDialog.d.ts.map +1 -0
- package/dist/default/FixIssueDialog.js +67 -0
- package/dist/default/FixIssueDialog.js.map +1 -0
- package/dist/default/IssueDetail.d.ts +10 -0
- package/dist/default/IssueDetail.d.ts.map +1 -0
- package/dist/default/IssueDetail.js +83 -0
- package/dist/default/IssueDetail.js.map +1 -0
- package/dist/default/IssueFilterBar.d.ts +21 -0
- package/dist/default/IssueFilterBar.d.ts.map +1 -0
- package/dist/default/IssueFilterBar.js +86 -0
- package/dist/default/IssueFilterBar.js.map +1 -0
- package/dist/default/IssuesFeed.d.ts +10 -0
- package/dist/default/IssuesFeed.d.ts.map +1 -0
- package/dist/default/IssuesFeed.js +137 -0
- package/dist/default/IssuesFeed.js.map +1 -0
- package/dist/default/MuteIssueDialog.d.ts +19 -0
- package/dist/default/MuteIssueDialog.d.ts.map +1 -0
- package/dist/default/MuteIssueDialog.js +72 -0
- package/dist/default/MuteIssueDialog.js.map +1 -0
- package/dist/default/index.d.ts +32 -0
- package/dist/default/index.d.ts.map +1 -0
- package/dist/default/index.js +26 -0
- package/dist/default/index.js.map +1 -0
- package/dist/default/labels.d.ts +40 -0
- package/dist/default/labels.d.ts.map +1 -0
- package/dist/default/labels.js +94 -0
- package/dist/default/labels.js.map +1 -0
- package/dist/default/layout.d.ts +32 -0
- package/dist/default/layout.d.ts.map +1 -0
- package/dist/default/layout.js +44 -0
- package/dist/default/layout.js.map +1 -0
- package/dist/default/types.d.ts +27 -0
- package/dist/default/types.d.ts.map +1 -0
- package/dist/default/types.js +2 -0
- package/dist/default/types.js.map +1 -0
- package/dist/flows/errors.d.ts +12 -0
- package/dist/flows/errors.d.ts.map +1 -0
- package/dist/flows/errors.js +13 -0
- package/dist/flows/errors.js.map +1 -0
- package/dist/flows/registry.d.ts +22 -0
- package/dist/flows/registry.d.ts.map +1 -0
- package/dist/flows/registry.js +20 -0
- package/dist/flows/registry.js.map +1 -0
- package/dist/headless/AlertsProvider.d.ts +19 -0
- package/dist/headless/AlertsProvider.d.ts.map +1 -0
- package/dist/headless/AlertsProvider.js +15 -0
- package/dist/headless/AlertsProvider.js.map +1 -0
- package/dist/i18n/errorsMap.d.ts +10 -0
- package/dist/i18n/errorsMap.d.ts.map +1 -0
- package/dist/i18n/errorsMap.js +20 -0
- package/dist/i18n/errorsMap.js.map +1 -0
- package/dist/i18n/es.d.ts +20 -0
- package/dist/i18n/es.d.ts.map +1 -0
- package/dist/i18n/es.js +152 -0
- package/dist/i18n/es.js.map +1 -0
- package/dist/i18n/generated/errors.es.gen.d.ts +9 -0
- package/dist/i18n/generated/errors.es.gen.d.ts.map +1 -0
- package/dist/i18n/generated/errors.es.gen.js +57 -0
- package/dist/i18n/generated/errors.es.gen.js.map +1 -0
- package/dist/i18n/generated/errors.gen.d.ts +323 -0
- package/dist/i18n/generated/errors.gen.d.ts.map +1 -0
- package/dist/i18n/generated/errors.gen.js +165 -0
- package/dist/i18n/generated/errors.gen.js.map +1 -0
- package/dist/i18n/generated/errors.ru.gen.d.ts +9 -0
- package/dist/i18n/generated/errors.ru.gen.d.ts.map +1 -0
- package/dist/i18n/generated/errors.ru.gen.js +57 -0
- package/dist/i18n/generated/errors.ru.gen.js.map +1 -0
- package/dist/i18n/keys.d.ts +164 -0
- package/dist/i18n/keys.d.ts.map +1 -0
- package/dist/i18n/keys.js +300 -0
- package/dist/i18n/keys.js.map +1 -0
- package/dist/i18n/ru.d.ts +27 -0
- package/dist/i18n/ru.d.ts.map +1 -0
- package/dist/i18n/ru.js +159 -0
- package/dist/i18n/ru.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/model/context.d.ts +11 -0
- package/dist/model/context.d.ts.map +1 -0
- package/dist/model/context.js +16 -0
- package/dist/model/context.js.map +1 -0
- package/dist/model/issues.d.ts +103 -0
- package/dist/model/issues.d.ts.map +1 -0
- package/dist/model/issues.js +230 -0
- package/dist/model/issues.js.map +1 -0
- package/dist/model/queryKeys.d.ts +11 -0
- package/dist/model/queryKeys.d.ts.map +1 -0
- package/dist/model/queryKeys.js +32 -0
- package/dist/model/queryKeys.js.map +1 -0
- package/dist/model/refusals.d.ts +25 -0
- package/dist/model/refusals.d.ts.map +1 -0
- package/dist/model/refusals.js +73 -0
- package/dist/model/refusals.js.map +1 -0
- package/dist/model/runtime.d.ts +51 -0
- package/dist/model/runtime.d.ts.map +1 -0
- package/dist/model/runtime.js +29 -0
- package/dist/model/runtime.js.map +1 -0
- package/dist/model/status.d.ts +60 -0
- package/dist/model/status.d.ts.map +1 -0
- package/dist/model/status.js +116 -0
- package/dist/model/status.js.map +1 -0
- package/dist/nav/manifest.d.ts +43 -0
- package/dist/nav/manifest.d.ts.map +1 -0
- package/dist/nav/manifest.js +36 -0
- package/dist/nav/manifest.js.map +1 -0
- package/llms.txt +84 -0
- package/manifest.json +694 -0
- package/nav-manifest.json +46 -0
- package/package.json +119 -0
- package/src/analytics/events.ts +29 -0
- package/src/analytics/generated/events.json +7 -0
- package/src/api/alertsApi.ts +259 -0
- package/src/api/generated/schema.ts +455 -0
- package/src/api/types.ts +113 -0
- package/src/default/FixIssueDialog.tsx +158 -0
- package/src/default/IssueDetail.tsx +431 -0
- package/src/default/IssueFilterBar.tsx +197 -0
- package/src/default/IssuesFeed.tsx +343 -0
- package/src/default/MuteIssueDialog.tsx +186 -0
- package/src/default/index.ts +37 -0
- package/src/default/labels.ts +113 -0
- package/src/default/layout.ts +52 -0
- package/src/default/types.ts +27 -0
- package/src/flows/errors.ts +16 -0
- package/src/flows/registry.ts +32 -0
- package/src/headless/AlertsProvider.tsx +20 -0
- package/src/i18n/errorsMap.ts +31 -0
- package/src/i18n/es.ts +181 -0
- package/src/i18n/generated/errors.es.gen.ts +63 -0
- package/src/i18n/generated/errors.gen.ts +196 -0
- package/src/i18n/generated/errors.json +370 -0
- package/src/i18n/generated/errors.ru.gen.ts +63 -0
- package/src/i18n/keys.ts +341 -0
- package/src/i18n/ru.ts +188 -0
- package/src/index.ts +152 -0
- package/src/model/context.tsx +28 -0
- package/src/model/issues.ts +311 -0
- package/src/model/queryKeys.ts +41 -0
- package/src/model/refusals.ts +82 -0
- package/src/model/runtime.ts +80 -0
- package/src/model/status.ts +179 -0
- package/src/nav/manifest.ts +76 -0
- package/tsconfig.json +26 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@stapel/alerts-react` — the headless React pair for stapel-alerts
|
|
3
|
+
* (frontend-standard §2). Business + state only, zero visual opinion; the
|
|
4
|
+
* shipped screens live behind the `./default` subpath so a host that brings
|
|
5
|
+
* its own visuals never pulls antd.
|
|
6
|
+
*
|
|
7
|
+
* Layers: api → model → flows → headless → i18n. Generated surfaces (schema,
|
|
8
|
+
* error map, manifest, llms.txt) are produced by the monorepo `gen:*` drivers
|
|
9
|
+
* from stapel-alerts's own contract artifacts and stand under drift gates.
|
|
10
|
+
*
|
|
11
|
+
* **This pair reads and manages the tracker; it does not report to it.**
|
|
12
|
+
* `POST /report` is authenticated by a service key that lives in every
|
|
13
|
+
* container in the fleet, and the blast radius of one leaking must not include
|
|
14
|
+
* a browser. Nothing here can reach that endpoint.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// ── api ──────────────────────────────────────────────────────────────────────
|
|
18
|
+
export { createAlertsApi } from "./api/alertsApi.js";
|
|
19
|
+
export type {
|
|
20
|
+
AlertsApi,
|
|
21
|
+
AlertsRawOptions,
|
|
22
|
+
ConditionalRead,
|
|
23
|
+
ConditionalReadOptions,
|
|
24
|
+
IssueFilters,
|
|
25
|
+
} from "./api/alertsApi.js";
|
|
26
|
+
export {
|
|
27
|
+
ISSUE_LEVELS,
|
|
28
|
+
ISSUE_STATUSES,
|
|
29
|
+
SETTABLE_ISSUE_STATUSES,
|
|
30
|
+
OPEN_ISSUE_STATUSES,
|
|
31
|
+
isSettableStatus,
|
|
32
|
+
isOpenStatus,
|
|
33
|
+
} from "./api/types.js";
|
|
34
|
+
export type {
|
|
35
|
+
Schemas,
|
|
36
|
+
Issue,
|
|
37
|
+
IssueDetail,
|
|
38
|
+
IssuePage,
|
|
39
|
+
ErrorEvent,
|
|
40
|
+
IssuePatch,
|
|
41
|
+
IssueFix,
|
|
42
|
+
IssueLevel,
|
|
43
|
+
IssueKind,
|
|
44
|
+
IssueStatus,
|
|
45
|
+
SettableIssueStatus,
|
|
46
|
+
} from "./api/types.js";
|
|
47
|
+
|
|
48
|
+
// ── flows ────────────────────────────────────────────────────────────────────
|
|
49
|
+
// The flow-machine primitive lives in `@stapel/core` (one reviewed copy for
|
|
50
|
+
// every pair — frontend-core-architecture §4b). Re-exported for ergonomics.
|
|
51
|
+
export { createFlowMachine, useFlow, isErrorCode } from "@stapel/core";
|
|
52
|
+
export type {
|
|
53
|
+
FlowMachine,
|
|
54
|
+
FlowMachineOptions,
|
|
55
|
+
FlowStateBase,
|
|
56
|
+
FlowError,
|
|
57
|
+
} from "@stapel/core";
|
|
58
|
+
export { toFlowError } from "./flows/errors.js";
|
|
59
|
+
export { ALERTS_FLOWS, flowEndpoints } from "./flows/registry.js";
|
|
60
|
+
export type {
|
|
61
|
+
AlertsFlowId,
|
|
62
|
+
AlertsFlowSpec,
|
|
63
|
+
FlowEndpoint,
|
|
64
|
+
} from "./flows/registry.js";
|
|
65
|
+
|
|
66
|
+
// ── model (runtime wiring, query keys, context) ──────────────────────────────
|
|
67
|
+
export {
|
|
68
|
+
createAlertsRuntime,
|
|
69
|
+
DEFAULT_ALERTS_BASE_URL,
|
|
70
|
+
DEFAULT_ALERTS_POLL_INTERVAL_MS,
|
|
71
|
+
} from "./model/runtime.js";
|
|
72
|
+
export type {
|
|
73
|
+
AlertsRuntime,
|
|
74
|
+
CreateAlertsRuntimeOptions,
|
|
75
|
+
} from "./model/runtime.js";
|
|
76
|
+
export {
|
|
77
|
+
AlertsRuntimeContext,
|
|
78
|
+
useAlertsRuntime,
|
|
79
|
+
useAlertsApi,
|
|
80
|
+
useAlertsAnalytics,
|
|
81
|
+
} from "./model/context.js";
|
|
82
|
+
export { alertsQueryKeys } from "./model/queryKeys.js";
|
|
83
|
+
|
|
84
|
+
// ── model (the two reads) ───────────────────────────────────────────────────
|
|
85
|
+
export {
|
|
86
|
+
useIssues,
|
|
87
|
+
useIssue,
|
|
88
|
+
issueFiltersKey,
|
|
89
|
+
groupIssuesByService,
|
|
90
|
+
} from "./model/issues.js";
|
|
91
|
+
export type {
|
|
92
|
+
IssuesBag,
|
|
93
|
+
IssueBag,
|
|
94
|
+
IssueFeedFilters,
|
|
95
|
+
IssueGroup,
|
|
96
|
+
IssuesSnapshot,
|
|
97
|
+
IssueSnapshot,
|
|
98
|
+
} from "./model/issues.js";
|
|
99
|
+
|
|
100
|
+
// ── model (the writes) ──────────────────────────────────────────────────────
|
|
101
|
+
export { useIssueStatus } from "./model/status.js";
|
|
102
|
+
export type {
|
|
103
|
+
IssueStatusBag,
|
|
104
|
+
FixIssueInput,
|
|
105
|
+
MuteIssueInput,
|
|
106
|
+
ReopenIssueInput,
|
|
107
|
+
AnnotateIssueInput,
|
|
108
|
+
} from "./model/status.js";
|
|
109
|
+
|
|
110
|
+
// ── model (named refusals) ──────────────────────────────────────────────────
|
|
111
|
+
export {
|
|
112
|
+
toAlertsError,
|
|
113
|
+
isAlertsUnauthorized,
|
|
114
|
+
isAlertsStaffOnly,
|
|
115
|
+
isIssueNotFound,
|
|
116
|
+
isStatusNotSettable,
|
|
117
|
+
ALERTS_ERROR_STATUS_NOT_SETTABLE,
|
|
118
|
+
ALERTS_ERROR_UNAUTHORIZED,
|
|
119
|
+
ALERTS_ERROR_FORBIDDEN,
|
|
120
|
+
ALERTS_ERROR_ISSUE_NOT_FOUND,
|
|
121
|
+
} from "./model/refusals.js";
|
|
122
|
+
|
|
123
|
+
// ── analytics vocabulary (names only — the runtime is the host's seam) ──────
|
|
124
|
+
export { ALERTS_EVENTS } from "./analytics/events.js";
|
|
125
|
+
export type { AlertsEventName } from "./analytics/events.js";
|
|
126
|
+
|
|
127
|
+
// ── headless (renderless components) ─────────────────────────────────────────
|
|
128
|
+
export { AlertsProvider } from "./headless/AlertsProvider.js";
|
|
129
|
+
|
|
130
|
+
// ── nav ─────────────────────────────────────────────────────────────────────
|
|
131
|
+
export { navEntries, ADMIN_ROOT_ID } from "./nav/manifest.js";
|
|
132
|
+
|
|
133
|
+
// ── i18n ─────────────────────────────────────────────────────────────────────
|
|
134
|
+
export {
|
|
135
|
+
ALERTS_I18N_KEYS,
|
|
136
|
+
alertsI18nBundleEn,
|
|
137
|
+
registerAlertsI18n,
|
|
138
|
+
} from "./i18n/keys.js";
|
|
139
|
+
export type { AlertsI18nKey } from "./i18n/keys.js";
|
|
140
|
+
|
|
141
|
+
// ── errors map (code → status/params/remediation/en; generated) ──────────────
|
|
142
|
+
export {
|
|
143
|
+
ALERTS_ERRORS,
|
|
144
|
+
ALERTS_ERROR_CODES,
|
|
145
|
+
alertsErrorBundleEn,
|
|
146
|
+
explainAlertsError,
|
|
147
|
+
} from "./i18n/errorsMap.js";
|
|
148
|
+
export type {
|
|
149
|
+
AlertsErrorCode,
|
|
150
|
+
AlertsErrorSpec,
|
|
151
|
+
Remediation,
|
|
152
|
+
} from "./i18n/errorsMap.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Context } from "react";
|
|
2
|
+
import { createModuleContext } from "@stapel/core";
|
|
3
|
+
import type { Analytics, ModuleContextKit } from "@stapel/core";
|
|
4
|
+
import type { AlertsApi } from "../api/alertsApi.js";
|
|
5
|
+
import type { AlertsRuntime } from "./runtime.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The wired AlertsRuntime shared through React context by `<AlertsProvider>`.
|
|
9
|
+
* Hooks in `model/` and the screens in `default/` read the singletons from
|
|
10
|
+
* here. One reviewed copy of this plumbing lives in `@stapel/core`
|
|
11
|
+
* (`createModuleContext`, slim wave §21/S2); this module binds it under the
|
|
12
|
+
* pair's public names.
|
|
13
|
+
*/
|
|
14
|
+
const kit: ModuleContextKit<AlertsRuntime> =
|
|
15
|
+
createModuleContext<AlertsRuntime>("Alerts");
|
|
16
|
+
|
|
17
|
+
export const AlertsRuntimeContext: Context<AlertsRuntime | null> =
|
|
18
|
+
kit.RuntimeContext;
|
|
19
|
+
|
|
20
|
+
export const useAlertsRuntime: () => AlertsRuntime = kit.useRuntime;
|
|
21
|
+
|
|
22
|
+
export const useAlertsApi: () => AlertsApi = kit.useApi;
|
|
23
|
+
|
|
24
|
+
export const useAlertsAnalytics: () => Analytics | null = kit.useAnalytics;
|
|
25
|
+
|
|
26
|
+
/** @internal Re-exported as `<AlertsProvider>` from `headless/`. */
|
|
27
|
+
export const ModuleProvider: ModuleContextKit<AlertsRuntime>["Provider"] =
|
|
28
|
+
kit.Provider;
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two reads — the triage feed and one issue — and the ETag machinery that
|
|
3
|
+
* makes re-asking cheap.
|
|
4
|
+
*
|
|
5
|
+
* ── The validator lives in the CACHE ENTRY, not in a ref ──────────────────
|
|
6
|
+
*
|
|
7
|
+
* The obvious place to keep an `ETag` is a `useRef` beside the query. It is
|
|
8
|
+
* the wrong place: two panes mounted on the same filters are two refs over one
|
|
9
|
+
* cache entry, an unmount throws the validator away while the rows survive,
|
|
10
|
+
* and a re-mount then asks unconditionally for a page it already has.
|
|
11
|
+
*
|
|
12
|
+
* So the cached VALUE is `{etag, page}` — the rows and the validator the
|
|
13
|
+
* server issued for exactly those rows, stored together because that is what
|
|
14
|
+
* they are. `queryFn` reads the previous entry, offers its validator, and on a
|
|
15
|
+
* 304 returns **the previous object itself**. Same reference in, same
|
|
16
|
+
* reference out: TanStack compares by identity, sees nothing changed, and
|
|
17
|
+
* notifies nobody — so a feed polling every minute through a quiet night
|
|
18
|
+
* repaints exactly zero times.
|
|
19
|
+
*
|
|
20
|
+
* A validator is offered only when there ARE previous rows to keep. "I have a
|
|
21
|
+
* validator but no data" is a state this hook cannot reach, and the one arm
|
|
22
|
+
* that could produce it (a 304 with an empty cache) re-asks without the
|
|
23
|
+
* validator rather than inventing an empty page.
|
|
24
|
+
*/
|
|
25
|
+
import { useCallback, useState } from "react";
|
|
26
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
27
|
+
import { loadStateFromQuery, mapLoad, useActiveSessionReady } from "@stapel/core";
|
|
28
|
+
import type { LoadState } from "@stapel/core";
|
|
29
|
+
import type { IssueFilters } from "../api/alertsApi.js";
|
|
30
|
+
import type { Issue, IssueDetail, IssuePage } from "../api/types.js";
|
|
31
|
+
import { useAlertsApi, useAlertsRuntime } from "./context.js";
|
|
32
|
+
import { alertsQueryKeys } from "./queryKeys.js";
|
|
33
|
+
|
|
34
|
+
/** The filters a feed carries. The page cursor is the hook's, not the caller's. */
|
|
35
|
+
export type IssueFeedFilters = Omit<IssueFilters, "offset">;
|
|
36
|
+
|
|
37
|
+
/** What one cache entry of the feed holds: the rows and their validator. */
|
|
38
|
+
export interface IssuesSnapshot {
|
|
39
|
+
readonly etag: string | null;
|
|
40
|
+
readonly page: IssuePage;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** What one cache entry of the detail holds. */
|
|
44
|
+
export interface IssueSnapshot {
|
|
45
|
+
readonly etag: string | null;
|
|
46
|
+
readonly detail: IssueDetail;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A stable cache identity for a filter set. Sorted, so `{level, service}` and
|
|
51
|
+
* `{service, level}` are ONE cache entry rather than two copies of the same
|
|
52
|
+
* page that invalidate independently — and so the same two filters always
|
|
53
|
+
* offer the same validator.
|
|
54
|
+
*/
|
|
55
|
+
export function issueFiltersKey(filters?: IssueFilters): string {
|
|
56
|
+
if (filters === undefined) return "";
|
|
57
|
+
const parts: string[] = [];
|
|
58
|
+
if (filters.status !== undefined) parts.push(`status=${filters.status}`);
|
|
59
|
+
if (filters.level !== undefined) parts.push(`level=${filters.level}`);
|
|
60
|
+
if (filters.service !== undefined) parts.push(`service=${filters.service}`);
|
|
61
|
+
if (filters.since !== undefined) parts.push(`since=${filters.since}`);
|
|
62
|
+
if (filters.open !== undefined) parts.push(`open=${String(filters.open)}`);
|
|
63
|
+
if (filters.offset !== undefined) parts.push(`offset=${String(filters.offset)}`);
|
|
64
|
+
if (filters.limit !== undefined) parts.push(`limit=${String(filters.limit)}`);
|
|
65
|
+
return parts.sort().join("&");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The filters with every FILTER removed and the page size kept: `limit` is a
|
|
70
|
+
* view setting, not a question about the rows, so "Clear" must not turn a
|
|
71
|
+
* host's hundred-row page back into fifty.
|
|
72
|
+
*/
|
|
73
|
+
export function clearedIssueFilters(filters: IssueFeedFilters): IssueFeedFilters {
|
|
74
|
+
return filters.limit === undefined ? {} : { limit: filters.limit };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One service's rows, as the feed groups them. */
|
|
78
|
+
export interface IssueGroup {
|
|
79
|
+
readonly service: string;
|
|
80
|
+
readonly issues: readonly Issue[];
|
|
81
|
+
/** The worst level present, for the group header. */
|
|
82
|
+
readonly worstLevel: Issue["level"];
|
|
83
|
+
/** Occurrences across the group — what makes one service louder than another. */
|
|
84
|
+
readonly count: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const LEVEL_RANK: Readonly<Record<string, number>> = {
|
|
88
|
+
debug: 0,
|
|
89
|
+
info: 1,
|
|
90
|
+
warning: 2,
|
|
91
|
+
error: 3,
|
|
92
|
+
fatal: 4,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Group a page's rows by reporting service, worst-first.
|
|
97
|
+
*
|
|
98
|
+
* Pure and exported: "which service is on fire" is the first question the feed
|
|
99
|
+
* answers, and it must be answerable in a test without a render. Services are
|
|
100
|
+
* ordered by their worst level and then by how loud they are, because a
|
|
101
|
+
* service with one fatal outranks a service with two hundred warnings — and
|
|
102
|
+
* alphabetical order would bury it under `auth`.
|
|
103
|
+
*/
|
|
104
|
+
export function groupIssuesByService(
|
|
105
|
+
issues: readonly Issue[]
|
|
106
|
+
): readonly IssueGroup[] {
|
|
107
|
+
const byService = new Map<string, Issue[]>();
|
|
108
|
+
for (const issue of issues) {
|
|
109
|
+
const rows = byService.get(issue.service);
|
|
110
|
+
if (rows) rows.push(issue);
|
|
111
|
+
else byService.set(issue.service, [issue]);
|
|
112
|
+
}
|
|
113
|
+
const groups: IssueGroup[] = [];
|
|
114
|
+
for (const [service, rows] of byService) {
|
|
115
|
+
let worst: Issue["level"] = "debug";
|
|
116
|
+
let count = 0;
|
|
117
|
+
for (const row of rows) {
|
|
118
|
+
count += row.count;
|
|
119
|
+
if ((LEVEL_RANK[row.level] ?? 0) > (LEVEL_RANK[worst] ?? 0)) {
|
|
120
|
+
worst = row.level;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
groups.push({ service, issues: rows, worstLevel: worst, count });
|
|
124
|
+
}
|
|
125
|
+
return groups.sort(
|
|
126
|
+
(a, b) =>
|
|
127
|
+
(LEVEL_RANK[b.worstLevel] ?? 0) - (LEVEL_RANK[a.worstLevel] ?? 0) ||
|
|
128
|
+
b.count - a.count ||
|
|
129
|
+
a.service.localeCompare(b.service)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The shape a 304 with nothing to keep degrades to — see the module header. */
|
|
134
|
+
const EMPTY_PAGE: IssuePage = { count: 0, offset: 0, limit: 0, results: [] };
|
|
135
|
+
|
|
136
|
+
/** What {@link useIssues} reports. */
|
|
137
|
+
export interface IssuesBag {
|
|
138
|
+
/** The rows of the current page. */
|
|
139
|
+
readonly rows: LoadState<readonly Issue[]>;
|
|
140
|
+
/** The same rows, grouped by service, worst-first. */
|
|
141
|
+
readonly groups: LoadState<readonly IssueGroup[]>;
|
|
142
|
+
/** How many issues match the filters across every page. */
|
|
143
|
+
readonly total: number;
|
|
144
|
+
/** The page size the server APPLIED — `limit` echoed back, clamped to 1..200. */
|
|
145
|
+
readonly pageSize: number;
|
|
146
|
+
readonly offset: number;
|
|
147
|
+
readonly hasPrevious: boolean;
|
|
148
|
+
readonly hasNext: boolean;
|
|
149
|
+
readonly previousPage: () => void;
|
|
150
|
+
readonly nextPage: () => void;
|
|
151
|
+
/** The validator the last answer carried — `null` until the first 200. */
|
|
152
|
+
readonly etag: string | null;
|
|
153
|
+
/** A re-read that still offers the validator: usually a 304. */
|
|
154
|
+
readonly refetch: () => void;
|
|
155
|
+
/** A read is in flight (including a conditional poll that will 304). */
|
|
156
|
+
readonly isFetching: boolean;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The triage feed.
|
|
161
|
+
*
|
|
162
|
+
* The page cursor is INSIDE the hook and resets to zero whenever the filters
|
|
163
|
+
* change — the alternative (a caller-held offset) reliably leaves somebody on
|
|
164
|
+
* page 4 of a filter with one page, looking at an empty screen that is not
|
|
165
|
+
* empty.
|
|
166
|
+
*
|
|
167
|
+
* `filters.limit` is passed through as `?limit=`; the hook still pages by the
|
|
168
|
+
* size the envelope echoes, so a request the server clamped cannot skip rows.
|
|
169
|
+
*/
|
|
170
|
+
export function useIssues(
|
|
171
|
+
filters: IssueFeedFilters = {},
|
|
172
|
+
options: { readonly enabled?: boolean } = {}
|
|
173
|
+
): IssuesBag {
|
|
174
|
+
const api = useAlertsApi();
|
|
175
|
+
const runtime = useAlertsRuntime();
|
|
176
|
+
const queryClient = useQueryClient();
|
|
177
|
+
const sessionReady = useActiveSessionReady();
|
|
178
|
+
const enabled = sessionReady && (options.enabled ?? true);
|
|
179
|
+
|
|
180
|
+
const filtersKey = issueFiltersKey(filters);
|
|
181
|
+
// Derived-during-render reset: a filter change is a new question, and the
|
|
182
|
+
// answer to it starts at its first page. No effect, so there is no frame in
|
|
183
|
+
// which the old offset is applied to the new filters.
|
|
184
|
+
const [cursor, setCursor] = useState<{ key: string; offset: number }>({
|
|
185
|
+
key: filtersKey,
|
|
186
|
+
offset: 0,
|
|
187
|
+
});
|
|
188
|
+
const offset = cursor.key === filtersKey ? cursor.offset : 0;
|
|
189
|
+
|
|
190
|
+
const queryKey = alertsQueryKeys.issueList(
|
|
191
|
+
issueFiltersKey({ ...filters, offset })
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
const query = useQuery({
|
|
195
|
+
queryKey,
|
|
196
|
+
queryFn: async ({ signal }): Promise<IssuesSnapshot> => {
|
|
197
|
+
const previous = queryClient.getQueryData<IssuesSnapshot>(queryKey);
|
|
198
|
+
const request = { ...filters, offset };
|
|
199
|
+
const answer = await api.issues(request, {
|
|
200
|
+
// Offered only when there are rows the validator belongs to.
|
|
201
|
+
etag: previous ? previous.etag : null,
|
|
202
|
+
signal,
|
|
203
|
+
});
|
|
204
|
+
if (answer.outcome === "unchanged") {
|
|
205
|
+
// The identity IS the message: same object, so nothing re-renders.
|
|
206
|
+
if (previous) return previous;
|
|
207
|
+
const fresh = await api.issues(request, { signal });
|
|
208
|
+
return fresh.outcome === "modified"
|
|
209
|
+
? { etag: fresh.etag, page: fresh.data }
|
|
210
|
+
: { etag: fresh.etag, page: EMPTY_PAGE };
|
|
211
|
+
}
|
|
212
|
+
return { etag: answer.etag, page: answer.data };
|
|
213
|
+
},
|
|
214
|
+
enabled,
|
|
215
|
+
// No stream in this module, so the feed asks. Conditionally, which is why
|
|
216
|
+
// the backend put an ETag on this route in the first place.
|
|
217
|
+
refetchInterval: runtime.pollIntervalMs > 0 ? runtime.pollIntervalMs : false,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const snapshot = loadStateFromQuery(query);
|
|
221
|
+
const page = query.data?.page;
|
|
222
|
+
const pageSize = page?.limit ?? 0;
|
|
223
|
+
const total = page?.count ?? 0;
|
|
224
|
+
|
|
225
|
+
const goto = useCallback(
|
|
226
|
+
(next: number) => setCursor({ key: filtersKey, offset: Math.max(0, next) }),
|
|
227
|
+
[filtersKey]
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
rows: mapLoad(snapshot, (s) => s.page.results),
|
|
232
|
+
groups: mapLoad(snapshot, (s) => groupIssuesByService(s.page.results)),
|
|
233
|
+
total,
|
|
234
|
+
pageSize,
|
|
235
|
+
offset,
|
|
236
|
+
hasPrevious: offset > 0,
|
|
237
|
+
hasNext: pageSize > 0 && offset + pageSize < total,
|
|
238
|
+
previousPage: () => goto(offset - (pageSize || 0)),
|
|
239
|
+
nextPage: () => goto(offset + (pageSize || 0)),
|
|
240
|
+
etag: query.data?.etag ?? null,
|
|
241
|
+
refetch: () => {
|
|
242
|
+
void query.refetch();
|
|
243
|
+
},
|
|
244
|
+
isFetching: query.isFetching,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** What {@link useIssue} reports. */
|
|
249
|
+
export interface IssueBag {
|
|
250
|
+
readonly state: LoadState<IssueDetail>;
|
|
251
|
+
/** The last events the store still holds for this issue (up to 20). */
|
|
252
|
+
readonly events: LoadState<IssueDetail["events"]>;
|
|
253
|
+
readonly etag: string | null;
|
|
254
|
+
readonly refetch: () => void;
|
|
255
|
+
readonly isFetching: boolean;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* One issue, with its last events — the read the detail screen is built on.
|
|
260
|
+
*
|
|
261
|
+
* Conditional in the same way as the feed, and for a sharper reason: an issue
|
|
262
|
+
* detail carries up to twenty full tracebacks, which is the largest body this
|
|
263
|
+
* module serves and the one nobody wants re-sent every minute.
|
|
264
|
+
*/
|
|
265
|
+
export function useIssue(
|
|
266
|
+
issueId: string | undefined,
|
|
267
|
+
options: { readonly enabled?: boolean } = {}
|
|
268
|
+
): IssueBag {
|
|
269
|
+
const api = useAlertsApi();
|
|
270
|
+
const runtime = useAlertsRuntime();
|
|
271
|
+
const queryClient = useQueryClient();
|
|
272
|
+
const sessionReady = useActiveSessionReady();
|
|
273
|
+
const enabled =
|
|
274
|
+
sessionReady && issueId !== undefined && (options.enabled ?? true);
|
|
275
|
+
const queryKey = alertsQueryKeys.issue(issueId ?? "");
|
|
276
|
+
|
|
277
|
+
const query = useQuery({
|
|
278
|
+
queryKey,
|
|
279
|
+
queryFn: async ({ signal }): Promise<IssueSnapshot> => {
|
|
280
|
+
const previous = queryClient.getQueryData<IssueSnapshot>(queryKey);
|
|
281
|
+
const id = issueId ?? "";
|
|
282
|
+
const answer = await api.issue(id, {
|
|
283
|
+
etag: previous ? previous.etag : null,
|
|
284
|
+
signal,
|
|
285
|
+
});
|
|
286
|
+
if (answer.outcome === "unchanged") {
|
|
287
|
+
if (previous) return previous;
|
|
288
|
+
const fresh = await api.issue(id, { signal });
|
|
289
|
+
if (fresh.outcome === "modified") {
|
|
290
|
+
return { etag: fresh.etag, detail: fresh.data };
|
|
291
|
+
}
|
|
292
|
+
throw new Error("alerts: a conditional read answered 304 with no cache");
|
|
293
|
+
}
|
|
294
|
+
return { etag: answer.etag, detail: answer.data };
|
|
295
|
+
},
|
|
296
|
+
enabled,
|
|
297
|
+
refetchInterval: runtime.pollIntervalMs > 0 ? runtime.pollIntervalMs : false,
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const snapshot = loadStateFromQuery(query);
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
state: mapLoad(snapshot, (s) => s.detail),
|
|
304
|
+
events: mapLoad(snapshot, (s) => s.detail.events),
|
|
305
|
+
etag: query.data?.etag ?? null,
|
|
306
|
+
refetch: () => {
|
|
307
|
+
void query.refetch();
|
|
308
|
+
},
|
|
309
|
+
isFetching: query.isFetching,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Namespaced TanStack Query keys (frontend-standard §2 — "keys are
|
|
3
|
+
* namespaced"). Everything under the `"alerts"` root, so a host can invalidate
|
|
4
|
+
* the whole module, one resource, or one row. Explicit tuple return types
|
|
5
|
+
* satisfy `--isolatedDeclarations`.
|
|
6
|
+
*
|
|
7
|
+
* ── The list key carries the FILTERS **and** the offset ───────────────────
|
|
8
|
+
*
|
|
9
|
+
* Those are different server answers, so they are different cache entries. A
|
|
10
|
+
* key that ignored the filters would show the "fatal only" view the rows it
|
|
11
|
+
* had already fetched for "everything" — on the one screen whose job is
|
|
12
|
+
* deciding what is on fire.
|
|
13
|
+
*
|
|
14
|
+
* The offset is in the key for the same reason and one more: the ETag a page
|
|
15
|
+
* carries is the validator for THAT page. Sharing a cache entry across offsets
|
|
16
|
+
* would mean offering a validator for rows 0–49 on a request for rows 50–99,
|
|
17
|
+
* and a matching 304 would then keep the wrong page on screen.
|
|
18
|
+
*
|
|
19
|
+
* ── Nothing here is keyed by a user id ────────────────────────────────────
|
|
20
|
+
*
|
|
21
|
+
* Every read is the same tracker for every operator: the rows are a property
|
|
22
|
+
* of the fleet, not of the reader. Core's query runtime partitions the
|
|
23
|
+
* persisted cache per user, and sign-out clears it.
|
|
24
|
+
*/
|
|
25
|
+
const ROOT = "alerts" as const;
|
|
26
|
+
|
|
27
|
+
export const alertsQueryKeys: {
|
|
28
|
+
/** Everything this module caches — the one invalidation a host needs. */
|
|
29
|
+
readonly all: readonly ["alerts"];
|
|
30
|
+
/** Every issue read (the invalidation a write targets). */
|
|
31
|
+
readonly issues: readonly ["alerts", "issues"];
|
|
32
|
+
/** One filtered page. `filtersKey` is a stable serialization of the query. */
|
|
33
|
+
issueList(filtersKey: string): readonly ["alerts", "issues", "list", string];
|
|
34
|
+
/** One issue by id, with its events. */
|
|
35
|
+
issue(issueId: string): readonly ["alerts", "issues", "one", string];
|
|
36
|
+
} = {
|
|
37
|
+
all: [ROOT],
|
|
38
|
+
issues: [ROOT, "issues"],
|
|
39
|
+
issueList: (filtersKey) => [ROOT, "issues", "list", filtersKey],
|
|
40
|
+
issue: (issueId) => [ROOT, "issues", "one", issueId],
|
|
41
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The refusals this pair has to tell apart, and the reason every one of them
|
|
3
|
+
* is read by CODE and never by status.
|
|
4
|
+
*
|
|
5
|
+
* | status | code | what it actually means |
|
|
6
|
+
* |--------|---------------------------------|------------------------------------|
|
|
7
|
+
* | 400 | `alerts_status_not_settable` | the store asserts that status, not you |
|
|
8
|
+
* | 401 | `unauthorized` | no session reached the tracker |
|
|
9
|
+
* | 403 | `forbidden` | signed in, but not staff |
|
|
10
|
+
* | 404 | `alerts_issue_not_found` | no issue with that id |
|
|
11
|
+
*
|
|
12
|
+
* ── The 403 is the whole authorization story, and it is not a fault ───────
|
|
13
|
+
*
|
|
14
|
+
* Every tracker route is `IsStaffUser`. The nav axis has two values
|
|
15
|
+
* (`public` | `member`) and cannot say "staff", so the SCREEN says it: the
|
|
16
|
+
* door stays visible and the pane names the refusal
|
|
17
|
+
* ({@link isAlertsStaffOnly}) rather than drawing an empty triage table at
|
|
18
|
+
* somebody signed in with the wrong account. A hidden door teaches nobody
|
|
19
|
+
* anything; an empty table teaches the wrong thing — "nothing is broken".
|
|
20
|
+
*
|
|
21
|
+
* ── The 401 and the 403 are different sentences ───────────────────────────
|
|
22
|
+
*
|
|
23
|
+
* A 401 is "sign in"; a 403 is "you are signed in as the wrong person". The
|
|
24
|
+
* raw conditional reads (`api/alertsApi.ts`) do not run core's bearer-refresh
|
|
25
|
+
* retry, so a 401 on a read is also the shape an expired token takes here —
|
|
26
|
+
* which is exactly the case where "sign in again" is the right copy and
|
|
27
|
+
* "you do not have permission" is a lie.
|
|
28
|
+
*
|
|
29
|
+
* ── The 400 is the store refusing to be told what it observed ─────────────
|
|
30
|
+
*
|
|
31
|
+
* `regressed` is not a settable status (`serializers.SETTABLE_STATUSES`).
|
|
32
|
+
* Nothing in this pair offers it — {@link SETTABLE_ISSUE_STATUSES} is the list
|
|
33
|
+
* every control is built from — so {@link isStatusNotSettable} exists for the
|
|
34
|
+
* host that wired its own control, and for the day the backend narrows the set
|
|
35
|
+
* further. A refusal nobody can provoke from our own UI is still a refusal the
|
|
36
|
+
* UI must be able to render.
|
|
37
|
+
*/
|
|
38
|
+
import { isErrorCode, toFlowError } from "@stapel/core";
|
|
39
|
+
import type { FlowError } from "@stapel/core";
|
|
40
|
+
|
|
41
|
+
/** A status the store sets from evidence. Carries `{status}`. A **400**. */
|
|
42
|
+
export const ALERTS_ERROR_STATUS_NOT_SETTABLE =
|
|
43
|
+
"error.400.alerts_status_not_settable";
|
|
44
|
+
/** No session reached the tracker at all. A **401**. */
|
|
45
|
+
export const ALERTS_ERROR_UNAUTHORIZED = "error.401.unauthorized";
|
|
46
|
+
/** Signed in, and not staff. The tracker wall. A **403**. */
|
|
47
|
+
export const ALERTS_ERROR_FORBIDDEN = "error.403.forbidden";
|
|
48
|
+
/** No issue with that id — it may have been swept, or the link is old. **404**. */
|
|
49
|
+
export const ALERTS_ERROR_ISSUE_NOT_FOUND = "error.404.alerts_issue_not_found";
|
|
50
|
+
|
|
51
|
+
/** Fold any thrown value into this pair's error dialect. */
|
|
52
|
+
export function toAlertsError(error: unknown): FlowError {
|
|
53
|
+
return toFlowError(error, "alerts.error.unknown");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const is = (error: unknown, code: string): boolean =>
|
|
57
|
+
isErrorCode(toAlertsError(error), code);
|
|
58
|
+
|
|
59
|
+
/** Nobody is signed in (or the session expired between two reads). */
|
|
60
|
+
export function isAlertsUnauthorized(error: unknown): boolean {
|
|
61
|
+
return is(error, ALERTS_ERROR_UNAUTHORIZED);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Signed in, but not staff — the tracker wall.
|
|
66
|
+
*
|
|
67
|
+
* The one refusal every screen in this pair must be able to draw, because it
|
|
68
|
+
* is the answer the whole surface gives to most of a host's accounts.
|
|
69
|
+
*/
|
|
70
|
+
export function isAlertsStaffOnly(error: unknown): boolean {
|
|
71
|
+
return is(error, ALERTS_ERROR_FORBIDDEN);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** No issue with that id (swept, or a stale `alerts:<id>` in a commit). */
|
|
75
|
+
export function isIssueNotFound(error: unknown): boolean {
|
|
76
|
+
return is(error, ALERTS_ERROR_ISSUE_NOT_FOUND);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The store, declining to be told what it is supposed to have observed. */
|
|
80
|
+
export function isStatusNotSettable(error: unknown): boolean {
|
|
81
|
+
return is(error, ALERTS_ERROR_STATUS_NOT_SETTABLE);
|
|
82
|
+
}
|