@goodpeanuts/dsh-sui 0.1.2-fix.1 → 0.1.2-fix.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.
@@ -1128,6 +1128,10 @@ window.__ModuleLoader__.load({
1128
1128
  section: "sync",
1129
1129
  query: { view: "backup" }
1130
1130
  };
1131
+ if (match.routeId === FinanceRouteIds.authorization) return {
1132
+ section: "sync",
1133
+ query: { view: "authorization" }
1134
+ };
1131
1135
  if (match.routeId === FinanceRouteIds.setup) return { section: "sync" };
1132
1136
  if (match.routeId === FinanceRouteIds.preferences) return { section: "preferences" };
1133
1137
  const bookId = match.params.bookId;
@@ -1230,6 +1234,7 @@ window.__ModuleLoader__.load({
1230
1234
  };
1231
1235
  }
1232
1236
  function ledgerRouteTarget(route) {
1237
+ if (route.section === "sync" && route.query?.view === "authorization") return { routeId: FinanceRouteIds.authorization };
1233
1238
  if (route.section === "sync" && route.query?.view === "backup") return { routeId: FinanceRouteIds.maintenance };
1234
1239
  if (route.section === "sync" && !route.bookId) return { routeId: FinanceRouteIds.setup };
1235
1240
  if (route.section === "preferences") return { routeId: FinanceRouteIds.preferences };
@@ -1341,6 +1346,7 @@ window.__ModuleLoader__.load({
1341
1346
  } else if (routeId === FinanceRouteIds.sync) set("view", search.get("view"), [
1342
1347
  "changes",
1343
1348
  "valuation",
1349
+ "authorization",
1344
1350
  "backup"
1345
1351
  ].includes(search.get("view") ?? ""));
1346
1352
  else if (routeId === FinanceRouteIds.accounts) set("group", search.get("group"));
@@ -1411,6 +1417,7 @@ window.__ModuleLoader__.load({
1411
1417
  preferences: "finance.ledger.preferences",
1412
1418
  maintenance: "finance.ledger.maintenance",
1413
1419
  setup: "finance.ledger.setup",
1420
+ authorization: "finance.ledger.authorization",
1414
1421
  overview: "finance.ledger.overview",
1415
1422
  transactions: "finance.ledger.transactions",
1416
1423
  transactionDetail: "finance.ledger.transaction-detail",
@@ -6542,7 +6549,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
6542
6549
  }));
6543
6550
  //#endregion
6544
6551
  //#region ../../packages/finance/src/transactions/canary.ts
6545
- var init_canary = __esmMin((() => {})), explicitOffsetDateTime, remoteId, writeFieldsSchema, updateTransactionPatchSchema;
6552
+ var init_canary = __esmMin((() => {})), explicitOffsetDateTime, remoteId, amountCents, writeFieldsSchema, updateTransactionPatchSchema;
6546
6553
  var init_contracts = __esmMin((() => {
6547
6554
  init_zod();
6548
6555
  init_date_range();
@@ -6551,6 +6558,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
6551
6558
  return !Number.isNaN(Date.parse(value));
6552
6559
  }, "occurredAt must be a valid date-time with an explicit timezone offset").refine(isSecondPrecisionInstant, "occurredAt must have whole-second precision; nonzero fractional seconds cannot be sent");
6553
6560
  remoteId = string$1().trim().min(1).max(128);
6561
+ amountCents = number().int().nonnegative().safe();
6554
6562
  writeFieldsSchema = object$1({
6555
6563
  occurredAt: explicitOffsetDateTime,
6556
6564
  type: _enum([
@@ -6558,7 +6566,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
6558
6566
  "income",
6559
6567
  "transfer"
6560
6568
  ]),
6561
- amountCents: number().int().positive().safe(),
6569
+ amountCents,
6562
6570
  secondaryAmountCents: number().int().positive().safe().optional(),
6563
6571
  accountId: remoteId,
6564
6572
  destinationAccountId: remoteId.optional(),
@@ -6610,7 +6618,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
6610
6618
  "income",
6611
6619
  "transfer"
6612
6620
  ]).optional(),
6613
- amountCents: number().int().positive().safe().optional(),
6621
+ amountCents: amountCents.optional(),
6614
6622
  secondaryAmountCents: number().int().positive().safe().nullable().optional(),
6615
6623
  accountId: remoteId.optional(),
6616
6624
  destinationAccountId: remoteId.nullable().optional(),
@@ -10210,7 +10218,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
10210
10218
  }
10211
10219
  function draftEditorIssues(value, references) {
10212
10220
  return [
10213
- ...value.amount.trim() && (!Number.isFinite(Number(value.amount)) || Number(value.amount) <= 0) ? ["金额:请输入大于 0 的数字。"] : [],
10221
+ ...value.amount.trim() && (!Number.isFinite(Number(value.amount)) || Number(value.amount) < 0) ? ["请输入有效的非负金额。"] : [],
10214
10222
  ...value.secondaryAmount.trim() && (!Number.isFinite(Number(value.secondaryAmount)) || Number(value.secondaryAmount) <= 0) ? ["转入金额:请输入大于 0 的数字。"] : [],
10215
10223
  ...value.occurredAt && !editableTransactionTimeValid(value.occurredAt) ? ["时间:请输入有效的北京时间,精确到秒。"] : [],
10216
10224
  ...value.type === "transfer" && value.destinationAccountId && value.destinationAccountId === value.accountId ? ["转入账户:不能与转出账户相同。"] : [],
@@ -10514,6 +10522,229 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
10514
10522
  init_memo_policy();
10515
10523
  }));
10516
10524
  //#endregion
10525
+ //#region ../../packages/finance-ui/src/book-grant.css
10526
+ var init_book_grant$1 = __esmMin((() => {}));
10527
+ //#endregion
10528
+ //#region ../../packages/finance-ui/src/book-grant.tsx
10529
+ /** A contextual switch inside financial UI, never mounted in an ordinary composer. */
10530
+ function BookGrantControl({ subjectId, client, onOpenLedger, disabled = false }) {
10531
+ const [opened, setOpened] = (0, react.useState)(false);
10532
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
10533
+ className: "sui-ledger-draft-ui book-grant-control",
10534
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
10535
+ type: "button",
10536
+ className: "book-grant-trigger",
10537
+ "aria-haspopup": "dialog",
10538
+ "aria-expanded": opened,
10539
+ title: "查看或切换本会话使用的账本",
10540
+ disabled,
10541
+ onClick: () => setOpened(true),
10542
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "book" }), "切换账本"]
10543
+ }), opened && (0, react_dom.createPortal)(/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
10544
+ className: "sui-ledger-draft-ui",
10545
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BookGrantDialog, {
10546
+ subjectId,
10547
+ client,
10548
+ onClose: () => setOpened(false),
10549
+ onOpenLedger
10550
+ })
10551
+ }), document.body)]
10552
+ });
10553
+ }
10554
+ function BookGrantDialog({ subjectId, client, onClose, onOpenLedger }) {
10555
+ const dialog = (0, react.useRef)(null);
10556
+ const alive = (0, react.useRef)(true);
10557
+ const running = (0, react.useRef)(false);
10558
+ const [data, setData] = (0, react.useState)(null);
10559
+ const [selected, setSelected] = (0, react.useState)("");
10560
+ const [makeDefault, setMakeDefault] = (0, react.useState)(false);
10561
+ const [busy, setBusy] = (0, react.useState)(false);
10562
+ const [error, setError] = (0, react.useState)("");
10563
+ const load = (0, react.useCallback)(async () => {
10564
+ if (running.current) return;
10565
+ running.current = true;
10566
+ setBusy(true);
10567
+ setError("");
10568
+ setData(null);
10569
+ setMakeDefault(false);
10570
+ try {
10571
+ if (!client.requestBookGrantSelection) throw new Error("请更新插件后重试。");
10572
+ const next = await client.requestBookGrantSelection(subjectId);
10573
+ if (!alive.current) return;
10574
+ setData(next);
10575
+ const preferred = next.grant?.bookId ?? next.defaultBookId;
10576
+ setSelected(next.books.find((book) => book.id === preferred)?.id ?? next.books[0]?.id ?? "");
10577
+ } catch {
10578
+ if (alive.current) setError("无法读取账本授权,请重试。");
10579
+ } finally {
10580
+ running.current = false;
10581
+ if (alive.current) setBusy(false);
10582
+ }
10583
+ }, [client, subjectId]);
10584
+ (0, react.useEffect)(() => {
10585
+ alive.current = true;
10586
+ const element = dialog.current;
10587
+ element.showModal();
10588
+ load();
10589
+ return () => {
10590
+ alive.current = false;
10591
+ element.close();
10592
+ };
10593
+ }, [load]);
10594
+ const finish = async (decision) => {
10595
+ if (running.current) return;
10596
+ if (!data || !client.respondBookGrantSelection) {
10597
+ onClose();
10598
+ return;
10599
+ }
10600
+ running.current = true;
10601
+ setBusy(true);
10602
+ setError("");
10603
+ try {
10604
+ await client.respondBookGrantSelection(subjectId, data.request.id, {
10605
+ decision,
10606
+ ...decision === "confirm" ? {
10607
+ bookId: selected,
10608
+ makeDefault
10609
+ } : {}
10610
+ });
10611
+ window.dispatchEvent(new Event("sui-ledger:finance-updated"));
10612
+ if (alive.current) onClose();
10613
+ } catch (cause) {
10614
+ if (!alive.current) return;
10615
+ if (decision === "cancel") onClose();
10616
+ else if (cause instanceof Error && cause.message === "finance_draft_book_change_blocked") setError("当前会话仍有流水草稿,请先将其加入待入账或移除全部条目,再切换账本。");
10617
+ else setError("授权未完成,账本可能已在其他页面更改。请重新读取后确认。");
10618
+ } finally {
10619
+ running.current = false;
10620
+ if (alive.current) setBusy(false);
10621
+ }
10622
+ };
10623
+ const clearDefault = async () => {
10624
+ if (running.current || !client.setDefaultBook) return;
10625
+ running.current = true;
10626
+ setBusy(true);
10627
+ setError("");
10628
+ try {
10629
+ await client.setDefaultBook(null);
10630
+ if (alive.current) {
10631
+ setData((current) => current && {
10632
+ ...current,
10633
+ defaultBookId: null
10634
+ });
10635
+ setMakeDefault(false);
10636
+ }
10637
+ } catch {
10638
+ if (alive.current) setError("清除默认账本失败,请重试。");
10639
+ } finally {
10640
+ running.current = false;
10641
+ if (alive.current) setBusy(false);
10642
+ }
10643
+ };
10644
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dialog", {
10645
+ ref: dialog,
10646
+ className: "book-grant-dialog",
10647
+ "aria-labelledby": "book-grant-title",
10648
+ onCancel: (event) => {
10649
+ event.preventDefault();
10650
+ finish("cancel");
10651
+ },
10652
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", {
10653
+ onSubmit: (event) => {
10654
+ event.preventDefault();
10655
+ if (selected && !busy) finish("confirm");
10656
+ },
10657
+ children: [
10658
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
10659
+ id: "book-grant-title",
10660
+ children: "选择会话账本"
10661
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10662
+ type: "button",
10663
+ tone: "ghost",
10664
+ disabled: busy,
10665
+ "aria-label": "关闭账本授权",
10666
+ onClick: () => void finish("cancel"),
10667
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "close" })
10668
+ })] }),
10669
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "授权后,当前会话可读取所选账本并维护草稿;实际入账仍需单独人工确认。" }),
10670
+ data && data.books.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
10671
+ className: "workset-field",
10672
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "授权账本" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", {
10673
+ value: selected,
10674
+ disabled: busy,
10675
+ onChange: (event) => setSelected(event.currentTarget.value),
10676
+ children: data.books.map((book) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", {
10677
+ value: book.id,
10678
+ children: book.name || book.id
10679
+ }, book.id))
10680
+ })]
10681
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
10682
+ className: "book-grant-default",
10683
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
10684
+ type: "checkbox",
10685
+ checked: makeDefault,
10686
+ disabled: busy,
10687
+ onChange: (event) => setMakeDefault(event.currentTarget.checked)
10688
+ }), "设为默认账本,供尚未授权的会话自动使用"]
10689
+ })] }),
10690
+ data && data.books.length === 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
10691
+ role: "status",
10692
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "尚无可用账本,请先在账本页面登录并同步。" }), onOpenLedger && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10693
+ type: "button",
10694
+ disabled: busy,
10695
+ onClick: () => {
10696
+ finish("cancel").then(onOpenLedger);
10697
+ },
10698
+ children: "打开账本"
10699
+ })]
10700
+ }),
10701
+ data?.defaultBookId && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
10702
+ className: "book-grant-current-default",
10703
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", { children: ["默认账本:", data.books.find((book) => book.id === data.defaultBookId)?.name ?? data.defaultBookId] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10704
+ type: "button",
10705
+ tone: "ghost",
10706
+ disabled: busy,
10707
+ onClick: () => void clearDefault(),
10708
+ children: "清除默认账本"
10709
+ })]
10710
+ }),
10711
+ error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
10712
+ role: "alert",
10713
+ children: error
10714
+ }),
10715
+ busy && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
10716
+ role: "status",
10717
+ children: "正在处理…"
10718
+ }),
10719
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("footer", { children: [
10720
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10721
+ type: "button",
10722
+ disabled: busy,
10723
+ onClick: () => void finish("cancel"),
10724
+ children: "取消"
10725
+ }),
10726
+ error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10727
+ type: "button",
10728
+ disabled: busy,
10729
+ onClick: () => void load(),
10730
+ children: "重新读取"
10731
+ }),
10732
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
10733
+ type: "submit",
10734
+ tone: "primary",
10735
+ disabled: busy || !data?.books.length || !selected,
10736
+ children: "确认授权"
10737
+ })
10738
+ ] })
10739
+ ]
10740
+ })
10741
+ });
10742
+ }
10743
+ var init_book_grant = __esmMin((() => {
10744
+ init_src$2();
10745
+ init_book_grant$1();
10746
+ }));
10747
+ //#endregion
10517
10748
  //#region ../../packages/finance-ui/src/draft-view.ts
