add option to add all open files

This commit is contained in:
Test 2024-11-21 17:24:37 -08:00
parent e0704b273b
commit f5684d7ca1
2 changed files with 82 additions and 17 deletions

View file

@ -0,0 +1,79 @@
import { useContext, useState, useRef, useEffect } from "react";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import { setShouldAddFileForEditing } from "../../redux/slices/uiStateSlice";
import { useDispatch } from "react-redux";
import { ChevronDownIcon, PlusIcon } from "@heroicons/react/24/outline";
import { addCodeToEdit } from "../../redux/slices/editModeState";
import { Listbox } from "@headlessui/react";
export default function AddFileButton() {
const [showAbove, setShowAbove] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const ideMessenger = useContext(IdeMessengerContext);
const dispatch = useDispatch();
function onClickAddFileToCodeToEdit() {
dispatch(setShouldAddFileForEditing(true));
}
async function handleAddAllOpenFiles() {
const openFiles = await ideMessenger.ide.getOpenFiles();
for (const filepath of openFiles) {
const contents = await ideMessenger.ide.readFile(filepath);
console.log({ filepath, contents });
dispatch(addCodeToEdit({ filepath, contents }));
}
}
// function calculatePosition() {
// const rect = buttonRef.current?.getBoundingClientRect();
// if (!rect) return;
// const spaceBelow = window.innerHeight - rect.bottom;
// const spaceAbove = rect.top;
// const dropdownHeight = 300;
// setShowAbove(spaceBelow < dropdownHeight && spaceAbove > spaceBelow);
// }
return (
<Listbox onChange={handleAddAllOpenFiles}>
<div className="relative">
<Listbox.Button
ref={buttonRef}
// onClick={calculatePosition}
className="bg-lightgray/10 border-lightgray flex cursor-pointer items-center justify-between rounded-md border border-solid px-0 shadow-sm transition-colors"
>
<div
className="flex items-center gap-1 px-1.5 hover:brightness-125"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onClickAddFileToCodeToEdit();
}}
>
<PlusIcon className="text-vsc-foreground inline h-2.5 w-2.5" />
<span className="text-vsc-foreground text-[10px]">Add file</span>
</div>
<div className="border-lightgray h-4 w-[1px] border-y-0 border-l-0 border-r border-solid" />
<ChevronDownIcon className="text-vsc-foreground h-2.5 w-2.5 cursor-pointer px-1 hover:brightness-125" />
</Listbox.Button>
<Listbox.Options
className={`bg-vsc-input-background border-lightgray absolute right-0 z-50 min-w-fit whitespace-nowrap rounded-md border border-solid px-1 py-0 shadow-lg ${
showAbove ? "bottom-full mb-1" : "top-full mt-1"
}`}
>
<Listbox.Option
value="addAllFiles"
className="text-vsc-foreground hover:bg-lightgray/33 block w-full cursor-pointer px-2 py-1 text-left text-[10px]"
>
Add all open files
</Listbox.Option>
</Listbox.Options>
</div>
</Listbox>
);
}

View file

@ -1,13 +1,12 @@
import { PlusIcon } from "@heroicons/react/24/outline";
import { useContext } from "react";
import { useDispatch, useSelector } from "react-redux";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import { removeCodeToEdit } from "../../redux/slices/editModeState";
import { RootState } from "../../redux/store";
import CodeToEditListItem from "./CodeToEditListItem";
import { vscEditorBackground } from "../../components";
import { setShouldAddFileForEditing } from "../../redux/slices/uiStateSlice";
import { CodeToEdit, RangeInFileWithContents } from "core";
import AddFileButton from "./AddFileButton";
export default function WorkingSet() {
const dispatch = useDispatch();
@ -16,8 +15,6 @@ export default function WorkingSet() {
const hasCodeToEdit = editModeState.codeToEdit.length > 0;
const addFileText = hasCodeToEdit ? "Add file" : "Add a file to get started";
const title =
editModeState.codeToEdit.length === 0
? "Code to edit"
@ -29,10 +26,6 @@ export default function WorkingSet() {
dispatch(removeCodeToEdit(rif));
}
function onClickAddFileToCodeToEdit() {
dispatch(setShouldAddFileForEditing(true));
}
function onClickFilename(code: CodeToEdit) {
if ("range" in code) {
ideMessenger.ide.showLines(
@ -60,14 +53,7 @@ export default function WorkingSet() {
<div className="bg-vsc-editor-background border-vsc-input-border mx-1 flex flex-col rounded-t-lg border border-b-0 border-solid p-1">
<div className="text-lightgray flex items-center justify-between gap-1.5 px-1 py-1.5 text-xs">
<span>{title}</span>
<span
className="hover:bg-lightgray hover:text-vsc-foreground flex cursor-pointer items-center justify-between gap-1 rounded px-1 py-0.5 transition-colors hover:bg-opacity-20"
onClick={onClickAddFileToCodeToEdit}
>
<PlusIcon className="inline h-3 w-3" />
<span>{addFileText}</span>
</span>
<AddFileButton />
</div>
{hasCodeToEdit && (