-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ggshield-view-provider): switch to WebviewView for customization (#2
- Loading branch information
1 parent
86d7d59
commit 512d6b6
Showing
7 changed files
with
181 additions
and
77 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
body { | ||
height: 100vh; | ||
} | ||
|
||
body.vscode-light { | ||
color: black; | ||
} | ||
|
||
body.vscode-dark { | ||
color: white; | ||
} | ||
|
||
p { | ||
font-size: 16px; | ||
} | ||
|
||
.anonymous { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
button.large { | ||
background-color: #2200fb; | ||
color: white; | ||
border: none; | ||
padding: 10px 20px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
border-radius: 5px; | ||
margin: 2em 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import * as vscode from "vscode"; | ||
import { GGShieldConfiguration } from "../lib/ggshield-configuration"; | ||
import { ggshieldAuthStatus } from "../lib/ggshield-api"; | ||
|
||
const documentationUri = vscode.Uri.parse( | ||
"https://docs.gitguardian.com/ggshield-docs/getting-started" | ||
); | ||
const feedbackFormUri = vscode.Uri.parse( | ||
"https://docs.google.com/forms/d/e/1FAIpQLSc_BemGrdQfxp6lg7KgeDoB32XZg8yMfapk2gbemu0mVfskDQ/viewform" | ||
); | ||
|
||
export class GitGuardianWebviewProvider implements vscode.WebviewViewProvider { | ||
public static readonly viewType = "gitguardian.gitguardianView"; | ||
private _view?: vscode.WebviewView; | ||
private isAuthenticated: boolean = false; | ||
|
||
constructor( | ||
private ggshieldConfiguration: GGShieldConfiguration, | ||
private readonly _extensionUri: vscode.Uri | ||
) { | ||
this.checkAuthenticationStatus(); | ||
} | ||
|
||
public async resolveWebviewView( | ||
webviewView: vscode.WebviewView, | ||
context: vscode.WebviewViewResolveContext, | ||
_token: vscode.CancellationToken | ||
) { | ||
this._view = webviewView; | ||
|
||
webviewView.webview.options = { | ||
enableScripts: true, | ||
localResourceRoots: [vscode.Uri.joinPath(this._extensionUri, "media"), vscode.Uri.joinPath(this._extensionUri, "images")], | ||
}; | ||
|
||
this.updateWebViewContent(webviewView); | ||
|
||
webviewView.webview.onDidReceiveMessage(async (data) => { | ||
if (data.type === "authenticate") { | ||
vscode.commands.executeCommand("gitguardian.authenticate"); | ||
} | ||
}); | ||
} | ||
|
||
private async checkAuthenticationStatus() { | ||
this.isAuthenticated = await ggshieldAuthStatus(this.ggshieldConfiguration); | ||
this.updateWebViewContent(this._view); | ||
} | ||
|
||
private updateWebViewContent(webviewView?: vscode.WebviewView) { | ||
if (webviewView) { | ||
webviewView.webview.html = this.getHtmlForWebview(webviewView.webview); | ||
} | ||
} | ||
|
||
private getHtmlForWebview(webview: vscode.Webview): string { | ||
const styleUri = webview.asWebviewUri( | ||
vscode.Uri.joinPath(this._extensionUri, "media", "main.css") | ||
); | ||
const logoUri = webview.asWebviewUri( | ||
vscode.Uri.joinPath(this._extensionUri, "images", "gitguardian-icon-primary700-background.svg") | ||
); | ||
|
||
if (this.isAuthenticated) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href="${styleUri}" rel="stylesheet"> | ||
<title>GitGuardian - Authenticated</title> | ||
</head> | ||
<body> | ||
<h2>How it works</h2> | ||
<p>Each time you save a document, it will undergo automatic scanning, and any detected secrets will be highlighted as errors.</p> | ||
<p><a href="${documentationUri}" target="_blank">Open documentation</a></p> | ||
<h2>Feedback</h2> | ||
<p>This extension is in beta.</p> | ||
<p>Please share any issues or suggestions <a href="${feedbackFormUri}" target="_blank"> using the following feedback form</a>.</p> | ||
</body> | ||
</html>`; | ||
} else { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href="${styleUri}" rel="stylesheet"> | ||
<title>GitGuardian - Welcome</title> | ||
</head> | ||
<body> | ||
<div class="anonymous"> | ||
<img src="${logoUri}" alt="GitGuardian Logo" height="100px"; /> | ||
<h1 style="margin-bottom:0px;">Welcome to GitGuardian</h1> | ||
<p>Protect your code from secrets leakage</p> | ||
<button class="button large" id="authenticate">Link your IDE to your account</button> | ||
</div> | ||
<script> | ||
const vscode = acquireVsCodeApi(); | ||
document.getElementById('authenticate').addEventListener('click', () => { | ||
vscode.postMessage({ type: 'authenticate' }); | ||
}); | ||
</script> | ||
</body> | ||
</html>`; | ||
} | ||
} | ||
|
||
public refresh() { | ||
this.checkAuthenticationStatus(); | ||
} | ||
|
||
dispose(): void { | ||
if (this._view) { | ||
this._view.webview.onDidReceiveMessage(() => { }); | ||
this._view.webview.html = ""; | ||
this._view = undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters