This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHistoryPanel.svelte
212 lines (183 loc) · 6.83 KB
/
HistoryPanel.svelte
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script lang="ts" context="module">
type HistoryStore = InfinityQueryStore<HistoryPanel_HistoryConnection['nodes'], { afterCursor: string | null }>
export interface Capture {
history: ReturnType<HistoryStore['capture']>
scroller?: ScrollerCapture
}
</script>
<script lang="ts">
import { page } from '$app/stores'
import Avatar from '$lib/Avatar.svelte'
import { SourcegraphURL } from '$lib/common'
import { scrollIntoViewOnMount } from '$lib/dom'
import type { InfinityQueryStore } from '$lib/graphql'
import Icon from '$lib/Icon.svelte'
import LoadingSpinner from '$lib/LoadingSpinner.svelte'
import Scroller, { type Capture as ScrollerCapture } from '$lib/Scroller.svelte'
import { replaceRevisionInURL } from '$lib/shared'
import Timestamp from '$lib/Timestamp.svelte'
import Tooltip from '$lib/Tooltip.svelte'
import { Alert, Badge } from '$lib/wildcard'
import type { HistoryPanel_HistoryConnection } from './HistoryPanel.gql'
export let history: HistoryStore
export let enableInlineDiff: boolean = false
export let enableViewAtCommit: boolean = false
export function capture(): Capture {
return {
history: history.capture(),
scroller: scroller?.capture(),
}
}
export async function restore(data: Capture) {
await history.restore(data.history)
// If the selected revision is not in the set of currently loaded commits, load more
if (selectedRev) {
await history.fetchWhile(data => !data.find(commit => selectedRev?.startsWith(commit.abbreviatedOID)))
}
if (data.scroller) {
// restore might be called when the history panel is closed
// in which case scroller doesn't exist
scroller?.restore(data.scroller)
}
}
let scroller: Scroller
$: selectedRev = $page.url?.searchParams.get('rev')
$: diffEnabled = $page.url?.searchParams.has('diff')
$: closeURL = SourcegraphURL.from($page.url).deleteSearchParameter('rev', 'diff').toString()
</script>
<Scroller bind:this={scroller} margin={200} on:more={history.fetchMore}>
{#if $history.data}
<table>
<tbody>
{#each $history.data as commit (commit.id)}
{@const selected = commit.abbreviatedOID === selectedRev || commit.oid === selectedRev}
{@const isPerforceDepot = commit.perforceChangelist !== null}
{@const revURL = isPerforceDepot ? commit.perforceChangelist?.canonicalURL : commit.canonicalURL}
{@const revID = isPerforceDepot ? commit.perforceChangelist?.cid : commit.abbreviatedOID}
<tr class:selected use:scrollIntoViewOnMount={selected}>
<td class="revision">
<Badge variant="link"><a href={revURL}>{revID}</a></Badge>
</td>
<td class="subject">
{#if enableInlineDiff}
<a href={selected ? closeURL : `?rev=${revID}&diff=1`}>{commit.subject}</a>
{:else}
{commit.subject}
{/if}
</td>
<td class="author">
<Avatar avatar={commit.author.person} />
{commit.author.person.displayName}
</td>
<td class="timestamp"><Timestamp date={new Date(commit.author.date)} strict /></td>
<td class="actions">
{#if enableViewAtCommit && !isPerforceDepot}
<Tooltip tooltip={selected && !diffEnabled ? 'Close commit' : 'View at commit'}>
<a href={selected && !diffEnabled ? closeURL : `?rev=${commit.oid}`}
><Icon icon={ILucideFileText} inline aria-hidden /></a
>
</Tooltip>
{/if}
<Tooltip tooltip="Browse files at commit">
<a
href={replaceRevisionInURL(
SourcegraphURL.from($page.url).deleteSearchParameter('rev', 'diff').toString(),
isPerforceDepot ? `changelist/${revID}` : revID || ''
)}><Icon icon={ILucideFolderGit} inline aria-hidden /></a
>
</Tooltip>
</td>
</tr>
{/each}
</tbody>
</table>
{/if}
{#if $history.fetching}
<div class="info">
<LoadingSpinner />
</div>
{:else if $history.error}
<div class="info">
<Alert variant="danger">Unable to load history: {$history.error.message}</Alert>
</div>
{/if}
</Scroller>
<style lang="scss">
.info {
padding: 0.5rem 1rem;
}
table {
width: 100%;
max-width: 100%;
display: grid;
grid-template-columns: [revision] auto [subject] 3fr [author] 1fr [timestamp] auto [actions] auto;
}
tbody,
tr {
display: grid;
grid-column: 1/-1;
grid-template-columns: subgrid;
}
td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0.5rem;
:global([data-avatar]) {
vertical-align: middle;
}
}
.timestamp {
grid-area: timestamp;
text-align: right;
color: var(--text-muted);
}
.revision {
grid-area: revision;
}
.subject {
grid-area: subject;
}
.author {
grid-area: author;
}
.actions {
display: flex;
gap: 1rem;
padding: 0.5rem 1rem;
grid-area: actions;
}
tr {
border-bottom: 1px solid var(--border-color);
font-size: var(--font-size-small);
&.selected {
--icon-color: currentColor;
color: var(--light-text);
background-color: var(--primary);
a,
.timestamp {
color: inherit;
}
}
}
@media (--mobile) {
table {
grid-template-columns: 1fr auto auto auto;
}
tr {
grid-template-areas: 'subject subject subject revision' 'author author timestamp actions';
}
.subject {
white-space: normal;
}
.author,
.timestamp,
.actions {
align-self: center;
}
.actions a {
display: inline-block;
padding: 0.5rem;
}
}
</style>