Skip to content

Commit 68a966b

Browse files
committed
feat: add extension configuration
Added 3 options to set preferred languages, select default language and time interval
1 parent 03149a1 commit 68a966b

File tree

4 files changed

+126
-19
lines changed

4 files changed

+126
-19
lines changed

README.md

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# VSC Github Trending
22

3+
[![Version](https://vsmarketplacebadge.apphb.com/version/4gray.vsc-github-trending.svg)](https://marketplace.visualstudio.com/items?itemName=4gray.vsc-github-trending)
4+
[![Installs](https://vsmarketplacebadge.apphb.com/installs-short/4gray.vsc-github-trending.svg)](https://marketplace.visualstudio.com/items?itemName=4gray.vsc-github-trending)
5+
[![Downloads](https://vsmarketplacebadge.apphb.com/downloads-short/4gray.vsc-github-trending.svg)](https://marketplace.visualstudio.com/items?itemName=4gray.vsc-github-trending)
6+
37
Explore Github Trending repositories directly from Visual Studio Code. Extension is based on React, [Material-UI](https://material-ui.com/), [Trending API](https://github.com/huchenme/github-trending-api) and VSCode Webview API.
48

59
<img src="https://raw.githubusercontent.com/4gray/vsc-github-trending/master/screenshot.png" title="VSC Github Trending" />
@@ -10,7 +14,6 @@ Just press `Ctrl+P` or `Cmd+P` and type:
1014

1115
`> GT: Open Github Trending`
1216

13-
1417
## Install
1518

1619
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
@@ -19,11 +22,41 @@ Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter
1922

2023
Extension is available on [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=4gray.vsc-github-trending#overview)
2124

25+
## Usage and configuration
26+
27+
Use `vsc-github-trending.languages` option to set list of preferred languages which should be displayed in the extension, e.g.:
28+
29+
```
30+
"vsc-github-trending.languages": [
31+
"c++"
32+
"css"
33+
"java",
34+
"javascript",
35+
"go",
36+
"python",
37+
"ruby",
38+
"rust",
39+
"typescript"
40+
],
41+
...
42+
```
43+
44+
_Note: See [list](https://github-trending-api.now.sh/languages) of all supported languages (use `urlParam` for configuration)_
45+
46+
Selected language and time interval could also be configured, e.g.:
47+
48+
```
49+
"vsc-github-trending.selectedInterval": "daily",
50+
"vsc-github-trending.selectedLanguage": "javascript",
51+
...
52+
```
53+
2254
## Source Code
2355

2456
Source code of this extension is available on <a href="https://github.com/4gray/vsc-github-trending">Github</a>.
2557

2658
## Credits
27-
* [Github Trending API](https://github.com/huchenme/github-trending-api)
28-
* [VSCode React Starter](https://github.com/rebornix/vscode-webview-react)
29-
* Hot Icon by [Vaibhav Radhakrishnan](https://thenounproject.com/search/?q=hot&i=551479)
59+
60+
- [Github Trending API](https://github.com/huchenme/github-trending-api)
61+
- [VSCode React Starter](https://github.com/rebornix/vscode-webview-react)
62+
- Hot Icon by [Vaibhav Radhakrishnan](https://thenounproject.com/search/?q=hot&i=551479)

ext-src/extension.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import * as vscode from 'vscode';
44
export function activate(context: vscode.ExtensionContext) {
55
context.subscriptions.push(
66
vscode.commands.registerCommand('react-webview.start', () => {
7-
ReactPanel.createOrShow(context.extensionPath);
7+
const panel = ReactPanel.createOrShow(context.extensionPath);
8+
9+
// get configuration and send to web application
10+
const config = vscode.workspace.getConfiguration(
11+
'vsc-github-trending'
12+
);
13+
panel.sendConfig(config);
814
})
915
);
1016
}
@@ -39,6 +45,7 @@ class ReactPanel {
3945
column || vscode.ViewColumn.One
4046
);
4147
}
48+
return ReactPanel.currentPanel;
4249
}
4350

4451
private constructor(extensionPath: string, column: vscode.ViewColumn) {
@@ -64,7 +71,7 @@ class ReactPanel {
6471
this._panel.webview.html = this._getHtmlForWebview();
6572

6673
// Listen for when the panel is disposed
67-
// This happens when the user closes the panel or when the panel is closed programatically
74+
// This happens when the user closes the panel or when the panel is closed programmatically
6875
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
6976

7077
// Handle messages from the webview
@@ -81,6 +88,12 @@ class ReactPanel {
8188
);
8289
}
8390

91+
public sendConfig(config: any) {
92+
// Send a message to the webview webview.
93+
// You can send any JSON serializable data.
94+
this._panel.webview.postMessage({ command: 'configuration', config });
95+
}
96+
8497
public dispose() {
8598
ReactPanel.currentPanel = undefined;
8699

package.json

+42-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vsc-github-trending",
33
"displayName": "VSC Github Trending",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"description": "Explore trending Github repositories directly in VSCode",
66
"engines": {
77
"vscode": "^1.37.0"
@@ -20,7 +20,8 @@
2020
"theme": "dark"
2121
},
2222
"activationEvents": [
23-
"onCommand:react-webview.start"
23+
"onCommand:react-webview.start",
24+
"onCommand:react-webview.setConfig"
2425
],
2526
"main": "./build/ext-src/extension.js",
2627
"contributes": {
@@ -30,7 +31,45 @@
3031
"title": "Open Github Trending",
3132
"category": "GT"
3233
}
33-
]
34+
],
35+
"configuration": {
36+
"type": "object",
37+
"title": "VSC Github Trending Configuration",
38+
"properties": {
39+
"vsc-github-trending.languages": {
40+
"type": "array",
41+
"default": [
42+
"c++",
43+
"css",
44+
"go",
45+
"html",
46+
"javascript",
47+
"java",
48+
"php",
49+
"python",
50+
"ruby",
51+
"rust",
52+
"typescript"
53+
],
54+
"description": "Names of the preferred languages"
55+
},
56+
"vsc-github-trending.selectedLanguage": {
57+
"type": "string",
58+
"default": "go",
59+
"description": "Selected language"
60+
},
61+
"vsc-github-trending.selectedInterval": {
62+
"type": "string",
63+
"enum": [
64+
"daily",
65+
"weekly",
66+
"monthly"
67+
],
68+
"default": "monthly",
69+
"description": "Selected time interval (daily, weekly or monthly)"
70+
}
71+
}
72+
}
3473
},
3574
"categories": [
3675
"Other"

src/components/App.tsx

+32-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import './Header.css';
1010
import RepositoryContainer from './RepositoryContainer';
1111

1212
class App extends React.Component<any> {
13+
private BASE_API_URL = 'https://github-trending-api.now.sh';
14+
1315
public state = {
1416
intervals: ['daily', 'weekly', 'monthly'],
1517
languages: { all: [], popular: [] },
@@ -19,6 +21,8 @@ class App extends React.Component<any> {
1921
selectedLanguage: 'javascript',
2022
};
2123

24+
public languages: string[] = [];
25+
2226
public theme = createMuiTheme({
2327
palette: {
2428
type: 'dark',
@@ -29,29 +33,47 @@ class App extends React.Component<any> {
2933
});
3034

3135
public componentDidMount() {
32-
Promise.all([this.getRepositories(), this.getLanguages()]).then(
33-
result => {
34-
this.setState({ loading: false });
36+
// Handle messages sent from the extension to the webview
37+
window.addEventListener('message', async event => {
38+
this.setState({ loading: true });
39+
const message = event.data; // The json data that the extension sent
40+
switch (message.command) {
41+
case 'configuration':
42+
this.languages = message.config.languages;
43+
this.state.selectedLanguage =
44+
message.config.selectedLanguage;
45+
this.state.selectedInterval =
46+
message.config.selectedInterval;
47+
await this.getRepositories();
48+
await this.getLanguages();
49+
this.setState({ loading: false });
50+
break;
3551
}
36-
);
52+
});
3753
}
3854

3955
public getLanguages() {
40-
fetch('https://github-trending-api.now.sh/languages')
56+
return fetch(this.BASE_API_URL + '/languages')
4157
.then(response => response.json())
42-
.then(data =>
58+
.then(data => {
59+
// TODO: create model
60+
data = data.filter(
61+
(item: { name: string; urlParam: string }) =>
62+
this.languages.indexOf(item.urlParam) !== -1
63+
);
4364
this.setState({
4465
languages: {
4566
all: data,
4667
},
47-
})
48-
);
68+
});
69+
});
4970
}
5071

5172
public getRepositories() {
5273
this.setState({ loading: true });
53-
fetch(
54-
'https://github-trending-api.now.sh/repositories?language=' +
74+
return fetch(
75+
this.BASE_API_URL +
76+
'/repositories?language=' +
5577
this.state.selectedLanguage +
5678
'&since=' +
5779
this.state.selectedInterval

0 commit comments

Comments
 (0)