Skip to content

Commit

Permalink
fix(chat controls): ignore the state of the controls if they are not …
Browse files Browse the repository at this point in the history
…shown
  • Loading branch information
MarcMcIntosh committed Mar 8, 2024
1 parent 4f6fecf commit 59fb270
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/components/ChatForm/ChatForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ const useControlsState = ({ activeFile, snippet }: useCheckboxStateProps) => {
return "```" + snippet.language + "\n" + snippet.code + "\n```\n";
}, [snippet.language, snippet.code]);

const [checkboxes, setCheckboxes] = React.useState<
ChatControlsProps["checkboxes"]
>({
const defaultState = {
search_workspace: {
name: "search_workspace",
checked: false,
Expand Down Expand Up @@ -91,7 +89,12 @@ const useControlsState = ({ activeFile, snippet }: useCheckboxStateProps) => {
disabled: !snippet.code,
fileName: nameWithLines,
},
} as const);
} as const;

const [checkboxes, setCheckboxes] =
React.useState<ChatControlsProps["checkboxes"]>(defaultState);

const reset = () => setCheckboxes(defaultState);

const toggleCheckbox = useCallback(
(name: string, value: boolean | string) => {
Expand Down Expand Up @@ -183,6 +186,7 @@ const useControlsState = ({ activeFile, snippet }: useCheckboxStateProps) => {
markdown,
nameWithLines,
fullPathWithLines,
reset,
};
};

Expand Down Expand Up @@ -233,7 +237,7 @@ export const ChatForm: React.FC<ChatFormProps> = ({
requestCaps,
}) => {
const [value, setValue] = React.useState("");
const { markdown, checkboxes, toggleCheckbox } = useControlsState({
const { markdown, checkboxes, toggleCheckbox, reset } = useControlsState({
activeFile: attachFile,
snippet: selectedSnippet,
});
Expand All @@ -255,6 +259,10 @@ export const ChatForm: React.FC<ChatFormProps> = ({
]);

const addCheckboxValuesToInput = (input: string) => {
if (!showControls) {
return input;
}

let result = input;
if (!result.endsWith("\n")) {
result += "\n";
Expand Down Expand Up @@ -286,6 +294,12 @@ export const ChatForm: React.FC<ChatFormProps> = ({
}
});

useEffect(() => {
if (!showControls) {
reset();
}
}, [showControls, reset]);

const isOnline = useIsOnline();

const handleSubmit = () => {
Expand Down

0 comments on commit 59fb270

Please sign in to comment.