/
homeWebviewView.ts
212 lines (177 loc) · 6.98 KB
/
homeWebviewView.ts
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
import type { Disposable } from 'vscode';
import { window } from 'vscode';
import { getAvatarUriFromGravatarEmail } from '../../avatars';
import { configuration } from '../../configuration';
import { ContextKeys, CoreCommands } from '../../constants';
import type { Container } from '../../container';
import { getContext, onDidChangeContext } from '../../context';
import type { RepositoriesVisibility } from '../../git/gitProviderService';
import type { SubscriptionChangeEvent } from '../../plus/subscription/subscriptionService';
import { ensurePlusFeaturesEnabled } from '../../plus/subscription/utils';
import type { Subscription } from '../../subscription';
import { executeCoreCommand, registerCommand } from '../../system/command';
import type { IpcMessage } from '../protocol';
import { onIpc } from '../protocol';
import { WebviewViewBase } from '../webviewViewBase';
import type { CompleteStepParams, DismissSectionParams, State } from './protocol';
import {
CompletedActions,
CompleteStepCommandType,
DidChangeExtensionEnabledType,
DidChangeSubscriptionNotificationType,
DismissSectionCommandType,
} from './protocol';
export class HomeWebviewView extends WebviewViewBase<State> {
constructor(container: Container) {
super(container, 'gitlens.views.home', 'home.html', 'Home', 'homeView');
this.disposables.push(
this.container.subscription.onDidChange(this.onSubscriptionChanged, this),
onDidChangeContext(key => {
if (key !== ContextKeys.Disabled) return;
this.notifyExtensionEnabled();
}),
);
}
override async show(options?: { preserveFocus?: boolean | undefined }): Promise<void> {
if (!(await ensurePlusFeaturesEnabled())) return;
return super.show(options);
}
private onSubscriptionChanged(e: SubscriptionChangeEvent) {
void this.notifyDidChangeData(e.current);
}
protected override onVisibilityChanged(visible: boolean): void {
if (!visible) return;
queueMicrotask(() => void this.validateSubscription());
}
protected override onWindowFocusChanged(focused: boolean): void {
if (!focused) return;
queueMicrotask(() => void this.validateSubscription());
}
protected override registerCommands(): Disposable[] {
return [
registerCommand(`${this.id}.refresh`, () => this.refresh(), this),
registerCommand('gitlens.home.toggleWelcome', async () => {
const welcomeVisible = !this.welcomeVisible;
await this.container.storage.store('views:welcome:visible', welcomeVisible);
if (welcomeVisible) {
await this.container.storage.store('home:actions:completed', []);
await this.container.storage.store('home:steps:completed', []);
await this.container.storage.store('home:sections:dismissed', []);
}
void this.refresh();
}),
registerCommand('gitlens.home.restoreWelcome', async () => {
await this.container.storage.store('home:steps:completed', []);
await this.container.storage.store('home:sections:dismissed', []);
void this.refresh();
}),
registerCommand('gitlens.home.showSCM', async () => {
const completedActions = this.container.storage.get('home:actions:completed', []);
if (!completedActions.includes(CompletedActions.OpenedSCM)) {
completedActions.push(CompletedActions.OpenedSCM);
await this.container.storage.store('home:actions:completed', completedActions);
void this.notifyDidChangeData();
}
await executeCoreCommand(CoreCommands.ShowSCM);
}),
];
}
protected override onMessageReceived(e: IpcMessage) {
switch (e.method) {
case CompleteStepCommandType.method:
onIpc(CompleteStepCommandType, e, params => this.completeStep(params));
break;
case DismissSectionCommandType.method:
onIpc(DismissSectionCommandType, e, params => this.dismissSection(params));
break;
}
}
private completeStep({ id, completed = false }: CompleteStepParams) {
const steps = this.container.storage.get('home:steps:completed', []);
const hasStep = steps.includes(id);
if (!hasStep && completed) {
steps.push(id);
} else if (hasStep && !completed) {
steps.splice(steps.indexOf(id), 1);
}
void this.container.storage.store('home:steps:completed', steps);
}
private dismissSection(params: DismissSectionParams) {
const sections = this.container.storage.get('home:sections:dismissed', []);
if (!sections.includes(params.id)) {
sections.push(params.id);
}
void this.container.storage.store('home:sections:dismissed', sections);
}
protected override async includeBootstrap(): Promise<State> {
return this.getState();
}
private get welcomeVisible(): boolean {
return this.container.storage.get('views:welcome:visible', true);
}
private async getRepoVisibility(): Promise<RepositoriesVisibility> {
const visibility = await this.container.git.visibility();
return visibility;
}
private async getSubscription(subscription?: Subscription) {
// Make sure to make a copy of the array otherwise it will be live to the storage value
const completedActions = [...this.container.storage.get('home:actions:completed', [])];
if (!this.welcomeVisible) {
completedActions.push(CompletedActions.DismissedWelcome);
}
const subscriptionState = subscription ?? (await this.container.subscription.getSubscription(true));
let avatar;
if (subscriptionState.account?.email) {
avatar = getAvatarUriFromGravatarEmail(subscriptionState.account.email, 34).toString();
} else {
avatar = `${this.getWebRoot() ?? ''}/media/gitlens-logo.webp`;
}
return {
subscription: subscriptionState,
completedActions: completedActions,
avatar: avatar,
};
}
private async getState(subscription?: Subscription): Promise<State> {
const subscriptionState = await this.getSubscription(subscription);
const steps = this.container.storage.get('home:steps:completed', []);
const sections = this.container.storage.get('home:sections:dismissed', []);
return {
extensionEnabled: this.getExtensionEnabled(),
webroot: this.getWebRoot(),
subscription: subscriptionState.subscription,
completedActions: subscriptionState.completedActions,
plusEnabled: configuration.get('plusFeatures.enabled'),
visibility: await this.getRepoVisibility(),
completedSteps: steps,
dismissedSections: sections,
avatar: subscriptionState.avatar,
};
}
private notifyDidChangeData(subscription?: Subscription) {
if (!this.isReady) return false;
return window.withProgress({ location: { viewId: this.id } }, async () =>
this.notify(DidChangeSubscriptionNotificationType, await this.getSubscription(subscription)),
);
}
private getExtensionEnabled() {
return !getContext(ContextKeys.Disabled, false);
}
private notifyExtensionEnabled() {
if (!this.isReady) return;
void this.notify(DidChangeExtensionEnabledType, {
extensionEnabled: this.getExtensionEnabled(),
});
}
private _validating: Promise<void> | undefined;
private async validateSubscription(): Promise<void> {
if (this._validating == null) {
this._validating = this.container.subscription.validate();
try {
await this._validating;
} finally {
this._validating = undefined;
}
}
}
}