Files
opencode-manager/frontend/src/components/session/SessionCard.tsx
T
Chris Scott 2d14972fad fix(sessions): address PR #403 review findings
- fetch picker sessions only while the dialog is open
- list all-projects sessions with one request per page
- block infinite-scroll paging while a search is pending
- memoize picker rows with stable callbacks
- remove deleted sessions from list caches instead of refetching
- keep the repo tab when the open session is deleted
- reuse useSessionSearch in the sidebar session tree
- drop dead activeSessionID/isActive and share delete, session-key and repo-map helpers
- move usePersistentBoolean to its own hook
2026-10-08 14:36:26 -04:00

182 lines
6.9 KiB
TypeScript

import { useRef, useEffect } from "react";
import { Card } from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import { SessionStatusIndicator } from "@/components/ui/session-status-indicator";
import { Trash2, Clock, MoreVertical, Pin, PinOff } from "lucide-react";
import { formatDistanceToNow } from "date-fns";
import type { Session } from "@/api/types";
import { useSwipe } from "@/hooks/useSwipe";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
interface SessionCardProps {
session: Session;
isSelected: boolean;
manageMode: boolean;
isPinned?: boolean;
onTogglePin?: () => void;
onSelect: (sessionID: string) => void;
onToggleSelection: (selected: boolean) => void;
onDelete: (e: React.MouseEvent<HTMLButtonElement>) => void;
}
export const SessionCard = ({
session,
isSelected,
manageMode,
isPinned,
onTogglePin,
onSelect,
onToggleSelection,
onDelete,
}: SessionCardProps) => {
const cardRef = useRef<HTMLDivElement>(null);
const { bind, swipeOffset, isOpen, isSwipingBack, close, swipeStyles } = useSwipe();
useEffect(() => {
if (cardRef.current) {
return bind(cardRef.current);
}
}, [bind]);
const handleDeleteClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
onDelete(e);
close();
};
return (
<div className="relative" onClick={close}>
<div
className={`absolute top-0.5 right-0 bottom-0.5 w-20 bg-destructive flex items-center justify-center rounded-r-lg transition-opacity ${
!isSwipingBack && (isOpen || swipeOffset > 40) ? "opacity-100" : "opacity-0 pointer-events-none"
}`}
>
<button
aria-label="Delete session"
className="h-full w-full flex items-center justify-center text-destructive-foreground hover:bg-destructive/90"
onClick={handleDeleteClick}
>
<Trash2 className="w-5 h-5" />
</button>
</div>
<div ref={cardRef} style={swipeStyles}>
<Card
className={`p-2 cursor-pointer transition-all overflow-hidden ${
isOpen
? "rounded-none"
: "rounded-r-lg"
} ${
isSelected
? "border-primary shadow-lg shadow-primary/30 bg-accent"
: "bg-card border-border hover:bg-accent hover:border-border"
} hover:shadow-lg`}
onClick={() => {
if (!isOpen) {
if (manageMode) {
onToggleSelection(!isSelected);
} else {
onSelect(session.id);
}
}
}}
>
<div className="flex items-start justify-between gap-2">
{manageMode ? (
<div className="flex items-start gap-2 flex-1 min-w-0">
<div className="flex flex-col items-center gap-2 flex-shrink-0">
<Checkbox
checked={isSelected}
onCheckedChange={(checked) => {
onToggleSelection(checked === true);
}}
onClick={(e) => {
e.stopPropagation();
}}
className="w-5 h-5 flex-shrink-0"
/>
<SessionStatusIndicator sessionID={session.id} size="sm" />
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-1">
{isPinned && <Pin className="w-3 h-3 text-highlight shrink-0" />}
<h3 className="text-base font-semibold text-highlight truncate">
{session.title || "Untitled Session"}
</h3>
</div>
<div className="flex items-center gap-2 mt-0.5 text-xs text-muted-foreground">
<span className="flex items-center gap-1">
<Clock className="w-3 h-3" />
{formatDistanceToNow(new Date(session.time.updated), {
addSuffix: true,
})}
</span>
</div>
</div>
</div>
) : (
<>
<div className="flex flex-col flex-1 min-w-0">
<div className="flex items-center gap-1">
{isPinned && <Pin className="w-3 h-3 text-highlight shrink-0" />}
<h3 className="text-sm font-semibold text-highlight truncate">
{session.title || "Untitled Session"}
</h3>
</div>
<div className="flex items-center gap-2 mt-0.5 text-xs text-muted-foreground">
<span className="flex items-center">
<Clock className="w-3 h-3 mr-1" />
{formatDistanceToNow(new Date(session.time.updated), {
addSuffix: true,
})}
</span>
<SessionStatusIndicator sessionID={session.id} size="sm" />
</div>
</div>
{onTogglePin && (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
aria-label="Session actions"
size="sm"
variant="ghost"
className="h-7 w-7 p-0 shrink-0"
onClick={(e) => e.stopPropagation()}
>
<MoreVertical className="w-4 h-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className="z-[200]"
onClick={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
>
<DropdownMenuItem onClick={() => onTogglePin()}>
{isPinned ? <PinOff className="w-4 h-4 mr-2" /> : <Pin className="w-4 h-4 mr-2" />}
{isPinned ? "Unpin" : "Pin to top"}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)}
</>
)}
{manageMode && (
<button
aria-label="Delete session"
className="h-6 w-6 p-0 text-foreground hover:text-destructive bg-transparent border-none cursor-pointer"
onClick={(e) => {
e.stopPropagation();
onDelete(e);
}}
>
<Trash2 className="w-4 h-4" />
</button>
)}
</div>
</Card>
</div>
</div>
);
};