Fix time duration and visuals

This commit is contained in:
2026-04-24 22:43:45 +02:00
parent ee0bb780c1
commit 192432f8e5
14 changed files with 281 additions and 111 deletions
+66 -36
View File
@@ -31,7 +31,7 @@
"tailwindcss": "^4.0.0",
"typescript": "^5.7.0",
"vite": "^6.0.0",
"vitest": "^2.1.0"
"vitest": "2.1.9"
}
},
"node_modules/@adobe/css-tools": {
@@ -1702,9 +1702,9 @@
}
},
"node_modules/@tanstack/query-core": {
"version": "5.99.2",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.99.2.tgz",
"integrity": "sha512-1HunU0bXVsR1ZJMZbcOPE6VtaBJxsW809RE9xPe4Gz7MlB0GWwQvuTPhMoEmQ/hIzFKJ/DWAuttIe7BOaWx0tA==",
"version": "5.100.1",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.100.1.tgz",
"integrity": "sha512-awvQhOO/2TrSCHE5LKKsXcvvj6WSBncwEcMFCB/ez0Qs0b17iyyivoGArNV3HFfXryZwCpnb/olsaBBKrIbtSw==",
"license": "MIT",
"funding": {
"type": "github",
@@ -1712,12 +1712,12 @@
}
},
"node_modules/@tanstack/react-query": {
"version": "5.99.2",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.99.2.tgz",
"integrity": "sha512-vM91UEe45QUS9ED6OklsVL15i8qKcRqNwpWzPTVWvRPRSEgDudDgHpvyTjcdlwHcrKNa80T+xXYcchT2noPnZA==",
"version": "5.100.1",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.100.1.tgz",
"integrity": "sha512-UgWRLhQKprC37SsO6y1zRabOqDmM2gsdTNPbqTT35yl7kOOhwXU4nyfOiGHXPwoEFJV1IpSk85hjIFjNFWVpzw==",
"license": "MIT",
"dependencies": {
"@tanstack/query-core": "5.99.2"
"@tanstack/query-core": "5.100.1"
},
"funding": {
"type": "github",
@@ -2180,9 +2180,9 @@
}
},
"node_modules/baseline-browser-mapping": {
"version": "2.10.20",
"resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.10.20.tgz",
"integrity": "sha512-1AaXxEPfXT+GvTBJFuy4yXVHWJBXa4OdbIebGN/wX5DlsIkU0+wzGnd2lOzokSk51d5LUmqjgBLRLlypLUqInQ==",
"version": "2.10.21",
"resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.10.21.tgz",
"integrity": "sha512-Q+rUQ7Uz8AHM7DEaNdwvfFCTq7a43lNTzuS94eiWqwyxfV/wJv+oUivef51T91mmRY4d4A1u9rcSvkeufCVXlA==",
"dev": true,
"license": "Apache-2.0",
"bin": {
@@ -2565,21 +2565,21 @@
}
},
"node_modules/electron-to-chromium": {
"version": "1.5.343",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.343.tgz",
"integrity": "sha512-YHnQ3MXI08icvL9ZKnEBy05F2EQ8ob01UaMOuMbM8l+4UcAq6MPPbBTJBbsBUg3H8JeZNt+O4fjsoWth3p6IFg==",
"version": "1.5.344",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.344.tgz",
"integrity": "sha512-4MxfbmNDm+KPh066EZy+eUnkcDPcZ35wNmOWzFuh/ijvHsve6kbLTLURy88uCNK5FbpN+yk2nQY6BYh1GEt+wg==",
"dev": true,
"license": "ISC"
},
"node_modules/enhanced-resolve": {
"version": "5.20.1",
"resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.20.1.tgz",
"integrity": "sha512-Qohcme7V1inbAfvjItgw0EaxVX5q2rdVEZHRBrEQdRZTssLDGsL8Lwrznl8oQ/6kuTJONLaDcGjkNP247XEhcA==",
"version": "5.21.0",
"resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.21.0.tgz",
"integrity": "sha512-otxSQPw4lkOZWkHpB3zaEQs6gWYEsmX4xQF68ElXC/TWvGxGMSGOvoNbaLXm6/cS/fSfHtsEdw90y20PCd+sCA==",
"dev": true,
"license": "MIT",
"dependencies": {
"graceful-fs": "^4.2.4",
"tapable": "^2.3.0"
"tapable": "^2.3.3"
},
"engines": {
"node": ">=10.13.0"
@@ -2739,9 +2739,9 @@
}
},
"node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
@@ -3378,21 +3378,6 @@
"node": ">=18"
}
},
"node_modules/playwright/node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/postcss": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.10.tgz",
@@ -4539,6 +4524,21 @@
"@esbuild/win32-x64": "0.21.5"
}
},
"node_modules/vite-node/node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/vite-node/node_modules/vite": {
"version": "5.4.21",
"resolved": "https://registry.npmjs.org/vite/-/vite-5.4.21.tgz",
@@ -4599,6 +4599,21 @@
}
}
},
"node_modules/vite/node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/vitest": {
"version": "2.1.9",
"resolved": "https://registry.npmjs.org/vitest/-/vitest-2.1.9.tgz",
@@ -5123,6 +5138,21 @@
"@esbuild/win32-x64": "0.21.5"
}
},
"node_modules/vitest/node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/vitest/node_modules/vite": {
"version": "5.4.21",
"resolved": "https://registry.npmjs.org/vite/-/vite-5.4.21.tgz",
+2 -1
View File
@@ -8,6 +8,7 @@
"build": "tsc && vite build",
"preview": "vite preview",
"test": "vitest",
"test:all": "vitest run && npx playwright test",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
},
"dependencies": {
@@ -34,6 +35,6 @@
"tailwindcss": "^4.0.0",
"typescript": "^5.7.0",
"vite": "^6.0.0",
"vitest": "^2.1.0"
"vitest": "2.1.9"
}
}
@@ -1,5 +1,6 @@
import React from 'react';
import { useStats } from '../../hooks/useStats';
import { timeToMinutes } from '../../utils/time';
interface TimetableProps {
from_date: string;
@@ -30,18 +31,26 @@ const Timetable: React.FC<TimetableProps> = ({ from_date, to_date, day_start, da
{Object.keys(groupedSlots).map((date) => (
<div key={date} className="mb-6 last:mb-0">
<h4 className="text-sm font-medium text-gray-500 mb-2">{date}</h4>
<div className="grid grid-cols-[repeat(auto-fill,minmax(20px,1fr))] gap-1">
{groupedSlots[date].map((slot: any) => (
<div
key={`${date}-${slot.slot_start}`}
className={`h-6 w-full rounded-sm ${
slot.sleeping ? 'bg-blue-900' : 'bg-gray-100'
}`}
title={`${slot.slot_start} - ${slot.slot_end}`}
>
<span className="text-[10px] text-white/50">{slot.slot_start}</span>
</div>
))}
<div className="flex w-full h-8 gap-1 bg-gray-50 p-1 rounded-lg overflow-hidden">
{groupedSlots[date].map((slot: any) => {
const duration = Math.max(1, timeToMinutes(slot.slot_end) - timeToMinutes(slot.slot_start));
return (
<div
key={`${date}-${slot.slot_start}`}
className={`h-full rounded-sm transition-all duration-300 cursor-help ${
slot.sleeping ? 'bg-blue-900' : 'bg-gray-200'
}`}
style={{ flex: duration }}
title={`${slot.slot_start} - ${slot.slot_end} (${duration}m)`}
>
{/* Only show text if there's enough space */}
<div className="overflow-hidden whitespace-nowrap text-[10px] leading-none flex items-center justify-center h-full px-1">
{duration > 40 && <span className="text-white/70">{slot.slot_start}</span>}
</div>
</div>
);
})}
</div>
</div>
))}
+2 -11
View File
@@ -8,7 +8,6 @@ import BedtimePlot from './BedtimePlot';
import { SessionList } from './SessionList';
import { ErrorBoundary } from '../../components/ErrorBoundary';
import { useSettings } from '../../hooks/useSettings';
import TestErrorComponent from '../test-error-component';
const StatsPage: React.FC = () => {
const { settings, update } = useSettings();
@@ -79,16 +78,8 @@ const StatsPage: React.FC = () => {
<ErrorBoundary>
<SessionList />
</ErrorBoundary>
<div className="mt-8 p-4 border-2 border-dashed border-gray-300 rounded">
<h3 className="text-lg font-semibold mb-2 text-gray-600">Error Boundary Test Area</h3>
<ErrorBoundary>
<TestErrorComponent />
</ErrorBoundary>
</div>
</div >
</div>
);
};
export default StatsPage;
export default StatsPage;
@@ -1,7 +0,0 @@
import React from 'react';
const TestErrorComponent = () => {
throw new Error("Test Error");
};
export default TestErrorComponent;
@@ -9,13 +9,6 @@ const SleepButton: React.FC<SleepButtonProps> = ({ time }) => {
const { activeSession, createSession, endSession, isCreating, isEnding } = useSleepSession();
const isSleeping = !!activeSession;
console.log('DEBUG: SleepButton State:', {
activeSession,
isSleeping,
isCreating,
isEnding
});
const handleClick = async () => {
try {
if (isSleeping && activeSession?.id) {
@@ -55,4 +55,4 @@ const TimeAdjuster: React.FC<TimeAdjusterProps> = ({ onTimeChange, defaultValue
);
};
export default TimeAdjuster;
export default TimeAdjuster;
@@ -4,7 +4,13 @@ import SleepButton from './SleepButton';
import TimeAdjuster from './TimeAdjuster';
const TrackerPage: React.FC = () => {
const [time, setTime] = useState<string>(new Date().toISOString().slice(0, 16));
const getLocalTime = () => {
const now = new Date();
const pad = (n: number) => String(n).padStart(2, '0');
return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}T${pad(now.getHours())}:${pad(now.getMinutes())}`;
};
const [time, setTime] = useState<string>(getLocalTime());
return (
<div className="max-w-md mx-auto space-y-6">
@@ -19,4 +25,4 @@ const TrackerPage: React.FC = () => {
);
};
export default TrackerPage;
export default TrackerPage;
+27 -33
View File
@@ -13,7 +13,6 @@ export function formatMinutesAfter(minutes: number | null, reference: string = "
const [refHours, refMinutes] = reference.split(":").map(Number);
const totalRefMinutes = refHours * 60 + refMinutes;
const targetMinutes = totalRefMinutes + minutes;
// Handle wrap around midnight
const normalizedMinutes = ((targetMinutes % 1440) + 1440) % 1440;
@@ -34,26 +33,23 @@ export function toLocalDatetimeInputValue(date: Date): string {
return localDate.toISOString().slice(0, 16);
}
/**
* Format elapsed time in human-readable format
* @param startTime - start time as ISO string or Date
* @returns formatted string like "2h 35m" or "just now"
*/
export function formatElapsedTime(startTime: string | Date): string {
const start = typeof startTime === "string" ? new Date(startTime) : startTime;
export function formatElapsedTime(utcTimestamp: string): string {
let isoString = utcTimestamp;
if (utcTimestamp.includes(' ')) {
isoString = utcTimestamp.replace(' ', 'T');
}
if (!isoString.endsWith('Z')) {
isoString += 'Z';
}
const then = new Date(isoString);
const now = new Date();
const diffMs = now.getTime() - start.getTime();
const diffMs = now - then;
const diffMins = Math.floor(diffMs / 60000);
const hours = Math.floor(diffMins / 60);
const mins = diffMins % 60;
if (diffMs < 0) return "just now";
const totalMinutes = Math.floor(diffMs / 60000);
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
if (hours === 0 && minutes === 0) return "just now";
if (hours === 0) return `${minutes}m`;
if (minutes === 0) return `${hours}h`;
return `${hours}h ${minutes}m`;
if (hours > 0) return `${hours}h ${mins}m`;
return `${mins}m`;
}
/**
@@ -77,6 +73,7 @@ export function clampToDay(date: Date, bound: "start" | "end"): Date {
* @param timeStr - HH:MM format
* @param baseDate - base date (defaults to today)
* @returns Date object
* @throws Error if timeStr is invalid
*/
export function parseTime(timeStr: string, baseDate: Date = new Date()): Date {
const [hours, minutes] = timeStr.split(":").map(Number);
@@ -85,19 +82,6 @@ export function parseTime(timeStr: string, baseDate: Date = new Date()): Date {
return result;
}
/**
* Format minutes as hours and minutes string
* @param minutes - total minutes
* @returns formatted string like "12h 30m"
*/
export function formatMinutes(minutes: number): string {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
if (hours === 0) return `${mins}m`;
if (mins === 0) return `${hours}h`;
return `${hours}h ${mins}m`;
}
/**
* Get date string for N days ago
* @param days - number of days ago
@@ -115,4 +99,14 @@ export function getDaysAgo(days: number): string {
*/
export function getToday(): string {
return new Date().toISOString().split("T")[0];
}
}
/**
* Convert HH:MM time string to total minutes from start of day
* @param timeStr - HH:MM format
* @returns total minutes
*/
export function timeToMinutes(timeStr: string): number {
const [hours, minutes] = timeStr.split(":").map(Number);
return hours * 60 + minutes;
}
+81
View File
@@ -0,0 +1,81 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import StatusBanner from '../../src/components/tracker/StatusBanner';
import { useSleepSession } from '../../src/hooks/useSleepSession';
import { formatElapsedTime } from '../../src/utils/time';
vi.mock('../../src/hooks/useSleepSession', () => ({
useSleepSession: vi.fn(),
}));
describe('StatusBanner - Timezone Fix', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
vi.clearAllMocks();
});
describe('formatElapsedTime', () => {
it('should show correct elapsed time for session started ~49 minutes ago', () => {
vi.setSystemTime(new Date('2026-04-24T08:51:00Z'));
const result = formatElapsedTime('2026-04-24T08:02:00');
expect(result).toBe('49m');
});
it('should show hours and minutes for longer sessions', () => {
vi.setSystemTime(new Date('2026-04-24T10:02:00Z'));
const result = formatElapsedTime('2026-04-24T08:02:00');
expect(result).toBe('2h 0m');
});
it('should show minutes only for sessions under an hour', () => {
vi.setSystemTime(new Date('2026-04-24T08:07:00Z'));
const result = formatElapsedTime('2026-04-24T08:02:00');
expect(result).toBe('5m');
});
});
describe('StatusBanner display', () => {
it('shows correct elapsed time for active session', () => {
vi.setSystemTime(new Date('2026-04-24T08:51:00Z'));
vi.mocked(useSleepSession).mockReturnValue({
activeSession: {
id: 4,
start_time: '2026-04-24T08:02:00',
end_time: null,
notes: null,
created_at: '2026-04-24T08:02:28.942902',
updated_at: '2026-04-24T08:02:28.942927',
},
isLoading: false,
} as any);
render(<StatusBanner />);
expect(screen.getByText(/Baby is sleeping/)).toBeInTheDocument();
expect(screen.getByText(/\(49m\)/)).toBeInTheDocument();
});
it('shows awake state when no active session', () => {
vi.mocked(useSleepSession).mockReturnValue({
activeSession: null,
isLoading: false,
} as any);
render(<StatusBanner />);
expect(screen.getByText(/Baby is awake/)).toBeInTheDocument();
});
});
});
+1 -1
View File
@@ -43,7 +43,7 @@ describe('toLocalDatetimeInputValue', () => {
describe('formatElapsedTime', () => {
it('formats hours and minutes', () => {
const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString();
expect(formatElapsedTime(twoHoursAgo)).toBe('2h');
expect(formatElapsedTime(twoHoursAgo)).toBe('2h 0m');
});
it('formats minutes only', () => {