mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add swipe-to-delete functionality and manage mode for session cards (#126)
Introduces swipe gesture on mobile to reveal delete button, plus manage mode toggle enabling bulk selection and deletion capabilities
This commit is contained in:
1 parent
cb21e66339
commit
79b034d3ba
3 files changed
+219
-80
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { useState, useRef } from "react";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { MiniScanner } from "@/components/ui/mini-scanner";
|
||||
@@ -9,6 +10,7 @@ interface SessionCardProps {
|
||||
session: Session;
|
||||
isSelected: boolean;
|
||||
isActive: boolean;
|
||||
manageMode: boolean;
|
||||
onSelect: (sessionID: string) => void;
|
||||
onToggleSelection: (selected: boolean) => void;
|
||||
onDelete: (e: React.MouseEvent<HTMLButtonElement>) => void;
|
||||
@@ -18,60 +20,159 @@ export const SessionCard = ({
|
||||
session,
|
||||
isSelected,
|
||||
isActive,
|
||||
manageMode,
|
||||
onSelect,
|
||||
onToggleSelection,
|
||||
onDelete,
|
||||
}: SessionCardProps) => {
|
||||
const [swipeOffset, setSwipeOffset] = useState(0);
|
||||
const [isSwipeOpen, setIsSwipeOpen] = useState(false);
|
||||
const touchStartX = useRef<number | null>(null);
|
||||
const cardRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const handleTouchStart = (e: React.TouchEvent) => {
|
||||
touchStartX.current = e.touches[0].clientX;
|
||||
};
|
||||
|
||||
const handleTouchMove = (e: React.TouchEvent) => {
|
||||
if (touchStartX.current === null) return;
|
||||
|
||||
const currentX = e.touches[0].clientX;
|
||||
const diff = touchStartX.current - currentX;
|
||||
|
||||
if (diff > 0) {
|
||||
const newOffset = Math.min(diff, 80);
|
||||
setSwipeOffset(newOffset);
|
||||
} else if (diff < 0 && isSwipeOpen) {
|
||||
const newOffset = Math.max(0, 80 + diff);
|
||||
setSwipeOffset(newOffset);
|
||||
}
|
||||
};
|
||||
|
||||
const handleTouchEnd = () => {
|
||||
if (swipeOffset > 50) {
|
||||
setIsSwipeOpen(true);
|
||||
setSwipeOffset(80);
|
||||
} else if (swipeOffset < 30) {
|
||||
setIsSwipeOpen(false);
|
||||
setSwipeOffset(0);
|
||||
}
|
||||
touchStartX.current = null;
|
||||
};
|
||||
|
||||
const closeSwipe = () => {
|
||||
setSwipeOffset(0);
|
||||
setIsSwipeOpen(false);
|
||||
};
|
||||
|
||||
const handleDeleteClick = (e: React.MouseEvent<HTMLButtonElement>) => {
|
||||
e.stopPropagation();
|
||||
onDelete(e);
|
||||
closeSwipe();
|
||||
};
|
||||
|
||||
return (
|
||||
<Card
|
||||
className={`p-3 cursor-pointer transition-all ${
|
||||
isSelected
|
||||
? "border-blue-500 shadow-lg shadow-blue-900/30 dark:shadow-blue-900/30 bg-accent"
|
||||
: isActive
|
||||
? "bg-accent border-border"
|
||||
: "bg-card border-border hover:bg-accent hover:border-border"
|
||||
} hover:shadow-lg`}
|
||||
onClick={() => onSelect(session.id)}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
<div className="flex items-start gap-2 flex-1 min-w-0">
|
||||
<div className="flex flex-col items-center gap-3 flex-shrink-0">
|
||||
<Checkbox
|
||||
checked={isSelected}
|
||||
onCheckedChange={(checked) => {
|
||||
onToggleSelection(checked === true);
|
||||
}}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
}}
|
||||
className="w-5 h-5 flex-shrink-0"
|
||||
/>
|
||||
<MiniScanner sessionID={session.id} />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<h3 className="text-base font-semibold text-orange-600 dark:text-orange-400 truncate">
|
||||
{session.title || "Untitled Session"}
|
||||
</h3>
|
||||
</div>
|
||||
<div className="flex items-center gap-3 mt-1 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="relative" onClick={closeSwipe}>
|
||||
<div
|
||||
className={`absolute top-0.5 right-0 bottom-0.5 w-20 bg-red-600 flex items-center justify-center rounded-r-lg transition-opacity ${
|
||||
swipeOffset > 20 || isSwipeOpen ? "opacity-100" : "opacity-0"
|
||||
}`}
|
||||
>
|
||||
<button
|
||||
className="h-6 w-6 p-0 text-foreground hover:text-red-600 dark:hover:text-red-400 bg-transparent border-none cursor-pointer"
|
||||
onClick={onDelete}
|
||||
className="h-full w-full flex items-center justify-center text-white hover:bg-red-700"
|
||||
onClick={handleDeleteClick}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
<Trash2 className="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
</Card>
|
||||
<div
|
||||
ref={cardRef}
|
||||
onTouchStart={handleTouchStart}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handleTouchEnd}
|
||||
style={{ transform: `translateX(-${swipeOffset}px)` }}
|
||||
className="transition-transform"
|
||||
>
|
||||
<Card
|
||||
className={`p-2 cursor-pointer transition-all overflow-hidden ${
|
||||
isSwipeOpen
|
||||
? "rounded-none"
|
||||
: "rounded-r-lg"
|
||||
} ${
|
||||
isSelected
|
||||
? "border-blue-500 shadow-lg shadow-blue-900/30 dark:shadow-blue-900/30 bg-accent"
|
||||
: isActive
|
||||
? "bg-accent border-border"
|
||||
: "bg-card border-border hover:bg-accent hover:border-border"
|
||||
} hover:shadow-lg`}
|
||||
onClick={() => {
|
||||
if (!isSwipeOpen) {
|
||||
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"
|
||||
/>
|
||||
<MiniScanner sessionID={session.id} />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-1">
|
||||
<h3 className="text-base font-semibold text-orange-600 dark:text-orange-400 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">
|
||||
<h3 className="text-sm font-semibold text-orange-600 dark:text-orange-400 truncate">
|
||||
{session.title || "Untitled Session"}
|
||||
</h3>
|
||||
<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>
|
||||
<MiniScanner sessionID={session.id} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{manageMode && (
|
||||
<button
|
||||
className="h-6 w-6 p-0 text-foreground hover:text-red-600 dark:hover:text-red-400 bg-transparent border-none cursor-pointer"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onDelete(e);
|
||||
}}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -27,6 +27,7 @@ export const SessionList = ({
|
||||
const [selectedSessions, setSelectedSessions] = useState<Set<string>>(
|
||||
new Set(),
|
||||
);
|
||||
const [manageMode, setManageMode] = useState(false);
|
||||
|
||||
const filteredSessions = useMemo(() => {
|
||||
if (!sessions) return [];
|
||||
@@ -107,6 +108,17 @@ export const SessionList = ({
|
||||
setSelectedSessions(newSelected);
|
||||
};
|
||||
|
||||
const toggleManageMode = () => {
|
||||
setManageMode((prev) => {
|
||||
if (!prev) {
|
||||
return true;
|
||||
} else {
|
||||
setSelectedSessions(new Set());
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const toggleSelectAll = () => {
|
||||
if (!filteredSessions || filteredSessions.length === 0) return;
|
||||
|
||||
@@ -150,6 +162,8 @@ export const SessionList = ({
|
||||
onToggleSelectAll={toggleSelectAll}
|
||||
onDelete={handleBulkDelete}
|
||||
onDeleteAll={handleDeleteAll}
|
||||
manageMode={manageMode}
|
||||
onToggleManageMode={toggleManageMode}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -172,6 +186,7 @@ export const SessionList = ({
|
||||
session={session}
|
||||
isSelected={selectedSessions.has(session.id)}
|
||||
isActive={activeSessionID === session.id}
|
||||
manageMode={manageMode}
|
||||
onSelect={onSelectSession}
|
||||
onToggleSelection={(selected) => {
|
||||
toggleSessionSelection(session.id, selected);
|
||||
@@ -191,6 +206,7 @@ export const SessionList = ({
|
||||
session={session}
|
||||
isSelected={selectedSessions.has(session.id)}
|
||||
isActive={activeSessionID === session.id}
|
||||
manageMode={manageMode}
|
||||
onSelect={onSelectSession}
|
||||
onToggleSelection={(selected) => {
|
||||
toggleSessionSelection(session.id, selected);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Trash2, Search, GripVertical, Check } from "lucide-react";
|
||||
import { Trash2, Search, GripVertical, Check, Pencil, PencilOff } from "lucide-react";
|
||||
|
||||
interface ListToolbarProps {
|
||||
searchQuery: string;
|
||||
@@ -15,6 +15,8 @@ interface ListToolbarProps {
|
||||
reorderMode?: boolean;
|
||||
onToggleReorderMode?: () => void;
|
||||
showReorderToggle?: boolean;
|
||||
manageMode?: boolean;
|
||||
onToggleManageMode?: () => void;
|
||||
}
|
||||
|
||||
export function ListToolbar({
|
||||
@@ -30,6 +32,8 @@ export function ListToolbar({
|
||||
reorderMode = false,
|
||||
onToggleReorderMode,
|
||||
showReorderToggle = false,
|
||||
manageMode = false,
|
||||
onToggleManageMode,
|
||||
}: ListToolbarProps) {
|
||||
const hasItems = totalCount > 0;
|
||||
const hasSelection = selectedCount > 0;
|
||||
@@ -43,7 +47,7 @@ export function ListToolbar({
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="relative flex-1">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
|
||||
<Input
|
||||
@@ -54,21 +58,7 @@ export function ListToolbar({
|
||||
className="pl-10"
|
||||
/>
|
||||
</div>
|
||||
{showReorderToggle && onToggleReorderMode && (
|
||||
<Button
|
||||
onClick={onToggleReorderMode}
|
||||
variant={reorderMode ? "default" : "outline"}
|
||||
size="icon"
|
||||
className="md:hidden shrink-0"
|
||||
>
|
||||
{reorderMode ? (
|
||||
<Check className="w-4 h-4" />
|
||||
) : (
|
||||
<GripVertical className="w-4 h-4" />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{hasItems && (
|
||||
{manageMode && hasItems && (
|
||||
<Button
|
||||
onClick={onToggleSelectAll}
|
||||
variant={hasSelection ? "default" : "outline"}
|
||||
@@ -77,24 +67,56 @@ export function ListToolbar({
|
||||
{allSelected ? "Deselect All" : "Select All"}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
onClick={onDelete}
|
||||
variant="destructive"
|
||||
disabled={!hasSelection}
|
||||
className="hidden md:flex whitespace-nowrap"
|
||||
>
|
||||
<Trash2 className="w-4 h-4 mr-2" />
|
||||
Delete ({selectedCount})
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleMobileDelete}
|
||||
variant="destructive"
|
||||
size="icon"
|
||||
className="md:hidden"
|
||||
disabled={!hasItems}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
{manageMode && (
|
||||
<Button
|
||||
onClick={onDelete}
|
||||
variant="destructive"
|
||||
disabled={!hasSelection}
|
||||
className="hidden md:flex whitespace-nowrap"
|
||||
>
|
||||
<Trash2 className="w-4 h-4 mr-2" />
|
||||
Delete ({selectedCount})
|
||||
</Button>
|
||||
)}
|
||||
{manageMode && (
|
||||
<Button
|
||||
onClick={handleMobileDelete}
|
||||
variant="destructive"
|
||||
size="icon"
|
||||
className="md:hidden shrink-0 size-10"
|
||||
disabled={!hasItems}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
)}
|
||||
{showReorderToggle && onToggleReorderMode && (
|
||||
<Button
|
||||
onClick={onToggleReorderMode}
|
||||
variant={reorderMode ? "default" : "outline"}
|
||||
size="icon"
|
||||
className="md:hidden shrink-0 size-10"
|
||||
>
|
||||
{reorderMode ? (
|
||||
<Check className="w-4 h-4" />
|
||||
) : (
|
||||
<GripVertical className="w-4 h-4" />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{onToggleManageMode && hasItems && (
|
||||
<Button
|
||||
onClick={onToggleManageMode}
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="shrink-0 size-10"
|
||||
>
|
||||
{manageMode ? (
|
||||
<PencilOff className="w-4 h-4" />
|
||||
) : (
|
||||
<Pencil className="w-4 h-4" />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user