Skip to content

Commit e463309

Browse files
committed
fix: annotations list show hourly granularity
1 parent 5e621dc commit e463309

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

apps/dashboard/components/charts/annotations-panel.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ interface AnnotationsPanelProps {
3737
onEdit: (annotation: Annotation) => void;
3838
onDelete: (id: string) => Promise<void>;
3939
isDeleting?: boolean;
40+
granularity?: 'hourly' | 'daily' | 'weekly' | 'monthly';
4041
}
4142

4243
export function AnnotationsPanel({
4344
annotations,
4445
onEdit,
4546
onDelete,
4647
isDeleting = false,
48+
granularity = 'daily',
4749
}: AnnotationsPanelProps) {
4850
const [deleteId, setDeleteId] = useState<string | null>(null);
4951
const [isOpen, setIsOpen] = useState(false);
@@ -115,7 +117,11 @@ export function AnnotationsPanel({
115117
style={{ backgroundColor: annotation.color }}
116118
/>
117119
<span className="text-xs text-sidebar-foreground/70">
118-
{formatAnnotationDateRange(annotation.xValue, annotation.xEndValue)}
120+
{formatAnnotationDateRange(
121+
annotation.xValue,
122+
annotation.xEndValue,
123+
granularity
124+
)}
119125
</span>
120126
{annotation.annotationType === 'range' &&
121127
annotation.xEndValue &&

apps/dashboard/components/charts/metrics-chart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ export function MetricsChart({
322322
annotations={annotations}
323323
onEdit={onEditAnnotation || (() => {})}
324324
onDelete={onDeleteAnnotation || (async () => {})}
325+
granularity={granularity}
325326
/>
326327
</div>
327328
)}

apps/dashboard/lib/annotation-utils.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@ type Granularity = 'hourly' | 'daily' | 'weekly' | 'monthly';
55

66
/**
77
* Formats a date to a readable string
8+
* Shows time if the date is within a 24-hour period or spans less than a day
89
*/
9-
export function formatAnnotationDate(date: Date | string): string {
10-
return new Date(date).toLocaleDateString('en-US', {
11-
month: 'short',
12-
day: 'numeric',
13-
year: 'numeric',
14-
});
10+
export function formatAnnotationDate(date: Date | string, showTime = false): string {
11+
const dateObj = dayjs(date);
12+
if (showTime) {
13+
return dateObj.format('MMM D, h:mm A');
14+
}
15+
return dateObj.format('MMM D, YYYY');
1516
}
1617

1718
/**
1819
* Formats a date range for annotations
20+
* Automatically detects if hourly format is needed based on granularity or date range
1921
*/
2022
export function formatAnnotationDateRange(
2123
start: Date | string,
22-
end: Date | string | null
24+
end: Date | string | null,
25+
granularity: Granularity = 'daily'
2326
): string {
24-
const startDate = new Date(start);
25-
const endDate = end ? new Date(end) : null;
27+
const startDate = dayjs(start);
28+
const endDate = end ? dayjs(end) : null;
29+
30+
// If hourly granularity, always show time
31+
const isHourly = granularity === 'hourly';
2632

27-
if (!endDate || startDate.getTime() === endDate.getTime()) {
28-
return formatAnnotationDate(startDate);
33+
if (!endDate || startDate.isSame(endDate)) {
34+
// For single date, show time if hourly or if time is not midnight
35+
const showTime = isHourly || !startDate.isSame(startDate.startOf('day'));
36+
return formatAnnotationDate(start, showTime);
2937
}
30-
return `${formatAnnotationDate(startDate)} - ${formatAnnotationDate(endDate)}`;
38+
39+
// Check if the range spans less than 24 hours or if times differ on same day
40+
const isHourlyRange = startDate.isSame(endDate, 'day') || endDate.diff(startDate, 'hour') < 24;
41+
42+
// If hourly granularity or range is within same day, show time
43+
const showTime = isHourly || isHourlyRange || startDate.isSame(endDate, 'day');
44+
45+
return `${formatAnnotationDate(start, showTime)} - ${formatAnnotationDate(end as Date | string, showTime)}`;
3146
}
3247

3348
/**

0 commit comments

Comments
 (0)