Annotations workflow, UI/UX polish

This commit is contained in:
Francis Cao committed 2026-09-03 12:59:56 -07:00
1 parent 26e236c95c
commit 336fbcb3c5
18 files changed
+463 -87

No files matched your search

+1
View File
@@ -443,6 +443,7 @@
"incorrect-username-password": "Incorrect username/password.", "incorrect-username-password": "Incorrect username/password.",
"invalid-domain": "Invalid domain", "invalid-domain": "Invalid domain",
"invalid-value": "Invalid value", "invalid-value": "Invalid value",
"max-length": "Maximum length of {n} characters",
"min-password-length": "Minimum length of {n} characters", "min-password-length": "Minimum length of {n} characters",
"new-version-available": "A new version of Umami {version} is available!", "new-version-available": "A new version of Umami {version} is available!",
"no-data-available": "No data available.", "no-data-available": "No data available.",
+1
View File
@@ -452,6 +452,7 @@
"incorrect-username-password": "Incorrect username and/or password.", "incorrect-username-password": "Incorrect username and/or password.",
"invalid-domain": "Invalid domain. Do not include http/https.", "invalid-domain": "Invalid domain. Do not include http/https.",
"invalid-value": "Invalid value", "invalid-value": "Invalid value",
"max-length": "Maximum length of {n} characters",
"min-password-length": "Minimum length of {n} characters", "min-password-length": "Minimum length of {n} characters",
"new-version-available": "A new version of Umami {version} is available!", "new-version-available": "A new version of Umami {version} is available!",
"no-data-available": "No data available.", "no-data-available": "No data available.",
+12
View File
@@ -46,6 +46,7 @@ export function TopNav() {
paddingRight="5" paddingRight="5"
width="100%" width="100%"
zIndex={100} zIndex={100}
backgroundColor="surface-raised"
> >
<Row alignItems="center"> <Row alignItems="center">
<TeamsButton /> <TeamsButton />
@@ -101,6 +102,17 @@ export function TopNav() {
</> </>
)} )}
</Row> </Row>
<div
style={{
position: 'absolute',
bottom: -16,
left: 0,
right: 0,
height: 16,
background: 'linear-gradient(to bottom, var(--zen-surface-raised), transparent)',
pointerEvents: 'none',
}}
/>
</Row> </Row>
); );
} }
@@ -1,3 +1,4 @@
import { isSameDay } from 'date-fns';
import { useCallback, useMemo } from 'react'; import { useCallback, useMemo } from 'react';
import type { ChartAnnotation } from '@/components/charts/ChartAnnotationMarkers'; import type { ChartAnnotation } from '@/components/charts/ChartAnnotationMarkers';
import { LoadingPanel } from '@/components/common/LoadingPanel'; import { LoadingPanel } from '@/components/common/LoadingPanel';
@@ -10,30 +11,37 @@ import {
} from '@/components/hooks'; } from '@/components/hooks';
import { useWebsitePageviewsQuery } from '@/components/hooks/queries/useWebsitePageviewsQuery'; import { useWebsitePageviewsQuery } from '@/components/hooks/queries/useWebsitePageviewsQuery';
import { PageviewsChart } from '@/components/metrics/PageviewsChart'; import { PageviewsChart } from '@/components/metrics/PageviewsChart';
import { getAnnotationDateRangeValue } from '@/lib/annotations'; import { type AnnotationRange, getAnnotationDateRangeValue } from '@/lib/annotations';
import { DATE_FUNCTIONS } from '@/lib/date';
export function WebsiteChart({ export function WebsiteChart({
websiteId, websiteId,
compareMode, compareMode,
showAnnotations,
onAnnotationMoreClick,
}: { }: {
websiteId: string; websiteId: string;
compareMode?: boolean; compareMode?: boolean;
showAnnotations?: boolean;
onAnnotationMoreClick?: (range: AnnotationRange) => void;
}) { }) {
const { timezone, localFromUtc } = useTimezone(); const { timezone, localFromUtc, localToUtc } = useTimezone();
const { dateRange, dateCompare } = useDateRange({ timezone: timezone }); const { dateRange, dateCompare } = useDateRange({ timezone: timezone });
const { startDate, endDate, unit, value } = dateRange; const { startDate, endDate, unit, value } = dateRange;
const { startAt, endAt } = useDateParameters(); const { startAt, endAt } = useDateParameters();
const { router, updateParams } = useNavigation(); const { router, updateParams } = useNavigation();
const { data: annotationData } = useWebsiteAnnotationsQuery(websiteId, { const { data: annotationData } = useWebsiteAnnotationsQuery(
startAt, websiteId,
endAt, { startAt, endAt, pageSize: 1000 },
pageSize: 1000, { enabled: !!showAnnotations && !!websiteId },
}); );
const { data, isLoading, isFetching, error } = useWebsitePageviewsQuery({ const { data, isLoading, isFetching, error } = useWebsitePageviewsQuery({
websiteId, websiteId,
compare: compareMode ? dateCompare?.compare : undefined, compare: compareMode ? dateCompare?.compare : undefined,
}); });
const { pageviews, sessions, compare } = (data || {}) as any; const { pageviews, sessions, compare } = (data || {}) as any;
const canDrillIntoAnnotation =
unit !== 'hour' && unit !== 'minute' && !isSameDay(startDate, endDate);
const chartData = useMemo(() => { const chartData = useMemo(() => {
if (!data) { if (!data) {
@@ -61,19 +69,47 @@ export function WebsiteChart({
}, [data, startDate, endDate, unit]); }, [data, startDate, endDate, unit]);
const annotations = useMemo<ChartAnnotation[]>(() => { const annotations = useMemo<ChartAnnotation[]>(() => {
return (annotationData?.data || []).map(({ id, date, note, allDay }) => ({ const isSubDayUnit = unit === 'hour' || unit === 'minute';
id,
date: localFromUtc(new Date(date)), return (annotationData?.data || [])
label: note, .filter(({ allDay }) => !isSubDayUnit || allDay === false)
allDay, .map(({ id, date, note, allDay }) => {
})); const annotationDate = localFromUtc(new Date(date));
}, [annotationData, timezone]);
return {
id,
date: annotationDate,
markerDate: DATE_FUNCTIONS[unit].start(annotationDate),
label: note,
allDay,
isClickable: canDrillIntoAnnotation,
isGroupClickable: unit === 'month',
};
});
}, [annotationData, timezone, unit, canDrillIntoAnnotation]);
const handleAnnotationClick = useCallback( const handleAnnotationClick = useCallback(
(annotation: ChartAnnotation) => { (annotations: ChartAnnotation[]) => {
const [annotation] = annotations;
const hasOneDate = annotations.every(item => isSameDay(item.date, annotation.date));
if (hasOneDate) {
router.push(
updateParams({
date: getAnnotationDateRangeValue(annotation.date, annotation.allDay !== false),
offset: undefined,
}),
);
return;
}
const markerDate = annotation.markerDate || annotation.date;
const startDate = DATE_FUNCTIONS.month.start(markerDate);
const endDate = DATE_FUNCTIONS.month.end(markerDate);
router.push( router.push(
updateParams({ updateParams({
date: getAnnotationDateRangeValue(annotation.date, annotation.allDay !== false), date: `range:${startDate.getTime()}:${endDate.getTime()}`,
offset: undefined, offset: undefined,
}), }),
); );
@@ -81,6 +117,19 @@ export function WebsiteChart({
[router, updateParams], [router, updateParams],
); );
const handleAnnotationMoreClick = useCallback(
(annotations: ChartAnnotation[]) => {
const markerDate = annotations[0].markerDate || annotations[0].date;
const { start, end } = DATE_FUNCTIONS[unit];
onAnnotationMoreClick?.({
startAt: +localToUtc(start(markerDate)),
endAt: +localToUtc(end(markerDate)),
});
},
[localToUtc, onAnnotationMoreClick, unit],
);
return ( return (
<LoadingPanel data={data} isFetching={isFetching} isLoading={isLoading} error={error}> <LoadingPanel data={data} isFetching={isFetching} isLoading={isLoading} error={error}>
<PageviewsChart <PageviewsChart
@@ -91,6 +140,7 @@ export function WebsiteChart({
unit={unit} unit={unit}
annotations={annotations} annotations={annotations}
onAnnotationClick={handleAnnotationClick} onAnnotationClick={handleAnnotationClick}
onAnnotationMoreClick={onAnnotationMoreClick ? handleAnnotationMoreClick : undefined}
/> />
</LoadingPanel> </LoadingPanel>
); );
@@ -1,15 +1,21 @@
'use client'; 'use client';
import { Column, Row } from '@umami/react-zen'; import { Column, Row } from '@umami/react-zen';
import { useState } from 'react';
import { ExpandedViewModal } from '@/app/(main)/websites/[websiteId]/ExpandedViewModal'; import { ExpandedViewModal } from '@/app/(main)/websites/[websiteId]/ExpandedViewModal';
import { Panel } from '@/components/common/Panel'; import { Panel } from '@/components/common/Panel';
import { DialogButton } from '@/components/input/DialogButton';
import type { AnnotationRange } from '@/lib/annotations';
import { UnitFilter } from '@/components/input/UnitFilter'; import { UnitFilter } from '@/components/input/UnitFilter';
import { AnnotationsButton } from './annotations/AnnotationsButton'; import { AnnotationsButton } from './annotations/AnnotationsButton';
import { AnnotationsModal } from './annotations/AnnotationsModal';
import { WebsiteChart } from './WebsiteChart'; import { WebsiteChart } from './WebsiteChart';
import { WebsiteControls } from './WebsiteControls'; import { WebsiteControls } from './WebsiteControls';
import { WebsiteMetricsBar } from './WebsiteMetricsBar'; import { WebsiteMetricsBar } from './WebsiteMetricsBar';
import { WebsitePanels } from './WebsitePanels'; import { WebsitePanels } from './WebsitePanels';
export function WebsitePage({ websiteId }: { websiteId: string }) { export function WebsitePage({ websiteId }: { websiteId: string }) {
const [annotationRange, setAnnotationRange] = useState<AnnotationRange | null>(null);
return ( return (
<Column gap> <Column gap>
<WebsiteControls websiteId={websiteId} allowBounceFilter={true} /> <WebsiteControls websiteId={websiteId} allowBounceFilter={true} />
@@ -18,13 +24,36 @@ export function WebsitePage({ websiteId }: { websiteId: string }) {
<Row justifyContent="end"> <Row justifyContent="end">
<UnitFilter /> <UnitFilter />
</Row> </Row>
<WebsiteChart websiteId={websiteId} /> <WebsiteChart
websiteId={websiteId}
showAnnotations
onAnnotationMoreClick={setAnnotationRange}
/>
<Row justifyContent="end"> <Row justifyContent="end">
<AnnotationsButton websiteId={websiteId} /> <AnnotationsButton websiteId={websiteId} />
</Row> </Row>
</Panel> </Panel>
<WebsitePanels websiteId={websiteId} /> <WebsitePanels websiteId={websiteId} />
<ExpandedViewModal websiteId={websiteId} /> <ExpandedViewModal websiteId={websiteId} />
<DialogButton
isOpen={!!annotationRange}
onOpenChange={isOpen => !isOpen && setAnnotationRange(null)}
title={null}
width="800px"
>
{({ close }) =>
annotationRange && (
<AnnotationsModal
websiteId={websiteId}
range={annotationRange}
onClose={() => {
close();
setAnnotationRange(null);
}}
/>
)
}
</DialogButton>
</Column> </Column>
); );
} }
@@ -6,12 +6,10 @@ import { DialogButton } from '@/components/input/DialogButton';
export function AnnotationDeleteButton({ export function AnnotationDeleteButton({
annotationId, annotationId,
websiteId, websiteId,
name,
onSave, onSave,
}: { }: {
annotationId: string; annotationId: string;
websiteId: string; websiteId: string;
name: string;
onSave?: () => void; onSave?: () => void;
}) { }) {
const { t, labels, messages } = useMessages(); const { t, labels, messages } = useMessages();
@@ -33,10 +31,7 @@ export function AnnotationDeleteButton({
<DialogButton icon={<Trash />} variant="quiet" title={t(labels.confirm)} width="400px"> <DialogButton icon={<Trash />} variant="quiet" title={t(labels.confirm)} width="400px">
{({ close }) => ( {({ close }) => (
<ConfirmationForm <ConfirmationForm
message={t.rich(messages.confirmRemove, { message={t(messages.confirmDelete, { target: t(labels.note) })}
target: name,
b: chunks => <b>{chunks}</b>,
})}
isLoading={isPending} isLoading={isPending}
error={error} error={error}
onConfirm={handleConfirm.bind(null, close)} onConfirm={handleConfirm.bind(null, close)}
@@ -1,17 +1,23 @@
import { import {
Button, Button,
Calendar as ZenCalendar,
Column, Column,
DatePicker, DialogTrigger,
Form, Form,
FormButtons, FormButtons,
FormField, FormField,
FormSubmitButton, FormSubmitButton,
Icon,
Label, Label,
Popover,
Text,
TextField, TextField,
} from '@umami/react-zen'; } from '@umami/react-zen';
import { startOfDay } from 'date-fns'; import { startOfDay } from 'date-fns';
import { useMemo, useState } from 'react'; import { useMemo, useState } from 'react';
import { useLocale, useMessages, useTimezone, useUpdateQuery } from '@/components/hooks'; import { useLocale, useMessages, useTimezone, useUpdateQuery } from '@/components/hooks';
import { Calendar } from '@/components/icons';
import { formatDate } from '@/lib/date';
export interface Annotation { export interface Annotation {
id: string; id: string;
@@ -21,6 +27,51 @@ export interface Annotation {
note: string; note: string;
} }
function AnnotationDatePicker({
date,
label,
locale,
onChange,
}: {
date: Date;
label: string;
locale: string;
onChange: (date: Date) => void;
}) {
const [isOpen, setIsOpen] = useState(false);
const handleChange = (value: Date) => {
onChange(value);
setIsOpen(false);
};
return (
<Column gap="1">
<Label>{label}</Label>
<DialogTrigger overlayType="popover" isOpen={isOpen} onOpenChange={setIsOpen}>
<Button
variant="outline"
aria-label={label}
style={{ width: 'fit-content', cursor: 'pointer', flexDirection: 'row-reverse' }}
>
<Icon size="sm">
<Calendar />
</Icon>
{formatDate(date, 'PP', locale)}
</Button>
<Popover
side="bottom"
align="start"
sideOffset={8}
className="min-w-[340px] bg-surface-overlay border border-edge-muted rounded-lg shadow-lg p-4"
>
<ZenCalendar value={date} onChange={handleChange} />
</Popover>
</DialogTrigger>
</Column>
);
}
export function AnnotationEditForm({ export function AnnotationEditForm({
annotation, annotation,
websiteId, websiteId,
@@ -70,29 +121,51 @@ export function AnnotationEditForm({
onSubmit={handleSubmit} onSubmit={handleSubmit}
defaultValues={{ note: annotation?.note || '' }} defaultValues={{ note: annotation?.note || '' }}
> >
<Column gap> {({ watch }) => {
<Column> const noteLength = (watch('note') || '').length;
<Label>{t(labels.date)}</Label> const isOverLimit = noteLength > 500;
<DatePicker value={date} locale={locale} onChange={value => value && setDate(value)} />
</Column>
<FormField return (
name="note" <Column gap>
label={t(labels.note)} <AnnotationDatePicker
rules={{ required: t(labels.required), maxLength: 500 }} label={t(labels.date)}
> date={date}
<TextField asTextArea autoFocus /> locale={locale}
</FormField> onChange={setDate}
/>
<FormButtons> <Column gap="1">
<Button isDisabled={isPending} onPress={onClose}> <FormField
{t(labels.cancel)} name="note"
</Button> label={t(labels.note)}
<FormSubmitButton variant="primary" isDisabled={isPending}> rules={{
{t(labels.save)} required: t(labels.required),
</FormSubmitButton> maxLength: { value: 500, message: t(messages.maxLength, { n: '500' }) },
</FormButtons> }}
</Column> >
<TextField asTextArea autoFocus style={{ minHeight: 128 }} />
</FormField>
<Text
size="sm"
color="muted"
align="right"
style={isOverLimit ? { color: 'var(--zen-status-error)' } : undefined}
>
{noteLength} / 500
</Text>
</Column>
<FormButtons>
<Button isDisabled={isPending} onPress={onClose}>
{t(labels.cancel)}
</Button>
<FormSubmitButton variant="primary" isDisabled={isPending}>
{t(labels.save)}
</FormSubmitButton>
</FormButtons>
</Column>
);
}}
</Form> </Form>
); );
} }
@@ -1,15 +1,11 @@
import { useMessages, useShare } from '@/components/hooks'; import { useDateParameters, useMessages } from '@/components/hooks';
import { MessageSquareText } from '@/components/icons'; import { MessageSquareText } from '@/components/icons';
import { DialogButton } from '@/components/input/DialogButton'; import { DialogButton } from '@/components/input/DialogButton';
import { AnnotationsModal } from './AnnotationsModal'; import { AnnotationsModal } from './AnnotationsModal';
export function AnnotationsButton({ websiteId }: { websiteId: string }) { export function AnnotationsButton({ websiteId }: { websiteId: string }) {
const { t, labels } = useMessages(); const { t, labels } = useMessages();
const share = useShare(); const { startAt, endAt } = useDateParameters();
if (share) {
return null;
}
return ( return (
<DialogButton <DialogButton
@@ -19,7 +15,9 @@ export function AnnotationsButton({ websiteId }: { websiteId: string }) {
variant="quiet" variant="quiet"
width="800px" width="800px"
> >
{({ close }) => <AnnotationsModal websiteId={websiteId} onClose={close} />} {({ close }) => (
<AnnotationsModal websiteId={websiteId} range={{ startAt, endAt }} onClose={close} />
)}
</DialogButton> </DialogButton>
); );
} }
@@ -0,0 +1,66 @@
import { beforeEach, expect, test, vi } from 'vitest';
import { render, screen } from '@/test/render';
import { AnnotationsModal } from './AnnotationsModal';
const mockUseWebsiteAnnotationsQuery = vi.fn();
vi.mock('@/components/charts/ChartAnnotationMarkers', () => ({
formatAnnotationDate: () => '',
}));
vi.mock('@/components/hooks', () => ({
useLocale: () => ({ locale: 'en-US' }),
useMessages: () => ({
t: (value: string) => value,
labels: {
addNote: 'Add note',
all: 'All',
current: 'Current',
date: 'Date',
dateRange: 'date range',
note: 'Note',
notes: 'Notes',
},
messages: {
noDataAvailable: 'No data available',
},
}),
useMobile: () => ({ isMobile: false }),
useNavigation: () => ({ router: { push: vi.fn() }, updateParams: vi.fn() }),
useShare: () => null,
useTimezone: () => ({ localFromUtc: (date: Date) => date }),
useWebsiteAnnotationsQuery: (...args: unknown[]) => mockUseWebsiteAnnotationsQuery(...args),
}));
vi.mock('@/components/input/FilterButtons', () => ({
FilterButtons: ({ onChange }: { onChange: (value: string) => void }) => (
<button type="button" onClick={() => onChange('all')}>
All notes
</button>
),
}));
beforeEach(() => {
mockUseWebsiteAnnotationsQuery.mockClear();
mockUseWebsiteAnnotationsQuery.mockReturnValue({ data: { data: [], count: 0 }, isLoading: false });
});
test('defaults to the supplied date range and can show all notes', async () => {
const { user } = render(
<AnnotationsModal websiteId="website-1" range={{ startAt: 1000, endAt: 2000 }} />,
);
expect(mockUseWebsiteAnnotationsQuery).toHaveBeenLastCalledWith('website-1', {
page: 1,
pageSize: 20,
startAt: 1000,
endAt: 2000,
});
await user.click(screen.getByRole('button', { name: 'All notes' }));
expect(mockUseWebsiteAnnotationsQuery).toHaveBeenLastCalledWith('website-1', {
page: 1,
pageSize: 20,
});
});
@@ -13,44 +13,61 @@ import { formatAnnotationDate } from '@/components/charts/ChartAnnotationMarkers
import { Empty } from '@/components/common/Empty'; import { Empty } from '@/components/common/Empty';
import { IconLabel } from '@/components/common/IconLabel'; import { IconLabel } from '@/components/common/IconLabel';
import { Pager } from '@/components/common/Pager'; import { Pager } from '@/components/common/Pager';
import { FilterButtons } from '@/components/input/FilterButtons';
import { import {
useLocale, useLocale,
useMessages, useMessages,
useMobile,
useNavigation, useNavigation,
useShare,
useTimezone, useTimezone,
useWebsiteAnnotationsQuery, useWebsiteAnnotationsQuery,
} from '@/components/hooks'; } from '@/components/hooks';
import { Edit, Plus } from '@/components/icons'; import { Edit, Plus } from '@/components/icons';
import { getAnnotationDateRangeValue } from '@/lib/annotations'; import { type AnnotationRange, getAnnotationDateRangeValue } from '@/lib/annotations';
import { AnnotationDeleteButton } from './AnnotationDeleteButton'; import { AnnotationDeleteButton } from './AnnotationDeleteButton';
import { type Annotation, AnnotationEditForm } from './AnnotationEditForm'; import { type Annotation, AnnotationEditForm } from './AnnotationEditForm';
const PAGE_SIZE = 20; const PAGE_SIZE = 20;
const RANGE_FILTER = 'range';
const ALL_FILTER = 'all';
type View = { mode: 'list' } | { mode: 'add' } | { mode: 'edit'; annotation: Annotation }; type View = { mode: 'list' } | { mode: 'add' } | { mode: 'edit'; annotation: Annotation };
export function AnnotationsModal({ export function AnnotationsModal({
websiteId, websiteId,
range,
onClose, onClose,
}: { }: {
websiteId: string; websiteId: string;
range: AnnotationRange;
onClose?: () => void; onClose?: () => void;
}) { }) {
const { t, labels } = useMessages(); const { t, labels } = useMessages();
const { locale } = useLocale(); const { locale } = useLocale();
const { localFromUtc } = useTimezone(); const { localFromUtc } = useTimezone();
const { isMobile } = useMobile();
const share = useShare();
const { router, updateParams } = useNavigation(); const { router, updateParams } = useNavigation();
const [view, setView] = useState<View>({ mode: 'list' }); const [view, setView] = useState<View>({ mode: 'list' });
const [page, setPage] = useState(1); const [page, setPage] = useState(1);
const [filter, setFilter] = useState(RANGE_FILTER);
const { data, isLoading } = useWebsiteAnnotationsQuery(websiteId, { const { data, isLoading } = useWebsiteAnnotationsQuery(websiteId, {
page, page,
pageSize: PAGE_SIZE, pageSize: PAGE_SIZE,
...(filter === RANGE_FILTER && range),
}); });
const showList = () => setView({ mode: 'list' }); const showList = () => setView({ mode: 'list' });
const toLocalDate = (annotation: Annotation) => localFromUtc(new Date(annotation.date)); const toLocalDate = (annotation: Annotation) => localFromUtc(new Date(annotation.date));
const handleFilterChange = (value: string) => {
setFilter(value);
setPage(1);
};
const handleSelect = (annotation: Annotation) => { const handleSelect = (annotation: Annotation) => {
router.push( router.push(
updateParams({ updateParams({
@@ -85,15 +102,25 @@ export function AnnotationsModal({
<Column gap> <Column gap>
<Row alignItems="center" justifyContent="space-between"> <Row alignItems="center" justifyContent="space-between">
<Heading size="xl">{t(labels.notes)}</Heading> <Heading size="xl">{t(labels.notes)}</Heading>
<Button variant="primary" onPress={() => setView({ mode: 'add' })}> {!share && (
<Plus /> <Button variant="primary" onPress={() => setView({ mode: 'add' })}>
{t(labels.addNote)} <Plus />
</Button> {t(labels.addNote)}
</Button>
)}
</Row> </Row>
<FilterButtons
value={filter}
onChange={handleFilterChange}
items={[
{ id: RANGE_FILTER, label: `${t(labels.current)} ${t(labels.dateRange)}` },
{ id: ALL_FILTER, label: `${t(labels.all)} ${t(labels.notes)}` },
]}
/>
{rows.length === 0 ? ( {rows.length === 0 ? (
<Empty /> <Empty />
) : ( ) : (
<DataTable data={rows}> <DataTable data={rows} displayMode={isMobile ? 'cards' : 'table'}>
<DataColumn id="date" label={t(labels.date)} width="200px"> <DataColumn id="date" label={t(labels.date)} width="200px">
{(row: Annotation) => ( {(row: Annotation) => (
<Button onPress={() => handleSelect(row)}> <Button onPress={() => handleSelect(row)}>
@@ -107,25 +134,31 @@ export function AnnotationsModal({
<DataColumn id="note" label={t(labels.note)}> <DataColumn id="note" label={t(labels.note)}>
{(row: Annotation) => <Text style={{ whiteSpace: 'pre-wrap' }}>{row.note}</Text>} {(row: Annotation) => <Text style={{ whiteSpace: 'pre-wrap' }}>{row.note}</Text>}
</DataColumn> </DataColumn>
<DataColumn id="action" align="end" width="100px"> {!share && (
{(row: Annotation) => ( <DataColumn id="action" align="end" width="100px">
<Row> {(row: Annotation) => (
<Button variant="quiet" onPress={() => setView({ mode: 'edit', annotation: row })}> <Row>
<IconLabel icon={<Edit />} /> <Button
</Button> variant="quiet"
<AnnotationDeleteButton onPress={() => setView({ mode: 'edit', annotation: row })}
annotationId={row.id} >
websiteId={websiteId} <IconLabel icon={<Edit />} />
name={row.note} </Button>
/> <AnnotationDeleteButton annotationId={row.id} websiteId={websiteId} />
</Row> </Row>
)} )}
</DataColumn> </DataColumn>
)}
</DataTable> </DataTable>
)} )}
{data?.count > PAGE_SIZE && ( {data?.count > PAGE_SIZE && (
<Pager page={page} pageSize={PAGE_SIZE} count={data.count} onPageChange={setPage} /> <Pager page={page} pageSize={PAGE_SIZE} count={data.count} onPageChange={setPage} />
)} )}
{onClose && (
<Row justifyContent="flex-end">
<Button onPress={onClose}>{t(labels.close)}</Button>
</Row>
)}
</Column> </Column>
); );
} }
+16 -3
View File
@@ -12,6 +12,7 @@ import {
ChartAnnotationMarkers, ChartAnnotationMarkers,
} from '@/components/charts/ChartAnnotationMarkers'; } from '@/components/charts/ChartAnnotationMarkers';
import { Legend } from '@/components/metrics/Legend'; import { Legend } from '@/components/metrics/Legend';
import { getChartBucketIndex } from '@/lib/charts';
import { DEFAULT_ANIMATION_DURATION } from '@/lib/constants'; import { DEFAULT_ANIMATION_DURATION } from '@/lib/constants';
ChartJS.defaults.font.family = 'Inter'; ChartJS.defaults.font.family = 'Inter';
@@ -26,7 +27,8 @@ export interface ChartProps extends BoxProps {
hiddenLabels?: Set<string>; hiddenLabels?: Set<string>;
onLegendClick?: (label: string, willBeHidden: boolean) => void; onLegendClick?: (label: string, willBeHidden: boolean) => void;
annotations?: ChartAnnotation[]; annotations?: ChartAnnotation[];
onAnnotationClick?: (annotation: ChartAnnotation) => void; onAnnotationClick?: (annotations: ChartAnnotation[]) => void;
onAnnotationMoreClick?: (annotations: ChartAnnotation[]) => void;
} }
function isSameMarkers(a: AnnotationMarker[], b: AnnotationMarker[]) { function isSameMarkers(a: AnnotationMarker[], b: AnnotationMarker[]) {
@@ -53,6 +55,7 @@ export function Chart({
onLegendClick, onLegendClick,
annotations, annotations,
onAnnotationClick, onAnnotationClick,
onAnnotationMoreClick,
...props ...props
}: ChartProps) { }: ChartProps) {
const canvas = useRef(null); const canvas = useRef(null);
@@ -79,7 +82,13 @@ export function Chart({
const groups = new Map<number, AnnotationMarker>(); const groups = new Map<number, AnnotationMarker>();
for (const annotation of list) { for (const annotation of list) {
const x = Math.round(scales.x.getPixelForValue(annotation.date.getTime())); const markerDate = annotation.markerDate || annotation.date;
const bucketIndex = getChartBucketIndex(
instance.data.datasets[0]?.data || [],
markerDate,
);
const bucket = instance.getDatasetMeta(0)?.data[bucketIndex];
const x = Math.round(bucket?.x ?? scales.x.getPixelForValue(markerDate.getTime()));
if (!Number.isFinite(x) || x < chartArea.left || x > chartArea.right) { if (!Number.isFinite(x) || x < chartArea.left || x > chartArea.right) {
continue; continue;
@@ -217,7 +226,11 @@ export function Chart({
<div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden' }}> <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden' }}>
<canvas ref={canvas} style={{ position: 'absolute', top: 0, left: 0 }} /> <canvas ref={canvas} style={{ position: 'absolute', top: 0, left: 0 }} />
{markers.length > 0 && ( {markers.length > 0 && (
<ChartAnnotationMarkers markers={markers} onClick={onAnnotationClick} /> <ChartAnnotationMarkers
markers={markers}
onClick={onAnnotationClick}
onMoreClick={onAnnotationMoreClick}
/>
)} )}
</div> </div>
</Box> </Box>
@@ -37,6 +37,34 @@
.note { .note {
max-width: 300px; max-width: 300px;
max-height: min(280px, 50vh);
overflow-y: auto;
white-space: pre-wrap; white-space: pre-wrap;
word-break: break-word; word-break: break-word;
} }
.noteFull {
display: block;
}
.noteText {
display: -webkit-box;
overflow: hidden;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
.moreButton {
align-self: center;
min-height: 0;
padding: 2px 4px;
border-radius: 4px;
color: inherit;
opacity: 0.8;
}
.moreButton:hover,
.moreButton:focus-visible {
background-color: rgba(255, 255, 255, 0.15);
opacity: 1;
}
@@ -1,15 +1,23 @@
import { Column, Text, Tooltip, TooltipTrigger, useTheme } from '@umami/react-zen'; import { IconLabel } from '@/components/common/IconLabel';
import { useMemo } from 'react'; import { useLocale, useMessages } from '@/components/hooks';
import { useLocale } from '@/components/hooks'; import { Maximize } from '@/components/icons';
import { getThemeColors } from '@/lib/colors'; import { getThemeColors } from '@/lib/colors';
import { formatDate } from '@/lib/date'; import { formatDate } from '@/lib/date';
import { Button, Column, Text, Tooltip, TooltipTrigger, useTheme } from '@umami/react-zen';
import { isSameDay } from 'date-fns';
import { useMemo } from 'react';
import styles from './ChartAnnotationMarkers.module.css'; import styles from './ChartAnnotationMarkers.module.css';
const MAX_TOOLTIP_ANNOTATIONS = 3;
export interface ChartAnnotation { export interface ChartAnnotation {
id: string; id: string;
date: Date; date: Date;
markerDate?: Date;
label: string; label: string;
allDay?: boolean; allDay?: boolean;
isClickable?: boolean;
isGroupClickable?: boolean;
} }
export interface AnnotationMarker { export interface AnnotationMarker {
@@ -25,40 +33,80 @@ export function formatAnnotationDate(annotation: ChartAnnotation, locale: string
export function ChartAnnotationMarkers({ export function ChartAnnotationMarkers({
markers, markers,
onClick, onClick,
onMoreClick,
}: { }: {
markers: AnnotationMarker[]; markers: AnnotationMarker[];
onClick?: (annotation: ChartAnnotation) => void; onClick?: (annotations: ChartAnnotation[]) => void;
onMoreClick?: (annotations: ChartAnnotation[]) => void;
}) { }) {
const { theme } = useTheme(); const { theme } = useTheme();
const { locale } = useLocale(); const { locale } = useLocale();
const { t, labels } = useMessages();
const { colors } = useMemo(() => getThemeColors(theme), [theme]); const { colors } = useMemo(() => getThemeColors(theme), [theme]);
return ( return (
<> <>
{markers.map(({ annotations, x, y }) => { {markers.map(({ annotations, x, y }) => {
const [first] = annotations; const [first] = annotations;
const hasOneDate = annotations.every(annotation => isSameDay(annotation.date, first.date));
const isClickable =
!!onClick &&
(hasOneDate
? first.isClickable !== false
: annotations.every(annotation => annotation.isGroupClickable === true));
const visibleAnnotations = annotations.slice(0, MAX_TOOLTIP_ANNOTATIONS);
return ( return (
<TooltipTrigger key={first.id} delay={0}> <TooltipTrigger key={first.id} delay={0}>
<button <button
type="button" type="button"
className={styles.marker} className={styles.marker}
style={{ left: x, top: y, backgroundColor: colors.chart.views.hoverBorderColor }} style={{
left: x,
top: y,
cursor: isClickable ? 'pointer' : 'default',
backgroundColor: colors.chart.views.hoverBorderColor,
}}
aria-label={annotations.map(({ label }) => label).join(', ')} aria-label={annotations.map(({ label }) => label).join(', ')}
onClick={() => onClick?.(first)} onClick={isClickable ? () => onClick?.(annotations) : undefined}
> >
{annotations.length > 1 && <span className={styles.count}>{annotations.length}</span>} {annotations.length > 1 && <span className={styles.count}>{annotations.length}</span>}
</button> </button>
<Tooltip placement="top"> <Tooltip placement="top">
<Column gap="3" className={styles.note}> <Column
{annotations.map(annotation => ( gap="3"
padding="1"
borderRadius="md"
className={styles.note}
style={{ backgroundColor: 'rgba(0,0,0,0.2)', color: 'white' }}
>
{visibleAnnotations.map(annotation => (
<Column key={annotation.id} gap="1"> <Column key={annotation.id} gap="1">
<Text size="sm" weight="bold"> <Text size="sm" weight="bold">
{formatAnnotationDate(annotation, locale)} {formatAnnotationDate(annotation, locale)}
</Text> </Text>
<Text size="sm">{annotation.label}</Text> <Text
size="sm"
className={annotations.length === 1 ? styles.noteFull : styles.noteText}
>
{annotation.label}
</Text>
</Column> </Column>
))} ))}
{annotations.length > 1 &&
(onMoreClick ? (
<Button
variant="quiet"
className={styles.moreButton}
onPress={() => onMoreClick(annotations)}
>
<IconLabel icon={<Maximize />}>{t(labels.view)}</IconLabel>
</Button>
) : (
<IconLabel icon={<Maximize />} style={{ opacity: 0.8 }}>
{t(labels.view)}
</IconLabel>
))}
</Column> </Column>
</Tooltip> </Tooltip>
</TooltipTrigger> </TooltipTrigger>
+2 -2
View File
@@ -3,7 +3,7 @@ import type { ReactNode } from 'react';
import { useMessages } from '@/components/hooks'; import { useMessages } from '@/components/hooks';
export interface ConfirmationFormProps { export interface ConfirmationFormProps {
message: ReactNode; message?: ReactNode;
buttonLabel?: ReactNode; buttonLabel?: ReactNode;
buttonVariant?: 'primary' | 'quiet' | 'danger'; buttonVariant?: 'primary' | 'quiet' | 'danger';
isLoading?: boolean; isLoading?: boolean;
@@ -25,7 +25,7 @@ export function ConfirmationForm({
return ( return (
<Form onSubmit={onConfirm} error={getErrorMessage(error)}> <Form onSubmit={onConfirm} error={getErrorMessage(error)}>
<Box marginY="4">{message}</Box> {message && <Box marginY="4">{message}</Box>}
<FormButtons> <FormButtons>
<Button onPress={onClose}>{t(labels.cancel)}</Button> <Button onPress={onClose}>{t(labels.cancel)}</Button>
<FormSubmitButton <FormSubmitButton
+1
View File
@@ -449,6 +449,7 @@ export const messages: Record<string, string> = {
confirmDelete: 'message.confirm-delete', confirmDelete: 'message.confirm-delete',
confirmRemove: 'message.confirm-remove', confirmRemove: 'message.confirm-remove',
confirmLeave: 'message.confirm-leave', confirmLeave: 'message.confirm-leave',
maxLength: 'message.max-length',
minPasswordLength: 'message.min-password-length', minPasswordLength: 'message.min-password-length',
noTeams: 'message.no-teams', noTeams: 'message.no-teams',
shareUrl: 'message.share-url', shareUrl: 'message.share-url',
+5
View File
@@ -1,5 +1,10 @@
import { endOfDay, endOfHour, startOfDay, startOfHour } from 'date-fns'; import { endOfDay, endOfHour, startOfDay, startOfHour } from 'date-fns';
export interface AnnotationRange {
startAt: number;
endAt: number;
}
/** /**
* Returns the `date` URL parameter value that filters the website view * Returns the `date` URL parameter value that filters the website view
* to the day (all-day annotation) or hour (timed annotation) of an annotation. * to the day (all-day annotation) or hour (timed annotation) of an annotation.
+9 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { renderNumberLabels } from './charts'; import { getChartBucketIndex, renderNumberLabels } from './charts';
// test for renderNumberLabels // test for renderNumberLabels
@@ -38,3 +38,11 @@ describe('renderNumberLabels', () => {
expect(renderNumberLabels(input)).toBe(expected); expect(renderNumberLabels(input)).toBe(expected);
}); });
}); });
describe('getChartBucketIndex', () => {
test('places an annotation in its monthly data bucket', () => {
const data = [{ x: '2026-08' }, { x: '2026-09' }];
expect(getChartBucketIndex(data, new Date('2026-08-27T07:00:00.000Z'))).toBe(0);
});
});
+15
View File
@@ -24,3 +24,18 @@ export function renderDateLabels(unit: string, locale: string) {
} }
}; };
} }
export function getChartBucketIndex(data: any[], date: Date) {
let index = -1;
for (let i = 0; i < data.length; i++) {
const timestamp = new Date(data[i]?.x ?? data[i]).getTime();
if (!Number.isFinite(timestamp)) continue;
if (timestamp > date.getTime()) break;
index = i;
}
return index;
}