feat: improve messages ux (#193)
This commit is contained in:
@@ -181,12 +181,12 @@ export const ChatMessage = memo(function ChatMessage({ comment, label, className
|
||||
return <InlineCode>{content}</InlineCode>;
|
||||
},
|
||||
ol: ({ children }) => (
|
||||
<ol className="list-decimal pl-6 my-2">
|
||||
<ol className="list-decimal pl-6 mb-2">
|
||||
{children}
|
||||
</ol>
|
||||
),
|
||||
ul: ({ children }) => (
|
||||
<ul className="list-disc pl-6 my-1">
|
||||
<ul className="list-disc pl-6 mb-2">
|
||||
{children}
|
||||
</ul>
|
||||
),
|
||||
@@ -196,7 +196,7 @@ export const ChatMessage = memo(function ChatMessage({ comment, label, className
|
||||
</li>
|
||||
),
|
||||
p: ({ children }) => (
|
||||
<p className="leading-relaxed my-0.5">
|
||||
<p className="leading-relaxed mb-1.5">
|
||||
{children}
|
||||
</p>
|
||||
),
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
'use client';
|
||||
|
||||
import { IconCode } from '@tabler/icons-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { DiffViewInline } from '@/components/diff';
|
||||
import type { FileDiffData } from '@/lib/diff/types';
|
||||
@@ -8,28 +7,13 @@ import type { FileDiffData } from '@/lib/diff/types';
|
||||
type DiffViewBlockProps = {
|
||||
/** Diff data in the new format */
|
||||
data: FileDiffData;
|
||||
/** Title to display */
|
||||
title?: string;
|
||||
/** Whether to show the title */
|
||||
showTitle?: boolean;
|
||||
/** Additional class name */
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export function DiffViewBlock({
|
||||
data,
|
||||
title = 'Diff',
|
||||
showTitle = true,
|
||||
className
|
||||
}: DiffViewBlockProps) {
|
||||
export function DiffViewBlock({ data, className }: DiffViewBlockProps) {
|
||||
return (
|
||||
<div className={cn("mt-3 rounded-md border border-border/50 bg-background/60 px-3 py-2 text-xs", className)}>
|
||||
{showTitle && (
|
||||
<div className="flex items-center gap-2 text-muted-foreground mb-1 uppercase tracking-wide">
|
||||
<IconCode className="h-3.5 w-3.5" />
|
||||
<span>{title}</span>
|
||||
</div>
|
||||
)}
|
||||
<DiffViewInline data={data} />
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -7,7 +7,7 @@ type FilePathButtonProps = {
|
||||
filePath: string;
|
||||
worktreePath?: string;
|
||||
onOpenFile?: (path: string) => void;
|
||||
variant?: 'badge' | 'list-item';
|
||||
variant?: 'badge' | 'list-item' | 'link';
|
||||
className?: string;
|
||||
};
|
||||
|
||||
@@ -40,10 +40,12 @@ export const FilePathButton = memo(function FilePathButton({ filePath, worktreeP
|
||||
onOpenFile?.(filePath);
|
||||
}, [onOpenFile, filePath]);
|
||||
|
||||
const baseStyles = 'font-mono truncate cursor-pointer text-indigo-400 hover:underline transition-colors block w-full text-left';
|
||||
const variantStyles = variant === 'badge'
|
||||
? 'text-xs px-1.5 py-0.5 rounded bg-indigo-500/20 hover:bg-indigo-500/30'
|
||||
: 'flex items-center px-2 py-1 rounded hover:bg-indigo-500/20';
|
||||
const baseStyles = 'font-mono truncate cursor-pointer transition-colors block w-full text-left';
|
||||
const variantStyles = {
|
||||
badge: 'text-xs px-1.5 py-0.5 rounded bg-indigo-500/20 hover:bg-indigo-500/30 text-indigo-400 hover:underline',
|
||||
'list-item': 'flex items-center px-2 py-1 rounded hover:bg-indigo-500/20 text-indigo-400 hover:underline',
|
||||
link: 'text-xs text-muted-foreground hover:text-foreground underline decoration-muted-foreground/40 hover:decoration-foreground/60',
|
||||
}[variant];
|
||||
|
||||
if (onOpenFile && canOpen) {
|
||||
return (
|
||||
@@ -58,9 +60,11 @@ export const FilePathButton = memo(function FilePathButton({ filePath, worktreeP
|
||||
);
|
||||
}
|
||||
|
||||
const inactiveStyles = variant === 'badge'
|
||||
? 'text-xs text-muted-foreground/60 truncate font-mono bg-muted/30 px-1.5 py-0.5 rounded block w-full'
|
||||
: 'px-2 py-1 font-mono text-muted-foreground truncate block w-full';
|
||||
const inactiveStyles = {
|
||||
badge: 'text-xs text-muted-foreground/60 truncate font-mono bg-muted/30 px-1.5 py-0.5 rounded block w-full',
|
||||
'list-item': 'px-2 py-1 font-mono text-muted-foreground truncate block w-full',
|
||||
link: 'text-xs font-mono text-muted-foreground/60 truncate block w-full',
|
||||
}[variant];
|
||||
|
||||
return (
|
||||
<span className={cn(inactiveStyles, className)} title={filePath}>
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useRef, memo, useCallback } from 'react';
|
||||
import { IconCheck, IconX, IconEdit, IconFilePlus } from '@tabler/icons-react';
|
||||
import { IconCheck, IconX, IconEdit, IconFilePlus, IconExternalLink, IconCopy } from '@tabler/icons-react';
|
||||
import { GridSpinner } from '@/components/grid-spinner';
|
||||
import { transformPathsInText } from '@/lib/utils';
|
||||
import { FilePathButton } from './file-path-button';
|
||||
import type { Message } from '@/lib/types/http';
|
||||
import { DiffViewBlock } from './diff-view-block';
|
||||
import { ExpandableRow } from './expandable-row';
|
||||
@@ -35,6 +34,7 @@ type ToolEditMessageProps = {
|
||||
export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktreePath, onOpenFile }: ToolEditMessageProps) {
|
||||
const metadata = comment.metadata as ToolEditMetadata | undefined;
|
||||
const [manualExpandState, setManualExpandState] = useState<boolean | null>(null);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const prevStatusRef = useRef(metadata?.status);
|
||||
|
||||
const status = metadata?.status;
|
||||
@@ -69,6 +69,15 @@ export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktree
|
||||
setManualExpandState((prev) => !(prev ?? autoExpanded));
|
||||
}, [autoExpanded]);
|
||||
|
||||
const handleCopyPath = useCallback((e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
if (filePath) {
|
||||
navigator.clipboard?.writeText(filePath);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 1500);
|
||||
}
|
||||
}, [filePath]);
|
||||
|
||||
const getStatusIcon = () => {
|
||||
switch (status) {
|
||||
case 'complete':
|
||||
@@ -84,11 +93,16 @@ export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktree
|
||||
|
||||
// Count lines for Write content
|
||||
const lineCount = writeContent ? writeContent.split('\n').length : 0;
|
||||
|
||||
// Check if file is inside worktree (only these can be opened)
|
||||
const isFileInWorktree = filePath && worktreePath && filePath.startsWith(worktreePath);
|
||||
|
||||
const getSummary = () => {
|
||||
const baseSummary = transformPathsInText(comment.content, worktreePath);
|
||||
if (isWriteOperation && lineCount > 0) {
|
||||
return `Write ${lineCount} line${lineCount !== 1 ? 's' : ''}`;
|
||||
return `${baseSummary} (${lineCount} line${lineCount !== 1 ? 's' : ''})`;
|
||||
}
|
||||
return transformPathsInText(comment.content, worktreePath);
|
||||
return baseSummary;
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -100,14 +114,32 @@ export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktree
|
||||
<span className="font-mono text-xs text-muted-foreground">{getSummary()}</span>
|
||||
{!isSuccess && getStatusIcon()}
|
||||
</span>
|
||||
{filePath && (
|
||||
<span className="min-w-0">
|
||||
<FilePathButton
|
||||
filePath={filePath}
|
||||
worktreePath={worktreePath}
|
||||
onOpenFile={onOpenFile}
|
||||
/>
|
||||
</span>
|
||||
{filePath && onOpenFile && isFileInWorktree && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onOpenFile(filePath);
|
||||
}}
|
||||
className="opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-foreground shrink-0 cursor-pointer"
|
||||
title="Open file"
|
||||
>
|
||||
<IconExternalLink className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
{filePath && !isFileInWorktree && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCopyPath}
|
||||
className="opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-foreground shrink-0 cursor-pointer"
|
||||
title={copied ? 'Copied!' : 'Copy path'}
|
||||
>
|
||||
{copied ? (
|
||||
<IconCheck className="h-3.5 w-3.5 text-green-500" />
|
||||
) : (
|
||||
<IconCopy className="h-3.5 w-3.5" />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
}
|
||||
@@ -116,7 +148,7 @@ export const ToolEditMessage = memo(function ToolEditMessage({ comment, worktree
|
||||
onToggle={handleToggle}
|
||||
>
|
||||
{diffData?.diff ? (
|
||||
<DiffViewBlock data={diffData} showTitle={false} className="mt-0 border-0 px-0" />
|
||||
<DiffViewBlock data={diffData} className="mt-0 border-0 px-0" />
|
||||
) : writeContent ? (
|
||||
<div className="pl-4 border-l-2 border-border/30">
|
||||
<pre className="text-xs bg-muted/30 rounded p-2 overflow-x-auto max-h-[300px] overflow-y-auto whitespace-pre-wrap">
|
||||
|
||||
@@ -10,6 +10,7 @@ import { Button } from '@kandev/ui/button';
|
||||
import { IconDeviceFloppy, IconLoader2, IconTrash, IconTextWrap, IconTextWrapDisabled, IconMessagePlus } from '@tabler/icons-react';
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@kandev/ui/tooltip';
|
||||
import { formatDiffStats } from '@/lib/utils/file-diff';
|
||||
import { toRelativePath } from '@/lib/utils';
|
||||
import { vscodeDark } from '@uiw/codemirror-theme-vscode';
|
||||
import { useDiffCommentsStore, useFileComments } from '@/lib/state/slices/diff-comments/diff-comments-slice';
|
||||
import type { DiffComment } from '@/lib/diff/types';
|
||||
@@ -24,6 +25,7 @@ type FileEditorContentProps = {
|
||||
isDirty: boolean;
|
||||
isSaving: boolean;
|
||||
sessionId?: string;
|
||||
worktreePath?: string;
|
||||
enableComments?: boolean;
|
||||
onChange: (newContent: string) => void;
|
||||
onSave: () => void;
|
||||
@@ -129,6 +131,7 @@ export function FileEditorContent({
|
||||
isDirty,
|
||||
isSaving,
|
||||
sessionId,
|
||||
worktreePath,
|
||||
enableComments = false,
|
||||
onChange,
|
||||
onSave,
|
||||
@@ -516,7 +519,7 @@ export function FileEditorContent({
|
||||
{/* Editor header with save button */}
|
||||
<div className="flex items-center justify-between px-2 border-foreground/10 border-b">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<span className="font-mono">{path}</span>
|
||||
<span className="font-mono">{toRelativePath(path, worktreePath)}</span>
|
||||
{isDirty && diffStats && (
|
||||
<span className="text-xs text-yellow-500">
|
||||
{formatDiffStats(diffStats.additions, diffStats.deletions)}
|
||||
|
||||
@@ -644,6 +644,7 @@ export const TaskCenterPanel = memo(function TaskCenterPanel({
|
||||
isDirty={tab.isDirty}
|
||||
isSaving={savingFiles.has(tab.path)}
|
||||
sessionId={activeSessionId || undefined}
|
||||
worktreePath={activeSession?.worktree_path ?? undefined}
|
||||
enableComments={!!activeSessionId}
|
||||
onChange={(newContent) => handleFileChange(tab.path, newContent)}
|
||||
onSave={() => handleFileSave(tab.path)}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { useEffect, useRef, useCallback } from 'react';
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
import { useAppStore, useAppStoreApi } from '@/components/state-provider';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { GitSnapshot } from '@/lib/state/slices/session-runtime/types';
|
||||
|
||||
@@ -10,12 +11,73 @@ import type { GitSnapshot } from '@/lib/state/slices/session-runtime/types';
|
||||
* For historical snapshots, use useSessionGitSnapshots hook.
|
||||
*/
|
||||
export function useSessionGitStatus(sessionId: string | null) {
|
||||
const gitStatus = useAppStore((state) =>
|
||||
sessionId ? state.gitStatus.bySessionId[sessionId] : undefined
|
||||
// Use shallow comparison to prevent re-renders when object reference changes but values are the same
|
||||
const gitStatus = useAppStore(
|
||||
useShallow((state) =>
|
||||
sessionId ? state.gitStatus.bySessionId[sessionId] : undefined
|
||||
)
|
||||
);
|
||||
const setGitStatus = useAppStore((state) => state.setGitStatus);
|
||||
const connectionStatus = useAppStore((state) => state.connection.status);
|
||||
const storeApi = useAppStoreApi();
|
||||
const prevSessionIdRef = useRef<string | null>(null);
|
||||
const hasFetchedRef = useRef(false);
|
||||
|
||||
// Stable reference to fetch function
|
||||
const fetchSnapshot = useCallback(async (sid: string) => {
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
|
||||
const setGitStatus = storeApi.getState().setGitStatus;
|
||||
|
||||
try {
|
||||
const response = await client.request<{ snapshots?: GitSnapshot[] }>(
|
||||
'session.git.snapshots',
|
||||
{
|
||||
session_id: sid,
|
||||
limit: 1, // Only fetch the latest snapshot
|
||||
}
|
||||
);
|
||||
|
||||
if (response?.snapshots && response.snapshots.length > 0) {
|
||||
const latest = response.snapshots[0];
|
||||
|
||||
// Extract file paths by status from the files object
|
||||
const modified: string[] = [];
|
||||
const added: string[] = [];
|
||||
const deleted: string[] = [];
|
||||
const untracked: string[] = [];
|
||||
const renamed: string[] = [];
|
||||
|
||||
if (latest.files) {
|
||||
Object.entries(latest.files).forEach(([path, fileInfo]) => {
|
||||
const status = fileInfo.status?.toLowerCase();
|
||||
if (status === 'modified') modified.push(path);
|
||||
else if (status === 'added') added.push(path);
|
||||
else if (status === 'deleted') deleted.push(path);
|
||||
else if (status === 'untracked') untracked.push(path);
|
||||
else if (status === 'renamed') renamed.push(path);
|
||||
});
|
||||
}
|
||||
|
||||
// Populate git status from latest snapshot
|
||||
setGitStatus(sid, {
|
||||
branch: latest.branch,
|
||||
remote_branch: latest.remote_branch,
|
||||
modified,
|
||||
added,
|
||||
deleted,
|
||||
untracked,
|
||||
renamed,
|
||||
ahead: latest.ahead,
|
||||
behind: latest.behind,
|
||||
files: latest.files,
|
||||
timestamp: latest.created_at,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch latest git snapshot:', error);
|
||||
}
|
||||
}, [storeApi]);
|
||||
|
||||
// Fetch initial status on mount if not already loaded
|
||||
// Also refetch when switching to a different session or when WebSocket connects
|
||||
@@ -28,70 +90,21 @@ export function useSessionGitStatus(sessionId: string | null) {
|
||||
// Detect session change to force refetch
|
||||
const sessionChanged = prevSessionIdRef.current !== null &&
|
||||
prevSessionIdRef.current !== sessionId;
|
||||
|
||||
if (sessionChanged) {
|
||||
hasFetchedRef.current = false;
|
||||
}
|
||||
prevSessionIdRef.current = sessionId;
|
||||
|
||||
// Skip fetch if status exists and session hasn't changed
|
||||
if (gitStatus && !sessionChanged) {
|
||||
// Skip fetch if we already have status or already fetched for this session
|
||||
const currentStatus = storeApi.getState().gitStatus.bySessionId[sessionId];
|
||||
if (currentStatus || hasFetchedRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch latest git snapshot
|
||||
const fetchSnapshot = async () => {
|
||||
const client = getWebSocketClient();
|
||||
if (!client) return;
|
||||
|
||||
try {
|
||||
const response = await client.request<{ snapshots?: GitSnapshot[] }>(
|
||||
'session.git.snapshots',
|
||||
{
|
||||
session_id: sessionId,
|
||||
limit: 1, // Only fetch the latest snapshot
|
||||
}
|
||||
);
|
||||
|
||||
if (response?.snapshots && response.snapshots.length > 0) {
|
||||
const latest = response.snapshots[0];
|
||||
|
||||
// Extract file paths by status from the files object
|
||||
const modified: string[] = [];
|
||||
const added: string[] = [];
|
||||
const deleted: string[] = [];
|
||||
const untracked: string[] = [];
|
||||
const renamed: string[] = [];
|
||||
|
||||
if (latest.files) {
|
||||
Object.entries(latest.files).forEach(([path, fileInfo]) => {
|
||||
const status = fileInfo.status?.toLowerCase();
|
||||
if (status === 'modified') modified.push(path);
|
||||
else if (status === 'added') added.push(path);
|
||||
else if (status === 'deleted') deleted.push(path);
|
||||
else if (status === 'untracked') untracked.push(path);
|
||||
else if (status === 'renamed') renamed.push(path);
|
||||
});
|
||||
}
|
||||
|
||||
// Populate git status from latest snapshot
|
||||
setGitStatus(sessionId, {
|
||||
branch: latest.branch,
|
||||
remote_branch: latest.remote_branch,
|
||||
modified,
|
||||
added,
|
||||
deleted,
|
||||
untracked,
|
||||
renamed,
|
||||
ahead: latest.ahead,
|
||||
behind: latest.behind,
|
||||
files: latest.files,
|
||||
timestamp: latest.created_at,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch latest git snapshot:', error);
|
||||
}
|
||||
};
|
||||
|
||||
fetchSnapshot();
|
||||
}, [sessionId, gitStatus, setGitStatus, connectionStatus]);
|
||||
hasFetchedRef.current = true;
|
||||
fetchSnapshot(sessionId);
|
||||
}, [sessionId, connectionStatus, fetchSnapshot, storeApi]);
|
||||
|
||||
// Subscribe to session updates to receive git status via WebSocket
|
||||
useEffect(() => {
|
||||
|
||||
@@ -84,8 +84,43 @@ export const createSessionRuntimeSlice: StateCreator<
|
||||
}),
|
||||
setGitStatus: (sessionId, gitStatus) =>
|
||||
set((draft) => {
|
||||
// Always update git status - the timestamp and file diffs may have changed
|
||||
// even if the file lists are the same. The backend sends updates when content changes.
|
||||
const existing = draft.gitStatus.bySessionId[sessionId];
|
||||
|
||||
// Skip update if key fields haven't changed to prevent unnecessary re-renders
|
||||
if (existing) {
|
||||
const branchChanged = existing.branch !== gitStatus.branch ||
|
||||
existing.remote_branch !== gitStatus.remote_branch;
|
||||
const syncStatusChanged = existing.ahead !== gitStatus.ahead ||
|
||||
existing.behind !== gitStatus.behind;
|
||||
|
||||
// Compare file list (keys) and total stats, not individual file contents
|
||||
const existingFileKeys = existing.files ? Object.keys(existing.files).sort().join(',') : '';
|
||||
const newFileKeys = gitStatus.files ? Object.keys(gitStatus.files).sort().join(',') : '';
|
||||
const fileListChanged = existingFileKeys !== newFileKeys;
|
||||
|
||||
// Compare total additions/deletions across all files
|
||||
const existingTotal = { additions: 0, deletions: 0 };
|
||||
const newTotal = { additions: 0, deletions: 0 };
|
||||
if (existing.files) {
|
||||
for (const f of Object.values(existing.files)) {
|
||||
existingTotal.additions += f.additions || 0;
|
||||
existingTotal.deletions += f.deletions || 0;
|
||||
}
|
||||
}
|
||||
if (gitStatus.files) {
|
||||
for (const f of Object.values(gitStatus.files)) {
|
||||
newTotal.additions += f.additions || 0;
|
||||
newTotal.deletions += f.deletions || 0;
|
||||
}
|
||||
}
|
||||
const statsChanged = existingTotal.additions !== newTotal.additions ||
|
||||
existingTotal.deletions !== newTotal.deletions;
|
||||
|
||||
if (!branchChanged && !syncStatusChanged && !fileListChanged && !statsChanged) {
|
||||
return; // No meaningful change, skip update
|
||||
}
|
||||
}
|
||||
|
||||
draft.gitStatus.bySessionId[sessionId] = gitStatus;
|
||||
}),
|
||||
clearGitStatus: (sessionId) =>
|
||||
|
||||
Reference in New Issue
Block a user