Repository navigation
Expand file tree
/
Copy pathtimestamp-tooltip.tsx
More file actions
137 lines (129 loc) · 4.82 KB
/
Copy pathtimestamp-tooltip.tsx
File metadata and controls
137 lines (129 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { usePage } from '@inertiajs/react';
import * as TooltipPrimitive from '@radix-ui/react-tooltip';
import { useRef, useState } from 'react';
import type { CSSProperties, ReactNode } from 'react';
import {
Tooltip,
TooltipTrigger,
useTooltipArmed,
} from '@/components/ui/tooltip';
import {
browserTimeZone,
formatZonedDate,
formatZonedTime,
laneColor,
timeZoneCity,
} from '@/lib/utils';
import type { User } from '@/types';
export function TimestampTooltip({
date,
author,
children,
}: {
date: string;
/**
* Whose local time to show next to the reader's, when it's a person's.
*/
author?: User;
children: ReactNode;
}) {
const [isOpen, setIsOpen] = useState(false);
const tap = useRef({ isTouch: false, wasOpen: false });
return (
<Tooltip open={isOpen} onOpenChange={setIsOpen}>
<TooltipTrigger
asChild
onPointerDown={(event) => {
// Before the tooltip's own handler, which closes it on any press of its trigger.
tap.current = {
isTouch: event.pointerType === 'touch',
wasOpen: isOpen,
};
}}
onClickCapture={(event) => {
const { isTouch, wasOpen } = tap.current;
tap.current = { isTouch: false, wasOpen: false };
// Touch can't hover, so the first tap shows the times instead of following the link or opening the
// commit around it. A second tap, with the times showing, goes through.
if (isTouch && !wasOpen) {
event.preventDefault();
event.stopPropagation();
setIsOpen(true);
}
}}
>
{children}
</TooltipTrigger>
<ZonedTimesTooltip date={date} author={author} />
</Tooltip>
);
}
/** The tooltip itself, once the surrounding one has mounted: its parts need it. */
function ZonedTimesTooltip({ date, author }: { date: string; author?: User }) {
if (!useTooltipArmed()) {
return null;
}
return (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
sideOffset={6}
className="z-50 animate-in rounded-lg border bg-popover text-popover-foreground shadow-md fade-in-0 zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95"
>
<ZonedTimes date={date} author={author} />
<TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%-1px)] rotate-45 rounded-[2px] border-r border-b bg-popover fill-popover" />
</TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
);
}
function ZonedTimes({ date, author }: { date: string; author?: User }) {
const { auth } = usePage().props;
const columns = [
{
who: 'You',
lane: auth.user?.lane,
timeZone: auth.user?.timezone ?? browserTimeZone(),
},
...(author !== undefined &&
author.timezone !== null &&
author.id !== auth.user?.id
? [
{
who: author.name.split(' ')[0],
lane: author.lane,
timeZone: author.timezone,
},
]
: []),
];
return (
<div
className="flex divide-x overflow-hidden rounded-[calc(var(--radius)-1px)]"
data-test="timestamp-tooltip"
>
{columns.map((column) => (
<div
key={column.who}
className="border-t-2 border-t-(--lane) px-2.5 py-1.5 whitespace-nowrap"
style={
{
'--lane':
column.lane === undefined
? 'var(--rail)'
: laneColor(column.lane),
} as CSSProperties
}
>
<p className="text-[11px] text-muted-foreground">
{column.who} · {timeZoneCity(column.timeZone)}
</p>
<p className="text-[11px] text-muted-foreground">
<span className="text-[13px] font-semibold text-popover-foreground tabular-nums">
{formatZonedTime(date, column.timeZone)}
</span>{' '}
{formatZonedDate(date, column.timeZone)}
</p>
</div>
))}
</div>
);
}