10518
10749
  /** Session surfaces share one open-note read/poll; archives are never loaded. */
10519
10750
  function draftSource(client, subjectId) {
@@ -10755,9 +10986,13 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
10755
10986
  role: "status",
10756
10987
  children: [props.validation.issues.length, " 项待检查"]
10757
10988
  }),
10758
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
10989
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
10759
10990
  className: "candidate-head-actions",
10760
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
10991
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(BookGrantControl, {
10992
+ subjectId: props.subjectId,
10993
+ client: props.client,
10994
+ disabled: busy || dirty
10995
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
10761
10996
  type: "button",
10762
10997
  className: "draft-icon-btn",
10763
10998
  "data-panel-action": "close",
@@ -10765,7 +11000,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
10765
11000
  disabled: busy,
10766
11001
  onClick: props.onClose,
10767
11002
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "close" })
10768
- })
11003
+ })]
10769
11004
  })
10770
11005
  ]
10771
11006
  }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
@@ -11090,335 +11325,99 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
11090
11325
  /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("strong", { children: editable ? "编辑流水" : "流水详情" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: operation === "matched" ? "账本已有" : missing ? `待补齐:${missing}` : operation === "delete" ? "待删除" : operation === "update" ? "待修改" : "待新建" })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
11091
11326
  type: "button",
