-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
graph.tsx
268 lines (229 loc) · 7.66 KB
/
graph.tsx
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*global document window*/
import type { CssVariables } from '@gitkraken/gitkraken-components';
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import type { GitGraphRowType } from 'src/git/models/graph';
import type { GraphColumnConfig } from '../../../../config';
import type {
CommitListCallback,
DismissBannerParams,
GraphRepository,
State,
} from '../../../../plus/webviews/graph/protocol';
import {
DidChangeCommitsNotificationType,
DidChangeGraphConfigurationNotificationType,
DidChangeNotificationType,
DidChangeSelectionNotificationType,
DidChangeSubscriptionNotificationType,
DismissBannerCommandType,
GetMoreCommitsCommandType,
UpdateColumnCommandType,
UpdateSelectedRepositoryCommandType as UpdateRepositorySelectionCommandType,
UpdateSelectionCommandType,
} from '../../../../plus/webviews/graph/protocol';
import { debounce } from '../../../../system/function';
import type { IpcMessage } from '../../../../webviews/protocol';
import { onIpc } from '../../../../webviews/protocol';
import { App } from '../../shared/appBase';
import { mix, opacity } from '../../shared/colors';
import { GraphWrapper } from './GraphWrapper';
import './graph.scss';
const graphLaneThemeColors = new Map([
['--vscode-gitlens-graphLane1Color', '#15a0bf'],
['--vscode-gitlens-graphLane2Color', '#0669f7'],
['--vscode-gitlens-graphLane3Color', '#8e00c2'],
['--vscode-gitlens-graphLane4Color', '#c517b6'],
['--vscode-gitlens-graphLane5Color', '#d90171'],
['--vscode-gitlens-graphLane6Color', '#cd0101'],
['--vscode-gitlens-graphLane7Color', '#f25d2e'],
['--vscode-gitlens-graphLane8Color', '#f2ca33'],
['--vscode-gitlens-graphLane9Color', '#7bd938'],
['--vscode-gitlens-graphLane10Color', '#2ece9d'],
]);
export class GraphApp extends App<State> {
private callback?: CommitListCallback;
constructor() {
super('GraphApp');
}
protected override onBind() {
const disposables = super.onBind?.() ?? [];
this.log('GraphApp.onBind paging:', this.state.paging);
const $root = document.getElementById('root');
if ($root != null) {
render(
<GraphWrapper
subscriber={(callback: CommitListCallback) => this.registerEvents(callback)}
onColumnChange={debounce(
(name: string, settings: GraphColumnConfig) => this.onColumnChanged(name, settings),
250,
)}
onSelectRepository={debounce(
(path: GraphRepository) => this.onRepositorySelectionChanged(path),
250,
)}
onMoreCommits={(...params) => this.onGetMoreCommits(...params)}
onSelectionChange={debounce(
(selection: { id: string; type: GitGraphRowType }[]) => this.onSelectionChanged(selection),
250,
)}
onDismissBanner={key => this.onDismissBanner(key)}
{...this.state}
/>,
$root,
);
disposables.push({
dispose: () => unmountComponentAtNode($root),
});
}
return disposables;
}
protected override onMessageReceived(e: MessageEvent) {
const msg = e.data as IpcMessage;
this.log(`${this.appName}.onMessageReceived(${msg.id}): name=${msg.method}`);
switch (msg.method) {
case DidChangeNotificationType.method:
onIpc(DidChangeNotificationType, msg, params => {
this.setState({ ...this.state, ...params.state });
this.refresh(this.state);
});
break;
case DidChangeCommitsNotificationType.method:
onIpc(DidChangeCommitsNotificationType, msg, params => {
let rows;
if (params?.paging?.startingCursor != null && this.state.rows != null) {
const previousRows = this.state.rows;
const lastSha = previousRows[previousRows.length - 1]?.sha;
let previousRowsLength = previousRows.length;
const newRowsLength = params.rows.length;
rows = [];
// Preallocate the array to avoid reallocations
rows.length = previousRowsLength + newRowsLength;
if (params.paging.startingCursor !== lastSha) {
let i = 0;
let row;
for (row of previousRows) {
rows[i++] = row;
if (row.sha === params.paging.startingCursor) {
previousRowsLength = i;
if (previousRowsLength !== previousRows.length) {
// If we stopped before the end of the array, we need to trim it
rows.length = previousRowsLength + newRowsLength;
}
break;
}
}
} else {
for (let i = 0; i < previousRowsLength; i++) {
rows[i] = previousRows[i];
}
}
for (let i = 0; i < newRowsLength; i++) {
rows[previousRowsLength + i] = params.rows[i];
}
} else {
rows = params.rows;
}
this.setState({
...this.state,
rows: rows,
paging: params.paging,
});
this.refresh(this.state);
});
break;
case DidChangeSelectionNotificationType.method:
onIpc(DidChangeSelectionNotificationType, msg, params => {
this.setState({ ...this.state, selectedSha: params.selection[0] });
this.refresh(this.state);
});
break;
case DidChangeGraphConfigurationNotificationType.method:
onIpc(DidChangeGraphConfigurationNotificationType, msg, params => {
this.setState({
...this.state,
config: params.config,
});
this.refresh(this.state);
});
break;
case DidChangeSubscriptionNotificationType.method:
onIpc(DidChangeSubscriptionNotificationType, msg, params => {
this.setState({
...this.state,
subscription: params.subscription,
allowed: params.allowed,
});
this.refresh(this.state);
});
break;
default:
super.onMessageReceived?.(e);
}
}
protected override onThemeUpdated() {
this.setState({ ...this.state, mixedColumnColors: undefined });
this.refresh(this.state);
}
protected override setState(state: State) {
if (state.mixedColumnColors == null) {
state.mixedColumnColors = this.getGraphColors();
}
super.setState(state);
}
private getGraphColors(): CssVariables {
// this will be called on theme updated as well as on config updated since it is dependent on the column colors from config changes and the background color from the theme
const computedStyle = window.getComputedStyle(document.body);
const bgColor = computedStyle.getPropertyValue('--color-background');
const mixedGraphColors: CssVariables = {};
let i = 0;
let color;
for (const [colorVar, colorDefault] of graphLaneThemeColors) {
color = computedStyle.getPropertyValue(colorVar) || colorDefault;
mixedGraphColors[`--column-${i}-color`] = color;
mixedGraphColors[`--graph-color-${i}`] = color;
for (const mixInt of [15, 25, 45, 50]) {
mixedGraphColors[`--graph-color-${i}-bg${mixInt}`] = mix(bgColor, color, mixInt);
}
for (const mixInt of [10, 50]) {
mixedGraphColors[`--graph-color-${i}-f${mixInt}`] = opacity(color, mixInt);
}
i++;
}
return mixedGraphColors;
}
private onDismissBanner(key: DismissBannerParams['key']) {
this.sendCommand(DismissBannerCommandType, { key: key });
}
private onColumnChanged(name: string, settings: GraphColumnConfig) {
this.sendCommand(UpdateColumnCommandType, {
name: name,
config: settings,
});
}
private onRepositorySelectionChanged(repo: GraphRepository) {
this.sendCommand(UpdateRepositorySelectionCommandType, {
path: repo.path,
});
}
private onGetMoreCommits(limit?: number) {
this.sendCommand(GetMoreCommitsCommandType, {
limit: limit,
});
}
private onSelectionChanged(selection: { id: string; type: GitGraphRowType }[]) {
this.sendCommand(UpdateSelectionCommandType, {
selection: selection,
});
}
private registerEvents(callback: CommitListCallback): () => void {
this.callback = callback;
return () => {
this.callback = undefined;
};
}
private refresh(state: State) {
this.callback?.(state);
}
}
new GraphApp();