app-keyboard-suggestions.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type { KeyEvent } from "@mbears/opentui-core";
  2. import type { Dispatch, SetStateAction } from "react";
  3. import { clampIndex } from "./app-input.js";
  4. import type {
  5. EffortCommandSelectionState,
  6. ModeCommandSelectionState,
  7. ModelCommandSelectionState,
  8. SlashCommand,
  9. SlashSelectionState,
  10. } from "./app-model.js";
  11. import type { TuiEffortOption, TuiModeOption, TuiModelOption } from "./types.js";
  12. type SelectionWithIndex = {
  13. selectedIndex: number;
  14. };
  15. type NavigationDelta = -1 | 1;
  16. type SuggestionNavigationInput = {
  17. consumeKey: (key: KeyEvent) => void;
  18. effortSelection: EffortCommandSelectionState | undefined;
  19. filteredEffortOptions: readonly TuiEffortOption[];
  20. filteredModeOptions: readonly TuiModeOption[];
  21. filteredModelOptions: readonly TuiModelOption[];
  22. filteredSlashCommands: readonly SlashCommand[];
  23. key: KeyEvent;
  24. modelSelection: ModelCommandSelectionState | undefined;
  25. modeSelection: ModeCommandSelectionState | undefined;
  26. setEffortSelection: Dispatch<SetStateAction<EffortCommandSelectionState | undefined>>;
  27. setModeSelection: Dispatch<SetStateAction<ModeCommandSelectionState | undefined>>;
  28. setModelSelection: Dispatch<SetStateAction<ModelCommandSelectionState | undefined>>;
  29. setSlashSelection: Dispatch<SetStateAction<SlashSelectionState | undefined>>;
  30. slashSelection: SlashSelectionState | undefined;
  31. };
  32. export function handleSuggestionNavigationKey(input: SuggestionNavigationInput): boolean {
  33. const delta = suggestionNavigationDelta(input.key);
  34. if (delta === undefined) return false;
  35. if (
  36. moveSuggestion({
  37. delta,
  38. optionCount: input.filteredModelOptions.length,
  39. selection: input.modelSelection,
  40. setSelection: input.setModelSelection,
  41. })
  42. ) {
  43. input.consumeKey(input.key);
  44. return true;
  45. }
  46. if (
  47. moveSuggestion({
  48. delta,
  49. optionCount: input.filteredEffortOptions.length,
  50. selection: input.effortSelection,
  51. setSelection: input.setEffortSelection,
  52. })
  53. ) {
  54. input.consumeKey(input.key);
  55. return true;
  56. }
  57. if (
  58. moveSuggestion({
  59. delta,
  60. optionCount: input.filteredModeOptions.length,
  61. selection: input.modeSelection,
  62. setSelection: input.setModeSelection,
  63. })
  64. ) {
  65. input.consumeKey(input.key);
  66. return true;
  67. }
  68. if (
  69. moveSuggestion({
  70. delta,
  71. optionCount: input.filteredSlashCommands.length,
  72. selection: input.slashSelection,
  73. setSelection: input.setSlashSelection,
  74. })
  75. ) {
  76. input.consumeKey(input.key);
  77. return true;
  78. }
  79. return false;
  80. }
  81. function suggestionNavigationDelta(key: KeyEvent): NavigationDelta | undefined {
  82. if (key.name === "up") return -1;
  83. if (key.name === "down") return 1;
  84. return undefined;
  85. }
  86. function moveSuggestion<TSelection extends SelectionWithIndex>({
  87. delta,
  88. optionCount,
  89. selection,
  90. setSelection,
  91. }: {
  92. delta: NavigationDelta;
  93. optionCount: number;
  94. selection: TSelection | undefined;
  95. setSelection: Dispatch<SetStateAction<TSelection | undefined>>;
  96. }): boolean {
  97. if (!selection || optionCount <= 0) return false;
  98. setSelection({
  99. ...selection,
  100. selectedIndex: clampIndex(selection.selectedIndex + delta, optionCount),
  101. });
  102. return true;
  103. }