11092
11327
  "aria-label": "关闭",
11093
- title: "关闭",
11094
- disabled: props.busy,
11095
- onClick: props.onClose,
11096
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "close" })
11097
- })] }),
11098
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
11099
- className: `candidate-edit-panel-scroll${editable ? " is-editing" : ""}`,
11100
- children: editable && props.editorState ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DraftInlineEditor, {
11101
- state: props.editorState,
11102
- onChange: props.onEditorChange,
11103
- entry: props.entry,
11104
- ...props.referenceData ? { referenceData: props.referenceData } : {},
11105
- busy: props.busy,
11106
- error: props.error,
11107
- summary,
11108
- onCancel: props.onClose,
11109
- onSave: props.onSave
11110
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
11111
- summary,
11112
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dl", {
11113
- className: "candidate-panel-details",
11114
- children: [
11115
- ["类型", facts.direction === "income" ? "收入" : facts.direction === "transfer" ? "转账" : facts.direction === "expense" ? "支出" : "待补齐"],
11116
- ["金额", facts.amount],
11117
- ["时间", facts.occurredAt],
11118
- ["账户", facts.account],
11119
- ["分类", facts.category],
11120
- ["商家", facts.store || "未关联"],
11121
- ["备注", facts.detail || "—"]
11122
- ].map(([label, value]) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
11123
- className: label === "备注" ? "wide" : "",
11124
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("dt", { children: label }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dd", { children: value })]
11125
- }, label))
11126
- }),
11127
- operation === "delete" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
11128
- className: "candidate-panel-actions",
11129
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11130
- disabled: props.busy,
11131
- onClick: props.onRemove,
11132
- children: "从草稿删除"
11133
- })
11134
- }),
11135
- props.error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
11136
- className: "candidate-panel-error",
11137
- role: "alert",
11138
- children: props.error
11139
- })
11140
- ] })
11141
- })
11142
- ]
11143
- });
11144
- }
11145
- function groupEntriesByTab(entries) {
11146
- const groups = {
11147
- ready: [],
11148
- pending: [],
11149
- matched: []
11150
- };
11151
- for (const entry of entries) groups[draftEntryNeedsCompletion(entry) ? "pending" : entry.draft?.operation === "matched" ? "matched" : "ready"].push(entry);
11152
- return groups;
11153
- }
11154
- function groupEntriesByAccount(entries, referenceData) {
11155
- const groups = /* @__PURE__ */ new Map();
11156
- for (const entry of entries) {
11157
- const fields = {
11158
- ...entry.original,
11159
- ...entry.draft?.fields
11160
- };
11161
- const id = typeof fields.accountId === "string" ? fields.accountId : "__unassigned__";
11162
- const name = referenceData?.accounts.find((account) => account.id === id)?.name ?? draftEntryFacts(entry, referenceData).accountKindName ?? "未指定账户";
11163
- const group = groups.get(id) ?? {
11164
- id,
11165
- name,
11166
- items: []
11167
- };
11168
- group.items.push(entry);
11169
- groups.set(id, group);
11170
- }
11171
- return [...groups.values()];
11172
- }
11173
- var TAB_LABELS;
11174
- var init_draft_card = __esmMin((() => {
11175
- init_src$2();
11176
- init_web_contracts();
11177
- init_src();
11178
- init_draft_entry_editor();
11179
- init_draft_view();
11180
- TAB_LABELS = {
11181
- ready: "待处理",
11182
- pending: "待补齐",
11183
- matched: "账本已有"
11184
- };
11185
- }));
11186
- //#endregion
11187
- //#region ../../packages/finance-ui/src/book-grant.css
11188
- var init_book_grant$1 = __esmMin((() => {}));
11189
- //#endregion
11190
- //#region ../../packages/finance-ui/src/book-grant.tsx
11191
- /** Available before the first draft exists: authorization must not depend on authorized work. */
11192
- function BookGrantControl({ subjectId, client, onOpenLedger }) {
11193
- const [opened, setOpened] = (0, react.useState)(false);
11194
- const [bound, setBound] = (0, react.useState)(false);
11195
- (0, react.useEffect)(() => {
11196
- let active = true;
11197
- const refresh = () => {
11198
- client.getSubjectGrant(subjectId).then((grant) => {
11199
- if (active) setBound(Boolean(grant));
11200
- }).catch(() => {});
11201
- };
11202
- refresh();
11203
- window.addEventListener("sui-ledger:finance-updated", refresh);
11204
- return () => {
11205
- active = false;
11206
- window.removeEventListener("sui-ledger:finance-updated", refresh);
11207
- };
11208
- }, [client, subjectId]);
11209
- return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
11210
- className: "sui-ledger-draft-ui book-grant-control",
11211
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
11212
- type: "button",
11213
- className: "book-grant-trigger",
11214
- "aria-haspopup": "dialog",
11215
- "aria-expanded": opened,
11216
- title: bound ? "查看或切换会话账本" : "选择当前会话可访问的账本",
11217
- onClick: () => setOpened(true),
11218
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "book" }), "账本授权"]
11219
- }), opened && (0, react_dom.createPortal)(/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
11220
- className: "sui-ledger-draft-ui",
11221
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BookGrantDialog, {
11222
- subjectId,
11223
- client,
11224
- onClose: () => setOpened(false),
11225
- onOpenLedger
11226
- })
11227
- }), document.body)]
11228
- });
11229
- }
11230
- function BookGrantDialog({ subjectId, client, onClose, onOpenLedger }) {
11231
- const dialog = (0, react.useRef)(null);
11232
- const alive = (0, react.useRef)(true);
11233
- const running = (0, react.useRef)(false);
11234
- const [data, setData] = (0, react.useState)(null);
11235
- const [selected, setSelected] = (0, react.useState)("");
11236
- const [makeDefault, setMakeDefault] = (0, react.useState)(false);
11237
- const [busy, setBusy] = (0, react.useState)(false);
11238
- const [error, setError] = (0, react.useState)("");
11239
- const load = (0, react.useCallback)(async () => {
11240
- if (running.current) return;
11241
- running.current = true;
11242
- setBusy(true);
11243
- setError("");
11244
- setData(null);
11245
- setMakeDefault(false);
11246
- try {
11247
- if (!client.requestBookGrantSelection) throw new Error("请更新插件后重试。");
11248
- const next = await client.requestBookGrantSelection(subjectId);
11249
- if (!alive.current) return;
11250
- setData(next);
11251
- const preferred = next.grant?.bookId ?? next.defaultBookId;
11252
- setSelected(next.books.find((book) => book.id === preferred)?.id ?? next.books[0]?.id ?? "");
11253
- } catch {
11254
- if (alive.current) setError("无法读取账本授权,请重试。");
11255
- } finally {
11256
- running.current = false;
11257
- if (alive.current) setBusy(false);
11258
- }
11259
- }, [client, subjectId]);
11260
- (0, react.useEffect)(() => {
11261
- alive.current = true;
11262
- const element = dialog.current;
11263
- element.showModal();
11264
- load();
11265
- return () => {
11266
- alive.current = false;
11267
- element.close();
11268
- };
11269
- }, [load]);
11270
- const finish = async (decision) => {
11271
- if (running.current) return;
11272
- if (!data || !client.respondBookGrantSelection) {
11273
- onClose();
11274
- return;
11275
- }
11276
- running.current = true;
11277
- setBusy(true);
11278
- setError("");
11279
- try {
11280
- await client.respondBookGrantSelection(subjectId, data.request.id, {
11281
- decision,
11282
- ...decision === "confirm" ? {
11283
- bookId: selected,
11284
- makeDefault
11285
- } : {}
11286
- });
11287
- window.dispatchEvent(new Event("sui-ledger:finance-updated"));
11288
- if (alive.current) onClose();
11289
- } catch (cause) {
11290
- if (!alive.current) return;
11291
- if (decision === "cancel") onClose();
11292
- else if (cause instanceof Error && cause.message === "finance_draft_book_change_blocked") setError("当前会话仍有流水草稿,请先将其加入待入账或移除全部条目,再切换账本。");
11293
- else setError("授权未完成,账本可能已在其他页面更改。请重新读取后确认。");
11294
- } finally {
11295
- running.current = false;
11296
- if (alive.current) setBusy(false);
11297
- }
11298
- };
11299
- const clearDefault = async () => {
11300
- if (running.current || !client.setDefaultBook) return;
11301
- running.current = true;
11302
- setBusy(true);
11303
- setError("");
11304
- try {
11305
- await client.setDefaultBook(null);
11306
- if (alive.current) {
11307
- setData((current) => current && {
11308
- ...current,
11309
- defaultBookId: null
11310
- });
11311
- setMakeDefault(false);
11312
- }
11313
- } catch {
11314
- if (alive.current) setError("清除默认账本失败,请重试。");
11315
- } finally {
11316
- running.current = false;
11317
- if (alive.current) setBusy(false);
11318
- }
11319
- };
11320
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dialog", {
11321
- ref: dialog,
11322
- className: "book-grant-dialog",
11323
- "aria-labelledby": "book-grant-title",
11324
- onCancel: (event) => {
11325
- event.preventDefault();
11326
- finish("cancel");
11327
- },
11328
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", {
11329
- onSubmit: (event) => {
11330
- event.preventDefault();
11331
- if (selected && !busy) finish("confirm");
11332
- },
11333
- children: [
11334
- /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
11335
- id: "book-grant-title",
11336
- children: "选择会话账本"
11337
- }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11338
- type: "button",
11339
- tone: "ghost",
11340
- disabled: busy,
11341
- "aria-label": "关闭账本授权",
11342
- onClick: () => void finish("cancel"),
11343
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "close" })
11344
- })] }),
11345
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "授权后,当前会话可读取所选账本并维护草稿;实际入账仍需单独人工确认。" }),
11346
- data && data.books.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
11347
- className: "workset-field",
11348
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "授权账本" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", {
11349
- value: selected,
11350
- disabled: busy,
11351
- onChange: (event) => setSelected(event.currentTarget.value),
11352
- children: data.books.map((book) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", {
11353
- value: book.id,
11354
- children: book.name || book.id
11355
- }, book.id))
11356
- })]
11357
- }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
11358
- className: "book-grant-default",
11359
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
11360
- type: "checkbox",
11361
- checked: makeDefault,
11362
- disabled: busy,
11363
- onChange: (event) => setMakeDefault(event.currentTarget.checked)
11364
- }), "设为默认账本,供尚未授权的会话自动使用"]
11365
- })] }),
11366
- data && data.books.length === 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
11367
- role: "status",
11368
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "尚无可用账本,请先在账本页面登录并同步。" }), onOpenLedger && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11369
- type: "button",
11370
- disabled: busy,
11371
- onClick: () => {
11372
- finish("cancel").then(onOpenLedger);
11373
- },
11374
- children: "打开账本"
11375
- })]
11376
- }),
11377
- data?.defaultBookId && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
11378
- className: "book-grant-current-default",
11379
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", { children: ["默认账本:", data.books.find((book) => book.id === data.defaultBookId)?.name ?? data.defaultBookId] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11380
- type: "button",
11381
- tone: "ghost",
11382
- disabled: busy,
11383
- onClick: () => void clearDefault(),
11384
- children: "清除默认账本"
11385
- })]
11386
- }),
11387
- error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
11388
- role: "alert",
11389
- children: error
11390
- }),
11391
- busy && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
11392
- role: "status",
11393
- children: "正在处理…"
11394
- }),
11395
- /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("footer", { children: [
11396
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11397
- type: "button",
11398
- disabled: busy,
11399
- onClick: () => void finish("cancel"),
11400
- children: "取消"
11328
+ title: "关闭",
11329
+ disabled: props.busy,
11330
+ onClick: props.onClose,
11331
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Icon, { name: "close" })
11332
+ })] }),
11333
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
11334
+ className: `candidate-edit-panel-scroll${editable ? " is-editing" : ""}`,
11335
+ children: editable && props.editorState ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DraftInlineEditor, {
11336
+ state: props.editorState,
11337
+ onChange: props.onEditorChange,
11338
+ entry: props.entry,
11339
+ ...props.referenceData ? { referenceData: props.referenceData } : {},
11340
+ busy: props.busy,
11341
+ error: props.error,
11342
+ summary,
11343
+ onCancel: props.onClose,
11344
+ onSave: props.onSave
11345
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
11346
+ summary,
11347
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dl", {
11348
+ className: "candidate-panel-details",
11349
+ children: [
11350
+ ["类型", facts.direction === "income" ? "收入" : facts.direction === "transfer" ? "转账" : facts.direction === "expense" ? "支出" : "待补齐"],
11351
+ ["金额", facts.amount],
11352
+ ["时间", facts.occurredAt],
11353
+ ["账户", facts.account],
11354
+ ["分类", facts.category],
11355
+ ["商家", facts.store || "未关联"],
11356
+ ["备注", facts.detail || "—"]
11357
+ ].map(([label, value]) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
11358
+ className: label === "备注" ? "wide" : "",
11359
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("dt", { children: label }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("dd", { children: value })]
11360
+ }, label))
11401
11361
  }),
