Skip to content

Commit 1eca569

Browse files
committed
Adds new context to home
1 parent d9daf6b commit 1eca569

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/webviews/apps/home/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createContext } from '@lit/context';
2+
import type { State } from '../../home/protocol';
3+
4+
export const stateContext = createContext<State>('state');

src/webviews/apps/home/home.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
data-placement="#{placement}"
2222
data-vscode-context='{ "webview": "#{webviewId}", "webviewInstance": "#{webviewInstanceId}" }'
2323
>
24+
<gl-home-app name="HomeView" placement="#{placement}" state="#{state}"></gl-home-app>
2425
<header class="home__header" id="header" hidden>
2526
<div id="no-repo-alert" class="alert alert--info mb-0" aria-hidden="true" hidden>
2627
<h1 class="alert__title">No repository detected</h1>

src/webviews/apps/home/home.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*global*/
22
import './home.scss';
3+
import { html } from 'lit';
34
import type { Disposable } from 'vscode';
45
import { getApplicablePromo } from '../../../plus/gk/account/promos';
56
import type { State } from '../../home/protocol';
@@ -13,17 +14,30 @@ import {
1314
import type { IpcMessage } from '../../protocol';
1415
import { ExecuteCommand } from '../../protocol';
1516
import type { AccountContent } from '../plus/account/components/account-content';
17+
import { GlApp } from '../shared/app';
1618
import { App } from '../shared/appBase';
1719
import type { GlFeatureBadge } from '../shared/components/feature-badge';
1820
import type { GlPromo } from '../shared/components/promo';
1921
import { DOM } from '../shared/dom';
22+
import type { HostIpc } from '../shared/ipc';
23+
import { HomeStateProvider } from './stateProvider';
2024
import '../shared/components/button';
2125
import '../shared/components/code-icon';
2226
import '../shared/components/feature-badge';
2327
import '../shared/components/overlays/tooltip';
2428
import '../shared/components/promo';
2529
import '../plus/account/components/account-content';
2630

31+
export class GlHomeApp extends GlApp<State> {
32+
protected override createStateProvider(state: State, ipc: HostIpc) {
33+
return new HomeStateProvider(this, state, ipc);
34+
}
35+
36+
override render() {
37+
return html`<account-content id="account-content"></account-content>`;
38+
}
39+
}
40+
2741
export class HomeApp extends App<State> {
2842
constructor() {
2943
super('HomeApp');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ContextProvider } from '@lit/context';
2+
import type { ReactiveControllerHost } from 'lit';
3+
import type { State } from '../../home/protocol';
4+
import { DidChangeSubscription } from '../../home/protocol';
5+
import type { Disposable } from '../shared/events';
6+
import type { HostIpc } from '../shared/ipc';
7+
import { stateContext } from './context';
8+
9+
type ReactiveElementHost = Partial<ReactiveControllerHost> & HTMLElement;
10+
11+
export class HomeStateProvider implements Disposable {
12+
private readonly disposable: Disposable;
13+
private readonly provider: ContextProvider<{ __context__: State }, ReactiveElementHost>;
14+
private readonly state: State;
15+
16+
constructor(
17+
host: ReactiveElementHost,
18+
state: State,
19+
private readonly _ipc: HostIpc,
20+
) {
21+
this.state = state;
22+
this.provider = new ContextProvider(host, { context: stateContext, initialValue: state });
23+
24+
this.disposable = this._ipc.onReceiveMessage(msg => {
25+
switch (true) {
26+
case DidChangeSubscription.is(msg):
27+
this.state.subscription = msg.params.subscription;
28+
this.state.avatar = msg.params.avatar;
29+
this.state.organizationsCount = msg.params.organizationsCount;
30+
this.state.timestamp = Date.now();
31+
32+
this.provider.setValue(this.state, true);
33+
break;
34+
}
35+
});
36+
}
37+
38+
dispose() {
39+
this.disposable.dispose();
40+
}
41+
}

0 commit comments

Comments
 (0)