-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathVersionHistoryTimeline.tsx
More file actions
152 lines (142 loc) · 5.12 KB
/
Copy pathVersionHistoryTimeline.tsx
File metadata and controls
152 lines (142 loc) · 5.12 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useEffect } from "react";
import { FilePen, Loader2 } from "lucide-react";
import { useInView } from "react-intersection-observer";
import { cn } from "@/lib/utils";
import DataTableNoData from "@/shared/DataTableNoData/DataTableNoData";
import TooltipWrapper from "@/shared/TooltipWrapper/TooltipWrapper";
import EnvironmentBadgeList from "@/shared/EnvironmentLabel/EnvironmentBadgeList";
import VersionTagList from "./VersionTagList";
import VersionMeta from "./VersionMeta";
export interface VersionHistoryItem {
id: string;
label: string;
tags: string[];
description?: string;
created_at: string;
created_by?: string;
environments?: string[] | null;
}
interface VersionHistoryTimelineProps {
items: VersionHistoryItem[];
selectedId?: string;
onSelect: (item: VersionHistoryItem) => void;
hasNextPage?: boolean;
isFetchingNextPage?: boolean;
// Broader than isFetchingNextPage — also true during a background refetch
// (e.g. the 30s poll, or a mutation's invalidation of already-loaded
// pages). Used only to gate the auto-load trigger, not the spinner: firing
// onLoadMore while an unrelated fetch is in flight races it and can
// produce a duplicate/overlapping row once both resolve.
isFetching?: boolean;
// True once a page fetch has failed. hasNextPage still reflects the last
// *successful* page, so without this the sentinel retries a permanently
// failing request forever, the moment isFetching settles back to false.
hasError?: boolean;
onLoadMore?: () => void;
emptyTitle?: string;
}
const VersionHistoryTimeline: React.FC<VersionHistoryTimelineProps> = ({
items,
selectedId,
onSelect,
hasNextPage = false,
isFetchingNextPage = false,
isFetching = false,
hasError = false,
onLoadMore,
emptyTitle = "No version history",
}) => {
const { ref: sentinelRef, inView } = useInView();
useEffect(() => {
if (inView && hasNextPage && !isFetching && !hasError && onLoadMore) {
onLoadMore();
}
}, [inView, hasNextPage, isFetching, hasError, onLoadMore]);
if (items.length === 0) {
return <DataTableNoData title={emptyTitle} />;
}
return (
<ul className="px-4 pb-4 pt-1" data-testid="version-history-timeline">
{items.map((item, index) => {
const isSelected = item.id === selectedId;
const isLast = index === items.length - 1;
return (
<li key={item.id} className="flex gap-2">
<div className="flex flex-col items-center">
<div
className={cn(
"h-3 w-0.5 shrink-0",
index > 0
? "bg-[var(--timeline-connector)] opacity-50"
: "bg-transparent",
)}
/>
<div
className={cn(
"size-2 shrink-0 rounded-full",
isSelected
? "bg-primary"
: "border-2 border-[var(--timeline-connector)]",
)}
/>
<div
className={cn(
"w-0.5 flex-1 shrink-0",
!isLast
? "bg-[var(--timeline-connector)] opacity-50"
: "bg-transparent",
)}
/>
</div>
<div
data-testid={`version-history-item-${item.label}`}
className={cn(
"min-w-0 flex-1 cursor-pointer rounded px-3 py-2 transition-colors",
isSelected
? "relative z-10 bg-primary-foreground ring-1 ring-primary"
: "hover:bg-primary-foreground/60",
)}
onClick={() => onSelect(item)}
>
<div className="flex min-w-0 items-center gap-1">
<span className="comet-body-s-accented shrink-0">
{item.label}
</span>
<VersionTagList tags={item.tags} size="sm" maxWidth={200} />
<EnvironmentBadgeList
names={item.environments}
size="pill"
badgeClassName="max-w-[120px]"
withOverflow
maxWidth={160}
/>
</div>
{item.description && (
<p className="comet-body-xs mt-1.5 flex min-w-0 items-center gap-1 text-light-slate">
<FilePen className="size-3 shrink-0" />
<TooltipWrapper content={item.description}>
<span className="w-fit max-w-full truncate">
{item.description}
</span>
</TooltipWrapper>
</p>
)}
<VersionMeta
className="mt-1.5"
createdAt={item.created_at}
createdBy={item.created_by}
/>
</div>
</li>
);
})}
<li ref={sentinelRef} className="h-1" />
{isFetchingNextPage && (
<li className="flex justify-center py-2">
<Loader2 className="size-4 animate-spin text-light-slate" />
</li>
)}
</ul>
);
};
export default VersionHistoryTimeline;