|
@@ -8,12 +8,15 @@
|
|
|
* 纪律:
|
|
* 纪律:
|
|
|
* - ⛔ 用当页行数冒充总数;
|
|
* - ⛔ 用当页行数冒充总数;
|
|
|
* - ⛔ 失败折成 0:任一路失败即整体 `error`,页面把数字渲染成 `—`;
|
|
* - ⛔ 失败折成 0:任一路失败即整体 `error`,页面把数字渲染成 `—`;
|
|
|
|
|
+ * - 整份 `status_totals` 缺失/类型错误 ⇒ 抛错(D-03),⛔ 伪装成真实 0;
|
|
|
|
|
+ * - 个别非法计数值省略该键(D-04),调用方渲染 `—`,⛔ NaN/负张数上屏;
|
|
|
* - 一次页面只发一次;状态集合变化才重取。
|
|
* - 一次页面只发一次;状态集合变化才重取。
|
|
|
*/
|
|
*/
|
|
|
|
|
+import { ApiError } from "./api/http.ts";
|
|
|
import * as ticketApi from "./api/ticket.ts";
|
|
import * as ticketApi from "./api/ticket.ts";
|
|
|
import { useLoad } from "./useLoad.ts";
|
|
import { useLoad } from "./useLoad.ts";
|
|
|
|
|
|
|
|
-/** 单状态张数;取不到(失败/未取)时调用方渲染 `—`。 */
|
|
|
|
|
|
|
+/** 单状态张数;取不到(失败/未取/非法)时调用方渲染 `—`。 */
|
|
|
export type StatusCounts = Partial<Record<string, number>>;
|
|
export type StatusCounts = Partial<Record<string, number>>;
|
|
|
|
|
|
|
|
/** 只留能带进 `statuses` 的合法状态码。 */
|
|
/** 只留能带进 `statuses` 的合法状态码。 */
|
|
@@ -21,6 +24,16 @@ const KNOWN = new Set([
|
|
|
"unassigned", "pending", "processing", "completed", "rejected", "settled", "terminated",
|
|
"unassigned", "pending", "processing", "completed", "rejected", "settled", "terminated",
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 解析单个状态计数:键缺失 ⇒ 真实 0;值非法 ⇒ `undefined`(调用方渲染 `—`)。
|
|
|
|
|
+ * 合法=有限、非负整数(JSON 数字;⛔ 字符串垃圾、NaN、负数、小数)。
|
|
|
|
|
+ */
|
|
|
|
|
+function parseStatusTotal(raw: unknown): number | undefined {
|
|
|
|
|
+ if (raw === undefined) return 0;
|
|
|
|
|
+ if (typeof raw !== "number" || !Number.isFinite(raw) || raw < 0 || !Number.isInteger(raw)) return undefined;
|
|
|
|
|
+ return raw;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 一次取回可见范围内各状态 `total`(服务端 `status_totals`)。
|
|
* 一次取回可见范围内各状态 `total`(服务端 `status_totals`)。
|
|
|
*
|
|
*
|
|
@@ -32,7 +45,18 @@ export function useTicketStatusCounts(statuses: readonly string[]) {
|
|
|
return useLoad<StatusCounts>(async () => {
|
|
return useLoad<StatusCounts>(async () => {
|
|
|
const wanted = key.split(",");
|
|
const wanted = key.split(",");
|
|
|
const r = await ticketApi.ticketList({ page: 1, page_size: 1, include_status_totals: true });
|
|
const r = await ticketApi.ticketList({ page: 1, page_size: 1, include_status_totals: true });
|
|
|
- const totals = r.status_totals ?? {};
|
|
|
|
|
- return Object.fromEntries(wanted.map((status) => [status, Number(totals[status] ?? 0)])) as StatusCounts;
|
|
|
|
|
|
|
+ const raw = r.status_totals;
|
|
|
|
|
+ // D-03:整份缺失/null/数组/非对象 ⇒ 未知,⛔ 折成 {} 再填 0。
|
|
|
|
|
+ if (raw == null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
|
|
|
+ throw new ApiError(null, "状态计数结构异常");
|
|
|
|
|
+ }
|
|
|
|
|
+ const totals = raw as Record<string, unknown>;
|
|
|
|
|
+ const out: StatusCounts = {};
|
|
|
|
|
+ for (const status of wanted) {
|
|
|
|
|
+ const n = parseStatusTotal(Object.prototype.hasOwnProperty.call(totals, status) ? totals[status] : undefined);
|
|
|
|
|
+ // D-04:非法值不写入 ⇒ Partial 保持 undefined ⇒ KPI「—」;合法/缺键照常。
|
|
|
|
|
+ if (n !== undefined) out[status] = n;
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
}, [key], Boolean(key));
|
|
}, [key], Boolean(key));
|
|
|
}
|
|
}
|