214a8f5cce
* fix: scope multi-repo file editor open/save to the right repo The editor open/save/sync/delete chain dropped the repository_name in multi-repo task workspaces, so the backend resolved paths against the bare task root and failed with "file not found". Thread the repo subpath through openFile, save, sync, and delete, and store it on FileEditorState. * fix: pass repo from diff Edit button to multi-repo file editor The diff/changes "Edit" button opened files without their repository scope, so multi-repo files resolved against the bare task root and failed with "file not found". Thread repository_name through the diff and review toolbars into onOpenFile. * fix: preserve repo across tab restore and stale syncs Address review feedback: persisted file tabs dropped the repo subpath, so a multi-repo file reopened against the bare task root after a browser refresh. Persist and restore `repo` on StoredFileTab. Also guard the workspace sync against a tab being swapped to a different repo mid-fetch, and widen the remaining onOpenFile type declarations to carry repo. * fix: thread repo into editor panel fallback fetch Address review feedback: FileEditorPanel's useFileLoader fallback fetch (triggered when the open-files map is transiently empty, e.g. after a session switch) called requestFileContent without the repo subpath, re-introducing the multi-repo "file not found" failure. Record repo on the panel params and forward it through useFileLoader. Also assert the full restored-tab shape in the local-storage round-trip test. * fix: guard editor fetches against stale session/repo races Address review feedback: file open and the panel fallback fetch could apply a late response after the user navigated away. openFile now drops the result if the active session changed mid-fetch (via fetchFileEditorState), and the panel loader keys its in-flight guard by session+path+repo so a changed target re-fetches and stale responses are discarded.
153 lines
4.4 KiB
TypeScript
153 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback } from "react";
|
|
import {
|
|
IconArrowBackUp,
|
|
IconCopy,
|
|
IconEye,
|
|
IconFold,
|
|
IconFoldDown,
|
|
IconLayoutColumns,
|
|
IconLayoutRows,
|
|
IconPencil,
|
|
IconTextWrap,
|
|
} from "@tabler/icons-react";
|
|
import { Button } from "@kandev/ui/button";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
|
|
import { FileActionsDropdown } from "@/components/editors/file-actions-dropdown";
|
|
import { useGlobalViewMode } from "@/hooks/use-global-view-mode";
|
|
|
|
const iconBtn = "h-6 w-6 p-0 cursor-pointer opacity-60 hover:opacity-100";
|
|
const iconBtnActive = "h-6 w-6 p-0 cursor-pointer opacity-100 bg-muted";
|
|
|
|
function isMarkdownPath(filePath: string): boolean {
|
|
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
return ext === "md" || ext === "mdx";
|
|
}
|
|
|
|
export type FileDiffToolbarProps = {
|
|
diff: string;
|
|
filePath: string;
|
|
sessionId: string;
|
|
source: string;
|
|
wordWrap: boolean;
|
|
expandUnchanged: boolean;
|
|
onDiscard: () => void;
|
|
onOpenFile?: (filePath: string, repo?: string) => void;
|
|
onPreviewMarkdown?: (filePath: string) => void;
|
|
onToggleExpandUnchanged: () => void;
|
|
onToggleWordWrap: () => void;
|
|
/** Multi-repo subpath (repository_name) so the Edit action opens the file
|
|
* under the right repository instead of the bare task root. */
|
|
repo?: string;
|
|
};
|
|
|
|
function ToolbarIconBtn({
|
|
onClick,
|
|
tooltip,
|
|
active,
|
|
children,
|
|
className,
|
|
}: {
|
|
onClick: () => void;
|
|
tooltip: string;
|
|
active?: boolean;
|
|
children: React.ReactNode;
|
|
className?: string;
|
|
}) {
|
|
return (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
type="button"
|
|
aria-label={tooltip}
|
|
aria-pressed={active}
|
|
variant="ghost"
|
|
size="sm"
|
|
className={className ?? (active ? iconBtnActive : iconBtn)}
|
|
onClick={onClick}
|
|
>
|
|
{children}
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>{tooltip}</TooltipContent>
|
|
</Tooltip>
|
|
);
|
|
}
|
|
|
|
export function FileDiffToolbar(props: FileDiffToolbarProps) {
|
|
const {
|
|
diff,
|
|
filePath,
|
|
sessionId,
|
|
source,
|
|
wordWrap,
|
|
expandUnchanged,
|
|
onDiscard,
|
|
onOpenFile,
|
|
onPreviewMarkdown,
|
|
onToggleExpandUnchanged,
|
|
onToggleWordWrap,
|
|
repo,
|
|
} = props;
|
|
const [globalViewMode, setGlobalViewMode] = useGlobalViewMode();
|
|
const handleCopyDiff = useCallback(() => {
|
|
navigator.clipboard.writeText(diff || "");
|
|
}, [diff]);
|
|
const handleToggleViewMode = useCallback(
|
|
() => setGlobalViewMode(globalViewMode === "split" ? "unified" : "split"),
|
|
[globalViewMode, setGlobalViewMode],
|
|
);
|
|
return (
|
|
<div className="flex items-center gap-0.5">
|
|
<ToolbarIconBtn onClick={handleCopyDiff} tooltip="Copy diff">
|
|
<IconCopy className="h-3.5 w-3.5" />
|
|
</ToolbarIconBtn>
|
|
<ToolbarIconBtn
|
|
onClick={onToggleExpandUnchanged}
|
|
tooltip={expandUnchanged ? "Collapse unchanged" : "Expand all"}
|
|
active={expandUnchanged}
|
|
>
|
|
{expandUnchanged ? (
|
|
<IconFold className="h-3.5 w-3.5" />
|
|
) : (
|
|
<IconFoldDown className="h-3.5 w-3.5" />
|
|
)}
|
|
</ToolbarIconBtn>
|
|
<ToolbarIconBtn onClick={onToggleWordWrap} tooltip="Toggle word wrap" active={wordWrap}>
|
|
<IconTextWrap className="h-3.5 w-3.5" />
|
|
</ToolbarIconBtn>
|
|
<ToolbarIconBtn
|
|
onClick={handleToggleViewMode}
|
|
tooltip={globalViewMode === "split" ? "Switch to unified view" : "Switch to split view"}
|
|
>
|
|
{globalViewMode === "split" ? (
|
|
<IconLayoutRows className="h-3.5 w-3.5" />
|
|
) : (
|
|
<IconLayoutColumns className="h-3.5 w-3.5" />
|
|
)}
|
|
</ToolbarIconBtn>
|
|
{onPreviewMarkdown && isMarkdownPath(filePath) && (
|
|
<ToolbarIconBtn onClick={() => onPreviewMarkdown(filePath)} tooltip="Preview markdown">
|
|
<IconEye className="h-3.5 w-3.5" />
|
|
</ToolbarIconBtn>
|
|
)}
|
|
{onOpenFile && (
|
|
<ToolbarIconBtn onClick={() => onOpenFile(filePath, repo)} tooltip="Edit">
|
|
<IconPencil className="h-3.5 w-3.5" />
|
|
</ToolbarIconBtn>
|
|
)}
|
|
<FileActionsDropdown filePath={filePath} sessionId={sessionId} size="xs" />
|
|
{source === "uncommitted" && (
|
|
<ToolbarIconBtn
|
|
onClick={onDiscard}
|
|
tooltip="Revert changes"
|
|
className="h-6 w-6 p-0 cursor-pointer opacity-60 hover:opacity-100 hover:text-destructive"
|
|
>
|
|
<IconArrowBackUp className="h-3.5 w-3.5" />
|
|
</ToolbarIconBtn>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|