continue-custom/gui/src/hooks/useInputHistory.ts
Nate Sesti 683d53bcc2
Free Trial Auth (#1367)
* 🎨 pass gh auth token to free trial

* 💄 improved onboarding flow

* 🎨 make fewer auth reqs

* 🎨 refactor ordering of vscode deps

* 🐛 resolve confighandler
2024-05-26 11:33:46 -07:00

71 lines
2 KiB
TypeScript

import { JSONContent } from "@tiptap/react";
import { useState } from "react";
import { getLocalStorage, setLocalStorage } from "../util/localStorage";
import useUpdatingRef from "./useUpdatingRef";
const emptyJsonContent = () => ({
type: "doc",
content: [{ type: "paragraph", content: [{ type: "text", text: "" }] }],
});
const MAX_HISTORY_LENGTH = 100;
export function useInputHistory() {
const [inputHistory, setInputHistory] = useState<JSONContent[]>(
getLocalStorage("inputHistory")?.slice(-MAX_HISTORY_LENGTH) ?? [],
);
const [pendingInput, setPendingInput] =
useState<JSONContent>(emptyJsonContent());
const [currentIndex, setCurrentIndex] = useState(inputHistory.length);
function prev(currentInput: JSONContent) {
let index = currentIndex;
if (index === inputHistory.length) {
setPendingInput(currentInput);
}
if (index > 0 && index <= inputHistory.length) {
setCurrentIndex((prevState) => prevState - 1);
return inputHistory[index - 1];
}
}
function next() {
let index = currentIndex;
if (index >= 0 && index < inputHistory.length) {
setCurrentIndex((prevState) => prevState + 1);
if (index === inputHistory.length - 1) {
return pendingInput;
}
return inputHistory[index + 1];
}
}
function add(inputValue: JSONContent) {
setPendingInput(emptyJsonContent());
if (
JSON.stringify(inputHistory[inputHistory.length - 1]) ===
JSON.stringify(inputValue)
) {
setCurrentIndex(inputHistory.length);
return;
} else {
setCurrentIndex(inputHistory.length + 1);
}
setInputHistory((prev) => {
return [...prev, inputValue].slice(-MAX_HISTORY_LENGTH);
});
setLocalStorage(
"inputHistory",
[...inputHistory, inputValue].slice(-MAX_HISTORY_LENGTH),
);
}
const prevRef = useUpdatingRef(prev, [inputHistory]);
const nextRef = useUpdatingRef(next, [inputHistory]);
const addRef = useUpdatingRef(add, [inputHistory]);
return { prevRef, nextRef, addRef };
}