11402
- error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11403
- type: "button",
11404
- disabled: busy,
11405
- onClick: () => void load(),
11406
- children: "重新读取"
11362
+ operation === "delete" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
11363
+ className: "candidate-panel-actions",
11364
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11365
+ disabled: props.busy,
11366
+ onClick: props.onRemove,
11367
+ children: "从草稿删除"
11368
+ })
11407
11369
  }),
11408
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
11409
- type: "submit",
11410
- tone: "primary",
11411
- disabled: busy || !data?.books.length || !selected,
11412
- children: "确认授权"
11370
+ props.error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
11371
+ className: "candidate-panel-error",
11372
+ role: "alert",
11373
+ children: props.error
11413
11374
  })
11414
11375
  ] })
11415
- ]
11416
- })
11376
+ })
11377
+ ]
11417
11378
  });
11418
11379
  }
11419
- var init_book_grant = __esmMin((() => {
11380
+ function groupEntriesByTab(entries) {
11381
+ const groups = {
11382
+ ready: [],
11383
+ pending: [],
11384
+ matched: []
11385
+ };
11386
+ for (const entry of entries) groups[draftEntryNeedsCompletion(entry) ? "pending" : entry.draft?.operation === "matched" ? "matched" : "ready"].push(entry);
11387
+ return groups;
11388
+ }
11389
+ function groupEntriesByAccount(entries, referenceData) {
11390
+ const groups = /* @__PURE__ */ new Map();
11391
+ for (const entry of entries) {
11392
+ const fields = {
11393
+ ...entry.original,
11394
+ ...entry.draft?.fields
11395
+ };
11396
+ const id = typeof fields.accountId === "string" ? fields.accountId : "__unassigned__";
11397
+ const name = referenceData?.accounts.find((account) => account.id === id)?.name ?? draftEntryFacts(entry, referenceData).accountKindName ?? "未指定账户";
11398
+ const group = groups.get(id) ?? {
11399
+ id,
11400
+ name,
11401
+ items: []
11402
+ };
11403
+ group.items.push(entry);
11404
+ groups.set(id, group);
11405
+ }
11406
+ return [...groups.values()];
11407
+ }
11408
+ var TAB_LABELS;
11409
+ var init_draft_card = __esmMin((() => {
11420
11410
  init_src$2();
11421
- init_book_grant$1();
11411
+ init_web_contracts();
11412
+ init_src();
11413
+ init_draft_entry_editor();
11414
+ init_book_grant();
11415
+ init_draft_view();
11416
+ TAB_LABELS = {
11417
+ ready: "待处理",
11418
+ pending: "待补齐",
11419
+ matched: "账本已有"
11420
+ };
11422
11421
  }));
11423
11422
  //#endregion
11424
11423
  //#region ../../packages/finance-ui/src/ledger-route-page.tsx
@@ -11600,6 +11599,12 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
11600
11599
  description: "备份与恢复。",
11601
11600
  loadPage: loadLedgerPage
11602
11601
  },
11602
+ {
11603
+ id: FinanceRouteIds.authorization,
11604
+ path: "/ledger/authorization",
11605
+ description: "账本授权。",
11606
+ loadPage: loadLedgerPage
11607
+ },
11603
11608
  {
11604
11609
  id: FinanceRouteIds.setup,
11605
11610
  path: "/ledger/setup",
@@ -14226,9 +14231,13 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
14226
14231
  destinationAccountId: ""
14227
14232
  };
14228
14233
  }
14234
+ function editableAmountValid(input) {
14235
+ const amount = Number(input);
14236
+ return input.trim() !== "" && Number.isFinite(amount) && amount >= 0;
14237
+ }
14229
14238
  function ProposalFeedback({ draft, categoryValid }) {
14230
14239
  const issues = [
14231
- ...!draft.amount.trim() || !Number.isFinite(Number(draft.amount)) || Number(draft.amount) <= 0 ? ["金额:请输入大于 0 的数字。"] : [],
14240
+ ...!editableAmountValid(draft.amount) ? ["请输入有效的非负金额。"] : [],
14232
14241
  ...!draft.occurredAt ? ["时间:请选择流水发生时间。"] : !editableTransactionTimeValid(draft.occurredAt) ? ["时间:请输入有效的北京时间,精确到秒,不含非零小数秒。"] : [],
14233
14242
  ...!draft.accountId.trim() ? ["账户:请选择收支账户。"] : [],
14234
14243
  ...draft.type === "transfer" && !draft.destinationAccountId.trim() ? ["转入账户:请选择收款账户。"] : [],
@@ -14242,8 +14251,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
14242
14251
  }) : null;
14243
14252
  }
