app-file-mention-panel.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import type { TuiCopy } from "@zcode/i18n";
  2. import React from "react";
  3. import {
  4. FILE_MENTION_VISIBLE_COUNT,
  5. type FileMentionState,
  6. visibleFileMentionWindow,
  7. } from "./app-file-mentions.js";
  8. import { DEFAULT_TUI_COPY } from "./app-locale.js";
  9. import { palette } from "./app-model.js";
  10. import { wordWrappedLineCount } from "./app-terminal-width.js";
  11. const h = React.createElement as (
  12. type: React.ElementType | string,
  13. props?: Record<string, unknown> | null,
  14. ...children: React.ReactNode[]
  15. ) => React.ReactElement;
  16. const FILE_MENTION_PANEL_CHROME_ROWS = 2;
  17. const FILE_MENTION_FALLBACK_CONTENT_WIDTH = 80;
  18. const MIN_FILE_MENTION_CONTENT_WIDTH = 8;
  19. export function FileMentionPanel({
  20. contentWidth,
  21. copy = DEFAULT_TUI_COPY,
  22. state,
  23. }: {
  24. contentWidth?: number;
  25. copy?: TuiCopy;
  26. state: FileMentionState;
  27. }): React.ReactElement | null {
  28. const rowContentWidth = normalizeFileMentionContentWidth(contentWidth);
  29. const visible = visibleFileMentionWindow(
  30. state.items,
  31. state.selectedIndex,
  32. FILE_MENTION_VISIBLE_COUNT,
  33. );
  34. const rows =
  35. visible.items.length > 0
  36. ? visible.items.map((item, index) => {
  37. const text = copy.fileMention.row({
  38. path: item.path,
  39. selected: index === visible.selectedIndex,
  40. });
  41. return {
  42. item,
  43. selected: index === visible.selectedIndex,
  44. text,
  45. height: wordWrappedLineCount(text, rowContentWidth),
  46. };
  47. })
  48. : [
  49. {
  50. item: undefined,
  51. selected: false,
  52. text: state.loading ? copy.fileMention.loading : copy.fileMention.empty,
  53. height: 1,
  54. },
  55. ];
  56. const panelHeight =
  57. FILE_MENTION_PANEL_CHROME_ROWS + rows.reduce((height, row) => height + row.height, 0);
  58. return h(
  59. "box",
  60. {
  61. title: copy.fileMention.title,
  62. style: {
  63. backgroundColor: palette.panel,
  64. border: true,
  65. borderColor: palette.border,
  66. flexDirection: "column",
  67. height: panelHeight,
  68. marginBottom: 1,
  69. paddingLeft: 1,
  70. paddingRight: 1,
  71. width: "100%",
  72. },
  73. },
  74. ...rows.map((row, index) =>
  75. h(
  76. "text",
  77. {
  78. key: row.item?.path ?? `file-mention-${index}`,
  79. style: {
  80. fg: row.selected ? palette.accent : row.item ? palette.text : palette.warning,
  81. height: row.height,
  82. width: "100%",
  83. wrapMode: "word",
  84. },
  85. },
  86. row.text,
  87. ),
  88. ),
  89. );
  90. }
  91. function normalizeFileMentionContentWidth(contentWidth: number | undefined): number {
  92. if (contentWidth === undefined || !Number.isFinite(contentWidth)) {
  93. return FILE_MENTION_FALLBACK_CONTENT_WIDTH;
  94. }
  95. return Math.max(MIN_FILE_MENTION_CONTENT_WIDTH, Math.floor(contentWidth));
  96. }