Skip to content

feat: add button to delete conversations #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions src/components/ConversationList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Clock, MessageSquare, Lock, Loader2, Signal } from 'lucide-react';
import { Clock, MessageSquare, Lock, Loader2, Signal, Trash } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { getRelativeTimeString } from '@/utils/time';
import { useApi } from '@/contexts/ApiContext';
import { demoConversations } from '@/democonversations';

import type { MessageRole } from '@/types/conversation';
import type { FC } from 'react';
import { useState, type FC } from 'react';
import { Computed, use$ } from '@legendapp/state/react';
import { type Observable } from '@legendapp/state';
import { conversations$ } from '@/stores/conversations';
import { DeleteConversationConfirmationDialog } from './DeleteConversationConfrimationDialog';

type MessageBreakdown = Partial<Record<MessageRole, number>>;

Expand Down Expand Up @@ -42,6 +43,8 @@ export const ConversationList: FC<Props> = ({
}) => {
const { isConnected$ } = useApi();
const isConnected = use$(isConnected$);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [conversationToDelete, setConversationToDelete] = useState<string | null>(null);

if (!conversations) {
return null;
Expand Down Expand Up @@ -198,6 +201,20 @@ export const ConversationList: FC<Props> = ({
<TooltipContent>This conversation is read-only</TooltipContent>
</Tooltip>
)}
<Tooltip>
<TooltipTrigger>
<span
className="flex items-center"
onClick={() => {
setConversationToDelete(conv.name);
setDeleteDialogOpen(true);
}}
>
<Trash className="h-4 w-4" />
</span>
</TooltipTrigger>
<TooltipContent>Delete conversation</TooltipContent>
</Tooltip>
</div>
</div>
</div>
Expand All @@ -209,6 +226,14 @@ export const ConversationList: FC<Props> = ({

return (
<div data-testid="conversation-list" className="h-full space-y-2 overflow-y-auto p-4">
<DeleteConversationConfirmationDialog
conversationName={conversationToDelete ?? ''}
open={deleteDialogOpen}
onOpenChange={setDeleteDialogOpen}
onDelete={() => {
setConversationToDelete(null);
}}
/>
{isLoading && (
<div className="flex items-center justify-center p-4 text-sm text-muted-foreground">
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Expand Down
93 changes: 93 additions & 0 deletions src/components/DeleteConversationConfrimationDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { useState } from 'react';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Loader2 } from 'lucide-react';
import { useApi } from '@/contexts/ApiContext';
import { conversations$, selectedConversation$ } from '@/stores/conversations';
import { useQueryClient } from '@tanstack/react-query';
import { use$ } from '@legendapp/state/react';
import { demoConversations } from '@/democonversations';

interface Props {
conversationName: string;
open: boolean;
onOpenChange: (open: boolean) => void;
onDelete: () => void;
}

export function DeleteConversationConfirmationDialog({
conversationName,
open,
onOpenChange,
onDelete,
}: Props) {
const { deleteConversation, connectionConfig, isConnected$ } = useApi();
const queryClient = useQueryClient();
const isConnected = use$(isConnected$);
const [isDeleting, setIsDeleting] = useState(false);
const [isError, setIsError] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);

const handleDelete = async () => {
// Show loading indicator
setIsDeleting(true);

// Delete conversation
try {
await deleteConversation(conversationName);
} catch (error) {
setIsError(true);
if (error instanceof Error) {
setErrorMessage(error.message);
} else {
setErrorMessage('An unknown error occurred');
}
setIsDeleting(false);
}
conversations$.delete(conversationName);
queryClient.invalidateQueries({
queryKey: ['conversations', connectionConfig.baseUrl, isConnected],
});
selectedConversation$.set(demoConversations[0].name);

// Reset state
await onDelete();
setIsDeleting(false);
onOpenChange(false);
};

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle>Delete Conversation</DialogTitle>
<DialogDescription>
Are you sure you want to delete the conversation <strong>{conversationName}</strong>?
This action cannot be undone.
</DialogDescription>
</DialogHeader>
{isError && (
<div className="mb-4 rounded-md bg-destructive/10 p-4 text-sm text-destructive">
<p>{errorMessage}</p>
</div>
)}
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button variant="destructive" onClick={handleDelete} disabled={isDeleting}>
{isDeleting && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
2 changes: 2 additions & 0 deletions src/contexts/ApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface ApiContextType {
closeEventStream: ApiClient['closeEventStream'];
getChatConfig: ApiClient['getChatConfig'];
updateChatConfig: ApiClient['updateChatConfig'];
deleteConversation: ApiClient['deleteConversation'];
}

const ApiContext = createContext<ApiContextType | null>(null);
Expand Down Expand Up @@ -217,6 +218,7 @@ export function ApiProvider({
closeEventStream: api.closeEventStream.bind(api),
getChatConfig: api.getChatConfig.bind(api),
updateChatConfig: api.updateChatConfig.bind(api),
deleteConversation: api.deleteConversation.bind(api),
}}
>
{children}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ export class ApiClient {
}
);
}

async deleteConversation(logfile: string): Promise<void> {
if (!this.isConnected) {
throw new ApiClientError('Not connected to API');
}

await this.fetchJson<{ status: string }>(`${this.baseUrl}/api/v2/conversations/${logfile}`, {
method: 'DELETE',
signal: this.controller?.signal,
});
}
}

export const createApiClient = (baseUrl?: string, authHeader?: string | null): ApiClient => {
Expand Down
Loading