14244
14253
  function editableProposalValid(draft) {
14245
- const amount = Number(draft.amount);
14246
- if (!editableTransactionTimeValid(draft.occurredAt) || !Number.isFinite(amount) || amount <= 0 || !draft.accountId.trim()) return false;
14254
+ if (!editableTransactionTimeValid(draft.occurredAt) || !editableAmountValid(draft.amount) || !draft.accountId.trim()) return false;
14247
14255
  return draft.type === "transfer" ? Boolean(draft.destinationAccountId.trim() && draft.destinationAccountId !== draft.accountId) : Boolean(draft.categoryId.trim());
14248
14256
  }
14249
14257
  function isWorksetEnvelope(value) {
@@ -15261,6 +15269,53 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
15261
15269
  init_local_ledger_helpers();
15262
15270
  }));
15263
15271
  //#endregion
15272
+ //#region ../../packages/finance-ui/src/ledger-sync-tabs.css
15273
+ var init_ledger_sync_tabs$1 = __esmMin((() => {}));
15274
+ //#endregion
15275
+ //#region ../../packages/finance-ui/src/ledger-sync-tabs.tsx
15276
+ function LedgerSyncTabs({ view, bookId, onNavigate, busy = false }) {
15277
+ const element = (0, react.useRef)(null);
15278
+ (0, react.useLayoutEffect)(() => {
15279
+ const nav = element.current;
15280
+ const reveal = () => {
15281
+ const active = nav.querySelector("[aria-current=\"page\"]");
15282
+ if (!active) return;
15283
+ const item = active.getBoundingClientRect(), bounds = nav.getBoundingClientRect();
15284
+ if (item.left < bounds.left) nav.scrollLeft -= bounds.left - item.left;
15285
+ else if (item.right > bounds.right) nav.scrollLeft += item.right - bounds.right;
15286
+ };
15287
+ reveal();
15288
+ const observer = new ResizeObserver(reveal);
15289
+ observer.observe(nav);
15290
+ return () => observer.disconnect();
15291
+ }, [view]);
15292
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("nav", {
15293
+ ref: element,
15294
+ className: "ledger-sync-tabs",
15295
+ "aria-label": "数据与同步分区",
15296
+ children: [
15297
+ ["status", "同步状态"],
15298
+ ["changes", "流水变更"],
15299
+ ["valuation", "汇率与计算"],
15300
+ ["authorization", "账本授权"],
15301
+ ["backup", "备份与恢复"]
15302
+ ].map(([value, label]) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
15303
+ type: "button",
15304
+ "aria-current": view === value ? "page" : void 0,
15305
+ disabled: busy || !bookId && (value === "changes" || value === "valuation"),
15306
+ onClick: () => onNavigate({
15307
+ ...bookId ? { bookId } : {},
15308
+ section: "sync",
15309
+ ...value === "status" ? {} : { query: { view: value } }
15310
+ }),
15311
+ children: label
15312
+ }, value))
15313
+ });
15314
+ }
15315
+ var init_ledger_sync_tabs = __esmMin((() => {
15316
+ init_ledger_sync_tabs$1();
15317
+ }));
15318
+ //#endregion
15264
15319
  //#region ../../packages/finance-ui/src/local-ledger-changes.tsx
