Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ function MainApp() {
closeSettings,
} = useSettingsModalState();
const composerInputRef = useRef<HTMLTextAreaElement | null>(null);
const workspaceHomeTextareaRef = useRef<HTMLTextAreaElement | null>(null);

const getWorkspaceName = useCallback(
(workspaceId: string) => workspacesById.get(workspaceId)?.name,
Expand Down Expand Up @@ -1062,13 +1063,6 @@ function MainApp() {
startStatus,
});

const handleInsertComposerText = useComposerInsert({
activeThreadId,
draftText: activeDraft,
onDraftChange: handleDraftChange,
textareaRef: composerInputRef,
});

const {
runs: workspaceRuns,
draft: workspacePrompt,
Expand All @@ -1093,6 +1087,16 @@ function MainApp() {
sendUserMessageToThread,
onWorktreeCreated: handleWorktreeCreated,
});

const canInsertComposerText = showWorkspaceHome
? Boolean(activeWorkspace)
: Boolean(activeThreadId);
const handleInsertComposerText = useComposerInsert({
isEnabled: canInsertComposerText,
draftText: showWorkspaceHome ? workspacePrompt : activeDraft,
onDraftChange: showWorkspaceHome ? setWorkspacePrompt : handleDraftChange,
textareaRef: showWorkspaceHome ? workspaceHomeTextareaRef : composerInputRef,
});
const RECENT_THREAD_LIMIT = 8;
const { recentThreadInstances, recentThreadsUpdatedAt } = useMemo(() => {
if (!activeWorkspaceId) {
Expand Down Expand Up @@ -1928,6 +1932,7 @@ function MainApp() {
prompts,
files,
onInsertComposerText: handleInsertComposerText,
canInsertComposerText,
textareaRef: composerInputRef,
composerEditorSettings,
composerEditorExpanded,
Expand Down Expand Up @@ -2018,6 +2023,7 @@ function MainApp() {
onDismissDictationHint={clearDictationHint}
dictationTranscript={dictationTranscript}
onDictationTranscriptHandled={clearDictationTranscript}
textareaRef={workspaceHomeTextareaRef}
agentMdContent={agentMdContent}
agentMdExists={agentMdExists}
agentMdTruncated={agentMdTruncated}
Expand Down
55 changes: 55 additions & 0 deletions src/features/app/hooks/useComposerInsert.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import type { RefObject } from "react";
import { useComposerInsert } from "./useComposerInsert";

describe("useComposerInsert", () => {
it("inserts text when enabled", () => {
const textarea = document.createElement("textarea");
textarea.value = "Hello";
textarea.selectionStart = 5;
textarea.selectionEnd = 5;
const textareaRef: RefObject<HTMLTextAreaElement> = { current: textarea };
const onDraftChange = vi.fn();

const { result } = renderHook(() =>
useComposerInsert({
isEnabled: true,
draftText: "Hello",
onDraftChange,
textareaRef,
}),
);

act(() => {
result.current("./src");
});

expect(onDraftChange).toHaveBeenCalledWith("Hello ./src");
});

it("does nothing when disabled", () => {
const textarea = document.createElement("textarea");
textarea.value = "Hello";
textarea.selectionStart = 5;
textarea.selectionEnd = 5;
const textareaRef: RefObject<HTMLTextAreaElement> = { current: textarea };
const onDraftChange = vi.fn();

const { result } = renderHook(() =>
useComposerInsert({
isEnabled: false,
draftText: "Hello",
onDraftChange,
textareaRef,
}),
);

act(() => {
result.current("./src");
});

expect(onDraftChange).not.toHaveBeenCalled();
});
});
8 changes: 4 additions & 4 deletions src/features/app/hooks/useComposerInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { useCallback } from "react";
import type { RefObject } from "react";

type UseComposerInsertArgs = {
activeThreadId: string | null;
isEnabled: boolean;
draftText: string;
onDraftChange: (next: string) => void;
textareaRef: RefObject<HTMLTextAreaElement | null>;
};

export function useComposerInsert({
activeThreadId,
isEnabled,
draftText,
onDraftChange,
textareaRef,
}: UseComposerInsertArgs) {
return useCallback(
(insertText: string) => {
if (!activeThreadId) {
if (!isEnabled) {
return;
}
const textarea = textareaRef.current;
Expand Down Expand Up @@ -46,6 +46,6 @@ export function useComposerInsert({
node.dispatchEvent(new Event("select", { bubbles: true }));
});
},
[activeThreadId, draftText, onDraftChange, textareaRef],
[isEnabled, draftText, onDraftChange, textareaRef],
);
}
26 changes: 26 additions & 0 deletions src/features/files/components/FilePreviewPopover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,30 @@ describe("FilePreviewPopover", () => {
expect(onLineMouseUp).toHaveBeenCalledWith(1, expect.any(Object));
expect(onSelectLine).toHaveBeenCalledWith(1, expect.any(Object));
});

it("disables add-to-chat when insertion is not allowed", () => {
render(
<FilePreviewPopover
path="src/example.ts"
absolutePath="/workspace/src/example.ts"
content={"one\ntwo"}
truncated={false}
previewKind="text"
imageSrc={null}
openTargets={[]}
openAppIconById={{}}
selectedOpenAppId=""
onSelectOpenAppId={vi.fn()}
selection={{ start: 0, end: 0 }}
onSelectLine={vi.fn()}
onClearSelection={vi.fn()}
onAddSelection={vi.fn()}
onClose={vi.fn()}
canInsertText={false}
/>,
);

const addButton = screen.getByRole("button", { name: "Add to chat" });
expect(addButton.hasAttribute("disabled")).toBe(true);
});
});
4 changes: 3 additions & 1 deletion src/features/files/components/FilePreviewPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type FilePreviewPopoverProps = {
onLineMouseUp?: (index: number, event: MouseEvent<HTMLButtonElement>) => void;
onClearSelection: () => void;
onAddSelection: () => void;
canInsertText?: boolean;
onClose: () => void;
selectionHints?: string[];
style?: CSSProperties;
Expand All @@ -48,6 +49,7 @@ export function FilePreviewPopover({
onLineMouseUp,
onClearSelection,
onAddSelection,
canInsertText = true,
onClose,
selectionHints = [],
style,
Expand Down Expand Up @@ -158,7 +160,7 @@ export function FilePreviewPopover({
type="button"
className="primary file-preview-action file-preview-action--add"
onClick={onAddSelection}
disabled={!selection}
disabled={!selection || !canInsertText}
>
Add to chat
</button>
Expand Down
34 changes: 28 additions & 6 deletions src/features/files/components/FileTreePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type FileTreePanelProps = {
filePanelMode: PanelTabId;
onFilePanelModeChange: (mode: PanelTabId) => void;
onInsertText?: (text: string) => void;
canInsertText: boolean;
openTargets: OpenAppTarget[];
openAppIconById: Record<string, string>;
selectedOpenAppId: string;
Expand Down Expand Up @@ -225,6 +226,7 @@ export function FileTreePanel({
filePanelMode,
onFilePanelModeChange,
onInsertText,
canInsertText,
openTargets,
openAppIconById,
selectedOpenAppId,
Expand Down Expand Up @@ -537,7 +539,13 @@ export function FileTreePanel({
);

const handleAddSelection = useCallback(() => {
if (previewKind !== "text" || !previewPath || !previewSelection || !onInsertText) {
if (
!canInsertText ||
previewKind !== "text" ||
!previewPath ||
!previewSelection ||
!onInsertText
) {
return;
}
const lines = previewContent.split("\n");
Expand All @@ -551,6 +559,7 @@ export function FileTreePanel({
onInsertText(snippet);
closePreview();
}, [
canInsertText,
previewContent,
previewKind,
previewPath,
Expand All @@ -559,12 +568,22 @@ export function FileTreePanel({
closePreview,
]);

const showFileMenu = useCallback(
const showMenu = useCallback(
async (event: MouseEvent<HTMLButtonElement>, relativePath: string) => {
event.preventDefault();
event.stopPropagation();
const menu = await Menu.new({
items: [
await MenuItem.new({
text: "Add to chat",
enabled: canInsertText,
action: async () => {
if (!canInsertText) {
return;
}
onInsertText?.(relativePath);
},
}),
await MenuItem.new({
text: "Reveal in Finder",
action: async () => {
Expand All @@ -577,7 +596,7 @@ export function FileTreePanel({
const position = new LogicalPosition(event.clientX, event.clientY);
await menu.popup(position, window);
},
[resolvePath],
[canInsertText, onInsertText, resolvePath],
);

const renderNode = (node: FileTreeNode, depth: number) => {
Expand All @@ -599,9 +618,7 @@ export function FileTreePanel({
openPreview(node.path, event.currentTarget);
}}
onContextMenu={(event) => {
if (!isFolder) {
void showFileMenu(event, node.path);
}
void showMenu(event, node.path);
}}
>
{isFolder ? (
Expand All @@ -622,8 +639,12 @@ export function FileTreePanel({
className="ghost icon-button file-tree-action"
onClick={(event) => {
event.stopPropagation();
if (!canInsertText) {
return;
}
onInsertText?.(node.path);
}}
disabled={!canInsertText}
aria-label={`Mention ${node.name}`}
title="Mention in chat"
>
Expand Down Expand Up @@ -717,6 +738,7 @@ export function FileTreePanel({
onLineMouseUp={handleLineMouseUp}
onClearSelection={() => setPreviewSelection(null)}
onAddSelection={handleAddSelection}
canInsertText={canInsertText}
onClose={closePreview}
selectionHints={selectionHints}
style={{
Expand Down
2 changes: 2 additions & 0 deletions src/features/layout/hooks/useLayoutNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ type LayoutNodesOptions = {
prompts: CustomPromptOption[];
files: string[];
onInsertComposerText: (text: string) => void;
canInsertComposerText: boolean;
textareaRef: RefObject<HTMLTextAreaElement | null>;
composerEditorSettings: ComposerEditorSettings;
composerEditorExpanded: boolean;
Expand Down Expand Up @@ -722,6 +723,7 @@ export function useLayoutNodes(options: LayoutNodesOptions): LayoutNodesResult {
filePanelMode={options.filePanelMode}
onFilePanelModeChange={options.onFilePanelModeChange}
onInsertText={options.onInsertComposerText}
canInsertText={options.canInsertComposerText}
openTargets={options.openAppTargets}
openAppIconById={options.openAppIconById}
selectedOpenAppId={options.selectedOpenAppId}
Expand Down
6 changes: 5 additions & 1 deletion src/features/workspaces/components/WorkspaceHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
useState,
type CSSProperties,
type KeyboardEvent,
type RefObject,
} from "react";
import { convertFileSrc } from "@tauri-apps/api/core";
import type {
Expand Down Expand Up @@ -84,6 +85,7 @@
onDismissDictationHint: () => void;
dictationTranscript: DictationTranscript | null;
onDictationTranscriptHandled: (id: string) => void;
textareaRef?: RefObject<HTMLTextAreaElement | null>;
agentMdContent: string;
agentMdExists: boolean;
agentMdTruncated: boolean;
Expand Down Expand Up @@ -159,6 +161,7 @@
onDismissDictationHint,
dictationTranscript,
onDictationTranscriptHandled,
textareaRef: textareaRefProp,
agentMdContent,
agentMdExists,
agentMdTruncated,
Expand All @@ -181,7 +184,8 @@
const iconSrc = useMemo(() => convertFileSrc(iconPath), [iconPath]);
const runModeRef = useRef<HTMLDivElement | null>(null);
const modelsRef = useRef<HTMLDivElement | null>(null);
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const fallbackTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const textareaRef = textareaRefProp ?? fallbackTextareaRef;
const {
activeImages,
attachImages,
Expand Down Expand Up @@ -266,7 +270,7 @@
bottom: "auto",
right: "auto",
});
}, [isAutocompleteOpen, prompt, selectionStart]);

Check warning on line 273 in src/features/workspaces/components/WorkspaceHome.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useLayoutEffect has a missing dependency: 'textareaRef'. Either include it or remove the dependency array

useEffect(() => {
const handleClick = (event: MouseEvent) => {
Expand Down Expand Up @@ -315,7 +319,7 @@
setSelectionStart(nextCursor);
});
onDictationTranscriptHandled(dictationTranscript.id);
}, [

Check warning on line 322 in src/features/workspaces/components/WorkspaceHome.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'textareaRef'. Either include it or remove the dependency array
dictationTranscript,
onDictationTranscriptHandled,
onPromptChange,
Expand Down