app-queued-inputs.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import type { TuiCopy } from "@zcode/i18n";
  2. import React from "react";
  3. import { DEFAULT_TUI_COPY } from "./app-locale.js";
  4. import type { QueuedInput } from "./app-model.js";
  5. import { palette } from "./app-model.js";
  6. import { displayWidth, truncateDisplay } from "./app-terminal-width.js";
  7. const h = React.createElement as (
  8. type: React.ElementType | string,
  9. props?: Record<string, unknown> | null,
  10. ...children: React.ReactNode[]
  11. ) => React.ReactElement;
  12. const QUEUED_INPUT_VISIBLE_COUNT = 3;
  13. const QUEUED_INPUT_PANEL_CHROME_ROWS = 2;
  14. const QUEUED_INPUT_HINT_ROWS = 1;
  15. const QUEUED_INPUT_ROW_HEIGHT = 1;
  16. const QUEUED_INPUT_INDEX_WIDTH = 4;
  17. const QUEUED_INPUT_FALLBACK_CONTENT_WIDTH = 80;
  18. const QUEUED_INPUT_MIN_CONTENT_WIDTH = 12;
  19. export function QueuedInputPanel({
  20. contentWidth,
  21. copy = DEFAULT_TUI_COPY,
  22. inputs,
  23. }: {
  24. contentWidth?: number;
  25. copy?: TuiCopy;
  26. inputs: QueuedInput[];
  27. }): React.ReactElement | null {
  28. if (inputs.length === 0) return null;
  29. const visible = inputs.slice(0, QUEUED_INPUT_VISIBLE_COUNT);
  30. const hiddenCount = Math.max(0, inputs.length - visible.length);
  31. const rowContentWidth = normalizeQueuedInputContentWidth(contentWidth);
  32. return h(
  33. "box",
  34. {
  35. title: copy.input.queuedTitle(inputs.length),
  36. style: {
  37. backgroundColor: palette.panel,
  38. border: true,
  39. borderColor: palette.border,
  40. flexDirection: "column",
  41. height: queuedInputPanelHeight(inputs.length),
  42. marginBottom: 1,
  43. paddingLeft: 1,
  44. paddingRight: 1,
  45. width: "100%",
  46. },
  47. },
  48. h("text", { key: "hint", style: { fg: palette.muted } }, copy.input.queuedSubmitHint),
  49. ...visible.map((input, index) =>
  50. h(QueuedInputRow, {
  51. contentWidth: rowContentWidth,
  52. input,
  53. key: input.id,
  54. position: index + 1,
  55. }),
  56. ),
  57. hiddenCount > 0
  58. ? h("text", { key: "more", style: { fg: palette.muted } }, copy.input.queuedMore(hiddenCount))
  59. : null,
  60. );
  61. }
  62. export function upsertQueuedInput(
  63. current: QueuedInput[],
  64. input: QueuedInput,
  65. options: { preserveExistingText?: boolean } = {},
  66. ): QueuedInput[] {
  67. const existingIndex = current.findIndex((item) => item.id === input.id);
  68. if (existingIndex === -1) return [...current, input];
  69. const existing = current[existingIndex]!;
  70. const nextInput = {
  71. ...existing,
  72. ...input,
  73. text: options.preserveExistingText && existing.text ? existing.text : input.text,
  74. };
  75. if (existing.id === nextInput.id && existing.text === nextInput.text) return current;
  76. return [
  77. ...current.slice(0, existingIndex),
  78. nextInput,
  79. ...current.slice(existingIndex + 1),
  80. ];
  81. }
  82. export function removeQueuedInputs(current: QueuedInput[], ids: readonly string[]): QueuedInput[] {
  83. if (ids.length === 0) return current;
  84. const idsToRemove = new Set(ids);
  85. return current.filter((input) => !idsToRemove.has(input.id));
  86. }
  87. function queuedInputPanelHeight(inputCount: number): number {
  88. const visibleCount = Math.min(Math.max(0, inputCount), QUEUED_INPUT_VISIBLE_COUNT);
  89. const hiddenRows = inputCount > visibleCount ? 1 : 0;
  90. return (
  91. QUEUED_INPUT_PANEL_CHROME_ROWS +
  92. QUEUED_INPUT_HINT_ROWS +
  93. visibleCount * QUEUED_INPUT_ROW_HEIGHT +
  94. hiddenRows
  95. );
  96. }
  97. function QueuedInputRow({
  98. contentWidth,
  99. input,
  100. position,
  101. }: {
  102. contentWidth: number;
  103. input: QueuedInput;
  104. position: number;
  105. }): React.ReactElement {
  106. const indexLabel = `${position}.`;
  107. const textWidth = Math.max(
  108. QUEUED_INPUT_MIN_CONTENT_WIDTH,
  109. contentWidth - QUEUED_INPUT_INDEX_WIDTH,
  110. );
  111. return h(
  112. "box",
  113. {
  114. style: {
  115. alignItems: "center",
  116. flexDirection: "row",
  117. height: QUEUED_INPUT_ROW_HEIGHT,
  118. width: "100%",
  119. },
  120. },
  121. h(
  122. "text",
  123. {
  124. style: {
  125. fg: palette.accent,
  126. flexShrink: 0,
  127. width: QUEUED_INPUT_INDEX_WIDTH,
  128. },
  129. },
  130. indexLabel.padEnd(QUEUED_INPUT_INDEX_WIDTH),
  131. ),
  132. h(
  133. "text",
  134. {
  135. style: {
  136. fg: palette.text,
  137. flexShrink: 1,
  138. minWidth: 1,
  139. width: displayWidth(truncateDisplay(input.text, textWidth)),
  140. },
  141. },
  142. truncateDisplay(input.text, textWidth),
  143. ),
  144. );
  145. }
  146. function normalizeQueuedInputContentWidth(contentWidth?: number): number {
  147. if (contentWidth === undefined || !Number.isFinite(contentWidth)) {
  148. return QUEUED_INPUT_FALLBACK_CONTENT_WIDTH;
  149. }
  150. return Math.max(QUEUED_INPUT_MIN_CONTENT_WIDTH, Math.floor(contentWidth));
  151. }