15265
15320
  function LedgerChanges({ bookId, refreshKey, onNavigate }) {
15266
15321
  const fetchImpl = useLedgerFetch();
@@ -18028,36 +18083,10 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
18028
18083
  const relevant = syncRecordsWithLiveOperation(syncRecords, book.id, liveSyncRecord);
18029
18084
  const latest = [...relevant].filter((item) => syncStatusKind(item.status) === "complete").sort((left, right) => String(right.completedAt).localeCompare(String(left.completedAt)))[0];
18030
18085
  const view = route.query?.view === "changes" || route.query?.view === "valuation" ? route.query.view : "status";
18031
- const tabs = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("nav", {
18032
- className: "ledger-sync-tabs",
18033
- "aria-label": "数据与同步分区",
18034
- children: [
18035
- {
18036
- value: "status",
18037
- label: "同步状态"
18038
- },
18039
- {
18040
- value: "changes",
18041
- label: "流水变更"
18042
- },
18043
- {
18044
- value: "valuation",
18045
- label: "汇率与计算"
18046
- },
18047
- {
18048
- value: "backup",
18049
- label: "备份与恢复"
18050
- }
18051
- ].map((tab) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
18052
- type: "button",
18053
- "aria-current": view === tab.value ? "page" : void 0,
18054
- onClick: () => onNavigate({
18055
- bookId: book.id,
18056
- section: "sync",
18057
- ...tab.value === "status" ? {} : { query: { view: tab.value } }
18058
- }),
18059
- children: tab.label
18060
- }, tab.value))
18086
+ const tabs = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LedgerSyncTabs, {
18087
+ view,
18088
+ bookId: book.id,
18089
+ onNavigate
18061
18090
  });
18062
18091
  if (view === "changes") return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
18063
18092
  className: "ledger-page ledger-changes-page",
@@ -18872,6 +18901,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
18872
18901
  init_finance_client();
18873
18902
  init_local_ledger_helpers();
18874
18903
  init_local_ledger_visuals();
18904
+ init_ledger_sync_tabs();
18875
18905
  init_local_ledger_changes();
18876
18906
  init_account_period();
18877
18907
  PAGE_SIZE = 15;
@@ -20328,25 +20358,11 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
20328
20358
  className: "page-head",
20329
20359
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: "数据与同步" })
20330
20360
  }),
20331
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("nav", {
20332
- className: "ledger-sync-tabs",
20333
- "aria-label": "数据与同步分区",
20334
- children: [
20335
- ["status", "同步状态"],
20336
- ["changes", "流水变更"],
20337
- ["valuation", "汇率与计算"],
20338
- ["backup", "备份与恢复"]
20339
- ].map(([view, label]) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
20340
- type: "button",
20341
- "aria-current": view === "backup" ? "page" : void 0,
20342
- disabled: view !== "backup" && view !== "status" && !bookId || !!busy,
20343
- onClick: () => onNavigate({
20344
- ...bookId ? { bookId } : {},
20345
- section: "sync",
20346
- ...view === "status" ? {} : { query: { view } }
20347
- }),
20348
- children: label
20349
- }, view))
20361
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LedgerSyncTabs, {
20362
+ view: "backup",
20363
+ bookId,
20364
+ onNavigate,
20365
+ busy: Boolean(busy)
20350
20366
  }),
20351
20367
  /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
20352
20368
  className: "backup-action-row",
@@ -20510,6 +20526,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
20510
20526
  }
20511
20527
  var BASE, post;
