Skip to content

Commit

Permalink
chore: update shortcut, put focus in textarea on first load
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Nov 25, 2024
1 parent 2dfe3b9 commit 21c83f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
11 changes: 6 additions & 5 deletions src/components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ export const ChatInputComponent = ({keyboardShortcutEnabled, onKeyboardShortcut,
// };

const addShortcutListener = useCallback((context: Window) => {
const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
const handler = (event: KeyboardEvent) => {
if (
(isMac && event.metaKey && event.ctrlKey && event.key === "a") || // Ctrl + Cmd + a on macOS
(!isMac && event.ctrlKey && event.altKey && event.key === "a") // Ctrl + Alt + a on Windows/Linux
) {
if (event.ctrlKey && event.shiftKey && event.key === "/") {
const activeElement = context.document.activeElement;
if (isInputElement(activeElement)) return;

Expand Down Expand Up @@ -136,6 +132,11 @@ export const ChatInputComponent = ({keyboardShortcutEnabled, onKeyboardShortcut,
};
}, [addShortcutListener, keyboardShortcutEnabled]);

// Place focus on the textarea when the component mounts.
useEffect(() => {
textAreaRef.current?.focus();
}, []);

return (
<div className="chat-input" data-testid="chat-input">
<form onSubmit={handleSubmit}>
Expand Down
38 changes: 19 additions & 19 deletions src/components/keyboard-shortcut-controls.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import React, { FormEvent, useState } from "react";
import React from "react";

import "./keyboard-shortcut-controls.scss";

interface IProps {
shortcutEnabled: boolean;
shortcutKeys: string;
shortcutKeys?: string;
onCustomizeShortcut?: (shortcut: string) => void;
onToggleShortcut: () => void;
}

export const KeyboardShortcutControls = (props: IProps) => {
const { shortcutEnabled, shortcutKeys, onCustomizeShortcut, onToggleShortcut } = props;
const { shortcutEnabled, onToggleShortcut } = props;
const toggleButtonLabel = shortcutEnabled ? "Disable Keyboard Shortcut" : "Enable Keyboard Shortcut";
const [showError, setShowError] = useState(false);
// const [showError, setShowError] = useState(false);

const handleToggleShortcut = () => {
onToggleShortcut();
};

const handleCustomizeShortcut = (event: FormEvent) => {
event.preventDefault();
const form = event.target as HTMLInputElement;
// get the text value from the form's input element
const shortcut = form.querySelector("input")?.value;
console.log("Customizing shortcut to", shortcut);
if (shortcut) {
onCustomizeShortcut?.(shortcut);
setShowError(false);
} else {
setShowError(true);
}
};
// const handleCustomizeShortcut = (event: FormEvent) => {
// event.preventDefault();
// const form = event.target as HTMLInputElement;
// // get the text value from the form's input element
// const shortcut = form.querySelector("input")?.value;
// console.log("Customizing shortcut to", shortcut);
// if (shortcut) {
// onCustomizeShortcut?.(shortcut);
// setShowError(false);
// } else {
// setShowError(true);
// }
// };

return (
<div className="keyboard-shortcut-controls">
<h3>Keyboard Shortcut</h3>
<button onClick={handleToggleShortcut}>
{toggleButtonLabel}
</button>
<form onSubmit={handleCustomizeShortcut}>
{/* <form onSubmit={handleCustomizeShortcut}>
<fieldset disabled={!shortcutEnabled}>
<label htmlFor="custom-keyboard-shortcut">Customize Keyboard Shortcut</label>
<input
Expand All @@ -61,7 +61,7 @@ export const KeyboardShortcutControls = (props: IProps) => {
</div>
}
</fieldset>
</form>
</form> */}
</div>
);
};

0 comments on commit 21c83f0

Please sign in to comment.