-
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.
Merge pull request #16 from GitGuardian/garancegourdel/scrt-4590-disp…
…lay-custom-remediation-infos Add sidebar to display custom remediation message
- Loading branch information
Showing
5 changed files
with
242 additions
and
311 deletions.
There are no files selected for viewing
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
114 changes: 114 additions & 0 deletions
114
src/ggshield-webview/gitguardian-remediation-message-view.ts
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,114 @@ | ||
import { getRemediationMessage, ggshieldAuthStatus } from "../lib/ggshield-api"; | ||
import { GGShieldConfiguration } from "../lib/ggshield-configuration"; | ||
import * as vscode from "vscode"; | ||
|
||
export class GitGuardianRemediationMessageWebviewProvider | ||
implements vscode.WebviewViewProvider | ||
{ | ||
public static readonly viewType = "gitguardian.gitguardianRemediationMessageView"; | ||
private _view?: vscode.WebviewView; | ||
private isAuthenticated: boolean = false; | ||
private remediationMessage: string = ""; | ||
private isLoading: boolean = false; | ||
|
||
constructor( | ||
private ggshieldConfiguration: GGShieldConfiguration, | ||
private readonly _extensionUri: vscode.Uri | ||
) { | ||
this.checkAuthenticationStatus(); | ||
this.updateRemediationMessage(); | ||
} | ||
|
||
public async resolveWebviewView( | ||
webviewView: vscode.WebviewView, | ||
context: vscode.WebviewViewResolveContext, | ||
_token: vscode.CancellationToken | ||
) { | ||
this._view = webviewView; | ||
this.refresh(); | ||
|
||
webviewView.onDidChangeVisibility(() => { | ||
if (webviewView.visible) { | ||
// Refresh when the view becomes visible (e.g., after being collapsed and reopened) | ||
this.refresh(); | ||
} | ||
}); | ||
} | ||
|
||
private async checkAuthenticationStatus() { | ||
this.isAuthenticated = ggshieldAuthStatus(this.ggshieldConfiguration); | ||
} | ||
|
||
private async updateRemediationMessage() { | ||
if (this.isAuthenticated) { | ||
this.remediationMessage = await getRemediationMessage(this.ggshieldConfiguration); | ||
} | ||
} | ||
|
||
private updateWebViewContent(webviewView?: vscode.WebviewView) { | ||
if (webviewView) { | ||
webviewView.webview.html = this.getHtmlForWebview(); | ||
} | ||
} | ||
|
||
private escapeHtml(unsafe: string):string { | ||
return unsafe | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/"/g, """) | ||
.replace(/'/g, "'"); | ||
} | ||
|
||
private getHtmlForWebview(): string { | ||
if (this.isLoading) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<p>Loading...</p> | ||
</body> | ||
</html>`; | ||
} | ||
|
||
if (this.isAuthenticated) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<pre style="white-space:pre-wrap"> | ||
${this.escapeHtml(this.remediationMessage)} | ||
</pre> | ||
</body> | ||
</html>`; | ||
} else { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<p>Please authenticate to see your personalized remediation message.</p> | ||
</body> | ||
</html>`; | ||
} | ||
} | ||
|
||
public async refresh() { | ||
this.isLoading = true; | ||
this.updateWebViewContent(this._view); | ||
|
||
await this.checkAuthenticationStatus(); | ||
console.log("Well authentificated"); | ||
await this.updateRemediationMessage(); | ||
|
||
this.isLoading = false; | ||
this.updateWebViewContent(this._view); | ||
} | ||
|
||
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
Oops, something went wrong.