20512
20528
  var init_backup_recovery = __esmMin((() => {
20529
+ init_ledger_sync_tabs();
20513
20530
  init_ledger_transport();
20514
20531
  init_src$2();
20515
20532
  init_backup_recovery$1();
@@ -22352,6 +22369,174 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22352
22369
  };
22353
22370
  }));
22354
22371
  //#endregion
22372
+ //#region ../../packages/finance-ui/src/book-authorization.css
22373
+ var init_book_authorization$1 = __esmMin((() => {}));
22374
+ //#endregion
22375
+ //#region ../../packages/finance-ui/src/book-authorization.tsx
22376
+ /** Profile-wide opt-in. It does not mint grants just because a chat is opened. */
22377
+ function BookAuthorizationPage({ client, books, bookId, onNavigate }) {
22378
+ const [current, setCurrent] = (0, react.useState)(null);
22379
+ const [selected, setSelected] = (0, react.useState)("");
22380
+ const [remember, setRemember] = (0, react.useState)(false);
22381
+ const [loading, setLoading] = (0, react.useState)(true);
22382
+ const [busy, setBusy] = (0, react.useState)(false);
22383
+ const [error, setError] = (0, react.useState)("");
22384
+ const [notice, setNotice] = (0, react.useState)("");
22385
+ const alive = (0, react.useRef)(true), locked = (0, react.useRef)(false);
22386
+ const [reload, setReload] = (0, react.useState)(0);
22387
+ (0, react.useEffect)(() => {
22388
+ let active = true;
22389
+ alive.current = true;
22390
+ setLoading(true);
22391
+ setError("");
22392
+ client.getDefaultBook().then((value) => {
22393
+ if (!active) return;
22394
+ setCurrent(value);
22395
+ setRemember(Boolean(value));
22396
+ setSelected(books.find((book) => book.id === value)?.id ?? books.find((book) => book.id === bookId)?.id ?? books[0]?.id ?? "");
22397
+ }).catch(() => {
22398
+ if (active) setError("无法读取账本授权,请重试。");
22399
+ }).finally(() => {
22400
+ if (active) setLoading(false);
22401
+ });
22402
+ return () => {
22403
+ active = false;
22404
+ alive.current = false;
22405
+ };
22406
+ }, [
22407
+ client,
22408
+ books,
22409
+ bookId,
22410
+ reload
22411
+ ]);
22412
+ const save = async (value) => {
22413
+ if (locked.current || !client.setDefaultBook) return;
22414
+ locked.current = true;
22415
+ setBusy(true);
22416
+ setError("");
22417
+ setNotice("");
22418
+ try {
22419
+ await client.setDefaultBook(value);
22420
+ if (!alive.current) return;
22421
+ setCurrent(value);
22422
+ setRemember(Boolean(value));
22423
+ setNotice(value ? "授权已保存,后续会话使用财务功能时将自动使用此账本。" : "已停止为新会话自动授权;已有会话的授权与草稿不受影响。");
22424
+ window.dispatchEvent(new Event("sui-ledger:finance-updated"));
22425
+ } catch {
22426
+ if (alive.current) setError("保存账本授权失败,请重新读取后重试。");
22427
+ } finally {
22428
+ locked.current = false;
22429
+ if (alive.current) setBusy(false);
22430
+ }
22431
+ };
22432
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
22433
+ className: "ledger-page book-authorization-page",
22434
+ children: [
22435
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("header", {
22436
+ className: "page-head",
22437
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: "数据与同步" })
22438
+ }),
22439
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LedgerSyncTabs, {
22440
+ view: "authorization",
22441
+ bookId,
22442
+ onNavigate,
22443
+ busy
22444
+ }),
22445
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
22446
+ className: "book-authorization-settings",
22447
+ "aria-labelledby": "book-authorization-title",
22448
+ children: [
22449
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("header", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
22450
+ id: "book-authorization-title",
22451
+ children: "账本授权"
22452
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
22453
+ className: "book-authorization-status",
22454
+ children: loading ? "正在读取" : current ? "已授权" : "未设置"
22455
+ })] }),
22456
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "选择默认账本,之后使用财务功能时无需重复授权。" }),
22457
+ loading ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
22458
+ role: "status",
22459
+ children: "正在读取账本授权…"
22460
+ }) : books.length === 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: "尚无可用账本,请先登录并同步。" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
22461
+ type: "button",
22462
+ onClick: () => onNavigate({ section: "sync" }),
22463
+ children: "登录并同步账本"
22464
+ })] }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("form", {
22465
+ onSubmit: (event) => {
22466
+ event.preventDefault();
22467
+ if (!busy && (remember ? selected : current)) save(remember ? selected : null);
22468
+ },
22469
+ children: [
22470
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
22471
+ className: "book-authorization-field",
22472
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: "默认账本" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", {
22473
+ value: selected,
22474
+ disabled: busy || Boolean(error),
22475
+ onChange: (event) => setSelected(event.currentTarget.value),
22476
+ children: books.map((book) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", {
22477
+ value: book.id,
22478
+ children: book.name || book.id
22479
+ }, book.id))
22480
+ })]
22481
+ }),
22482
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("label", {
22483
+ className: "book-authorization-remember",
22484
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
22485
+ type: "checkbox",
22486
+ checked: remember,
22487
+ disabled: busy || Boolean(error),
22488
+ onChange: (event) => setRemember(event.currentTarget.checked)
22489
+ }), "后续会话使用财务功能时自动使用此账本"]
22490
+ }),
22491
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
22492
+ className: "book-authorization-note",
22493
+ children: "仅在使用财务功能时读取账本。实际入账仍需人工确认。"
22494
+ }),
22495
+ current && !books.some((book) => book.id === current) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
22496
+ role: "status",
22497
+ children: "原授权账本暂不可用,请重新选择可用账本。"
22498
+ }),
22499
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("footer", { children: [current && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
22500
+ type: "button",
22501
+ disabled: busy,
22502
+ onClick: () => void save(null),
22503
+ children: "停止自动授权"
22504
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
22505
+ type: "submit",
22506
+ tone: "primary",
22507
+ disabled: busy || (remember ? !selected : !current) || Boolean(error) || !client.setDefaultBook,
22508
+ children: current ? "保存更改" : "确认授权"
22509
+ })] })
22510
+ ]
22511
+ }),
22512
+ busy && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
22513
+ role: "status",
22514
+ children: "正在保存…"
22515
+ }),
22516
+ notice && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
22517
+ role: "status",
22518
+ children: notice
22519
+ }),
22520
+ error && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
22521
+ role: "alert",
22522
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: error }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Button, {
22523
+ type: "button",
22524
+ disabled: busy,
22525
+ onClick: () => setReload((value) => value + 1),
22526
+ children: "重新读取"
22527
+ })]
22528
+ })
22529
+ ]
22530
+ })
22531
+ ]
22532
+ });
22533
+ }
22534
+ var init_book_authorization = __esmMin((() => {
22535
+ init_src$2();
22536
+ init_ledger_sync_tabs();
22537
+ init_book_authorization$1();
22538
+ }));
22539
+ //#endregion
22355
22540
  //#region ../../packages/finance-ui/src/first-run-setup.css
