167 lines
7.0 KiB
TypeScript
167 lines
7.0 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { useSessions } from '../../hooks/useSleepSession';
|
|
import { useSettings } from '../../hooks/useSettings';
|
|
import { Pagination } from './Pagination';
|
|
import { Edit2, Check, X, Trash2 } from 'lucide-react';
|
|
import type { SleepSessionRead } from '../../types';
|
|
import { formatDateTime, toLocalDatetimeInputValue, getDurationMinutes, formatMinutes } from '../../utils/time';
|
|
|
|
export function SessionList() {
|
|
const { sessions, isLoading, updateSession, deleteSession, isDeleting } = useSessions();
|
|
const { settings } = useSettings();
|
|
const sessionsPerPage = settings.sessionsPerPage;
|
|
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
const [editingId, setEditingId] = useState<number | null>(null);
|
|
const [editStart, setEditStart] = useState<string>('');
|
|
const [editEnd, setEditEnd] = useState<string>('');
|
|
|
|
const totalPages = Math.ceil(sessions.length / sessionsPerPage);
|
|
const clampedPage = Math.max(1, Math.min(currentPage, totalPages || 1));
|
|
|
|
const paginatedSessions = sessions.slice(
|
|
(clampedPage - 1) * sessionsPerPage,
|
|
clampedPage * sessionsPerPage
|
|
);
|
|
|
|
if (isLoading) return <div className="text-center p-4">Loading sessions...</div>;
|
|
if (!sessions.length) return <div className="text-center p-4 text-gray-500">No sessions recorded.</div>;
|
|
|
|
const handleSave = async (id: number) => {
|
|
try {
|
|
await updateSession({
|
|
sessionId: id,
|
|
payload: {
|
|
start_time: new Date(editStart).toISOString(),
|
|
end_time: editEnd ? new Date(editEnd).toISOString() : undefined,
|
|
}
|
|
});
|
|
setEditingId(null);
|
|
} catch (err) {
|
|
console.error(err);
|
|
alert('Failed to update session');
|
|
}
|
|
};
|
|
|
|
const handleDelete = async (e: React.MouseEvent, id: number) => {
|
|
e.stopPropagation();
|
|
if (window.confirm('Are you sure you want to delete this sleep session?')) {
|
|
try {
|
|
await deleteSession(id);
|
|
} catch (err) {
|
|
console.error(err);
|
|
alert('Failed to delete session');
|
|
}
|
|
}
|
|
};
|
|
|
|
const startEditing = (s: SleepSessionRead) => {
|
|
setEditingId(s.id);
|
|
setEditStart(toLocalDatetimeInputValue(new Date(s.start_time)));
|
|
setEditEnd(s.end_time ? toLocalDatetimeInputValue(new Date(s.end_time)) : '');
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setEditingId(null);
|
|
};
|
|
|
|
return (
|
|
<div className="session-list bg-white p-4 rounded-xl shadow-sm border border-gray-200">
|
|
<h3 className="text-lg font-semibold mb-4 text-gray-800">Sleep Sessions</h3>
|
|
<div className="space-y-2">
|
|
{paginatedSessions.map((s) => (
|
|
<div key={s.id} className="border-b border-gray-100 last:border-0 py-3 sm:py-2">
|
|
{editingId === s.id ? (
|
|
<div className="flex flex-col space-y-3 py-1">
|
|
<div className="grid grid-cols-1 gap-3 text-xs text-gray-500">
|
|
<div className="flex flex-col gap-1">
|
|
<span className="font-medium text-gray-700">Start Time</span>
|
|
<input
|
|
type="datetime-local"
|
|
className="border rounded px-2 py-1.5 text-sm text-black w-full focus:ring-2 focus:ring-blue-500 outline-none"
|
|
value={editStart}
|
|
onChange={(e) => setEditStart(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<span className="font-medium text-gray-700">End Time</span>
|
|
<input
|
|
type="datetime-local"
|
|
className="border rounded px-2 py-1.5 text-sm text-black w-full focus:ring-2 focus:ring-blue-500 outline-none"
|
|
value={editEnd}
|
|
onChange={(e) => setEditEnd(e.target.value)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="flex justify-end space-x-4">
|
|
<button
|
|
onClick={handleCancel}
|
|
className="flex items-center gap-1 px-3 py-1.5 text-sm text-gray-500 hover:text-gray-700 bg-gray-100 rounded-lg transition-colors"
|
|
title="Cancel"
|
|
>
|
|
<X size={16} />
|
|
<span>Cancel</span>
|
|
</button>
|
|
<button
|
|
onClick={() => handleSave(s.id)}
|
|
className="flex items-center gap-1 px-3 py-1.5 text-sm text-white bg-green-600 hover:text-green-700 rounded-lg transition-colors"
|
|
title="Save"
|
|
>
|
|
<Check size={16} />
|
|
<span>Save</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div
|
|
className="flex flex-col sm:flex-row sm:justify-between sm:items-center cursor-pointer hover:bg-gray-50 transition-colors rounded-lg px-2"
|
|
onClick={() => startEditing(s)}
|
|
>
|
|
<div className="flex flex-col sm:flex-row sm:items-baseline sm:gap-3">
|
|
<div className="flex flex-col sm:flex-row sm:items-baseline sm:gap-2">
|
|
<span className="text-gray-900 font-bold text-base sm:text-sm">
|
|
{formatDateTime(s.start_time)}
|
|
</span>
|
|
<span className="text-blue-600 font-semibold text-xs sm:text-sm">
|
|
{s.end_time && s.start_time ? formatMinutes(getDurationMinutes(s.start_time, s.end_time)) : '—'}
|
|
</span>
|
|
</div>
|
|
<span className="text-gray-500 text-xs">
|
|
{s.end_time ? `${formatDateTime(s.start_time)} → ${formatDateTime(s.end_time)}` : 'In progress'}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-end space-x-2 sm:space-x-4 pt-3 sm:pt-0">
|
|
<div className="flex items-center gap-2 sm:hidden text-blue-600 font-semibold text-sm">
|
|
<span>{s.end_time && s.start_time ? formatMinutes(getDurationMinutes(s.start_time, s.end_time)) : '—'}</span>
|
|
</div>
|
|
<div className="flex items-center gap-1 sm:gap-2">
|
|
<button
|
|
onClick={(e) => handleDelete(e, s.id)}
|
|
disabled={isDeleting}
|
|
className="p-2 text-red-400 hover:text-red-600 disabled:opacity-50 transition-colors"
|
|
title="Delete session"
|
|
>
|
|
<Trash2 size={18} />
|
|
</button>
|
|
<div className="p-2 text-gray-400">
|
|
<Edit2 size={18} />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
{sessions.length > sessionsPerPage && (
|
|
<Pagination
|
|
currentPage={clampedPage}
|
|
totalPages={totalPages}
|
|
onPageChange={setCurrentPage}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|