22356
22541
  var init_first_run_setup$1 = __esmMin((() => {}));
22357
22542
  //#endregion
@@ -22423,34 +22608,10 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22423
22608
  className: "page-head",
22424
22609
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("h1", { children: showTabs ? "数据与同步" : "连接随手记账本" })
22425
22610
  }),
22426
- showTabs && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("nav", {
22427
- className: "ledger-sync-tabs",
22428
- "aria-label": "数据与同步分区",
22429
- children: [
22430
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
22431
- type: "button",
22432
- "aria-current": "page",
22433
- children: "同步状态"
22434
- }),
22435
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
22436
- type: "button",
22437
- disabled: true,
22438
- children: "流水变更"
22439
- }),
22440
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
22441
- type: "button",
22442
- disabled: true,
22443
- children: "汇率与计算"
22444
- }),
22445
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
22446
- type: "button",
22447
- onClick: () => onNavigate({
22448
- section: "sync",
22449
- query: { view: "backup" }
22450
- }),
22451
- children: "备份与恢复"
22452
- })
22453
- ]
22611
+ showTabs && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LedgerSyncTabs, {
22612
+ view: "status",
22613
+ onNavigate,
22614
+ busy
22454
22615
  }),
22455
22616
  /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
22456
22617
  className: "ledger-card first-run-card",
@@ -22558,6 +22719,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22558
22719
  });
22559
22720
  }
22560
22721
  var init_first_run_setup = __esmMin((() => {
22722
+ init_ledger_sync_tabs();
22561
22723
  init_src$2();
22562
22724
  init_ledger_sync_progress();
22563
22725
  init_session_recovery();
@@ -22622,7 +22784,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22622
22784
  return () => controller.abort();
22623
22785
  }, [fetchImpl]);
22624
22786
  (0, react.useEffect)(() => {
22625
- if (loading || props.route.section === "preferences" || props.route.query?.view === "backup") return;
22787
+ if (loading || props.route.section === "preferences" || ["backup", "authorization"].includes(props.route.query?.view ?? "")) return;
22626
22788
  if (books.length === 0) return;
22627
22789
  const requestedBook = props.route.bookId;
22628
22790
  if (requestedBook && !books.some((item) => item.id === requestedBook)) return;
@@ -22734,6 +22896,11 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22734
22896
  }) : section === "preferences" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(PersonalPreferencesPage, {
22735
22897
  onDirtyChange: setPreferencesDirty,
22736
22898
  onSavingChange: setPreferencesSaving
22899
+ }) : section === "sync" && props.route.query?.view === "authorization" && !loading ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(BookAuthorizationPage, {
22900
+ client: props.financeClient,
22901
+ books,
22902
+ bookId: bookId || void 0,
22903
+ onNavigate: props.onNavigate
22737
22904
  }) : loading ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LedgerState, {
22738
22905
  icon: "refresh",
22739
22906
  title: "正在读取账本",
@@ -22858,6 +23025,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22858
23025
  init_local_ledger_visuals();
22859
23026
  init_backup_recovery();
22860
23027
  init_service_records_page();
23028
+ init_book_authorization();
22861
23029
  init_first_run_setup();
22862
23030
  LEDGER_BOOK_KEY = "sui-ledger.local-ledger.book";
22863
23031
  LEDGER_SECTION_KEY = "sui-ledger.local-ledger.section";
@@ -22945,6 +23113,13 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
22945
23113
  description: "备份与恢复。",
22946
23114
  loadPage: stubLoadPage
22947
23115
  },
23116
+ {
23117
+ id: FinanceRouteIds.authorization,
23118
+ path: "/ledger/authorization",
23119
+ ownerModuleId: OWNER_MODULE_ID,
23120
+ description: "账本授权。",
23121
+ loadPage: stubLoadPage
23122
+ },
22948
23123
  {
22949
23124
  id: FinanceRouteIds.setup,
22950
23125
  path: "/ledger/setup",
@@ -23451,11 +23626,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23451
23626
  init_src$2();
23452
23627
  /** DSH's session-scoped input dock stays mounted even while the icon is absent. */
23453
23628
  function DraftComposerDock(props) {
23454
- return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(BookGrantControl, {
23455
- subjectId: `dsh-session-${props.sessionId}`,
23456
- client: props.financeClient,
23457
- onOpenLedger: props.onOpenLedger
23458
- }, `grant-${props.sessionId}`), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SessionDraftDock, { ...props }, props.sessionId)] });
23629
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SessionDraftDock, { ...props }, props.sessionId);
23459
23630
  }
23460
23631
  function SessionDraftDock(props) {
23461
23632
  const timeline = props.useChat((snapshot) => snapshot.timeline);
@@ -23790,6 +23961,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23790
23961
  function DraftAddToolview({ block }) {
23791
23962
  const result = asRecord(parseResult(block));
23792
23963
  const counts = asRecord(result?.counts);
23964
+ const state = blockState(block);
23793
23965
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(Frame, {
23794
23966
  title: "流水草稿 · 追加",
23795
23967
  block,
@@ -23815,7 +23987,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23815
23987
  }) : null,
23816
23988
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
23817
23989
  k: "提示",
23818
- v: blockState(block) === "ok" && result?.draftId ? "已加到会话草稿便签,可从输入框上方的草稿图标打开;未入账。" : "尚未追加成功,请查看调用结果。"
23990
+ v: blockState(block) === "ok" && result?.draftId ? "已加到会话草稿便签,可从输入框上方的草稿图标打开;未入账。" : state === "running" || state === "preparing" ? "正在处理,请稍候。" : "尚未追加成功,请查看调用结果。"
23819
23991
  })
23820
23992
  ]
23821
23993
  });
@@ -23972,10 +24144,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
23972
24144
  id: "sui-ledger-draft",
23973
24145
  order: 15,
23974
24146
  locale: NS,
23975
- inject: () => ({
23976
- financeClient,
23977
- onOpenLedger: () => ctx.layout.selectPanel(PANEL_ID)
23978
- })
24147
+ inject: () => ({ financeClient })
23979
24148
  }, DraftComposerDock));
23980
24149
  for (const { key, component } of TOOLVIEWS) ctx.slots.inject("tool.call.toolview", () => ctx.slots.register({
23981
24150
  name: "tool.call.toolview",