sdedic commented on code in PR #7607: URL: https://github.com/apache/netbeans/pull/7607#discussion_r1696639731
########## java/java.lsp.server/vscode/src/panels/SshGuidePanel.ts: ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from "vscode"; +import * as path from "path"; +import * as fs from "fs"; +import * as Handlebars from "handlebars"; +import { promisify } from "util"; +import { toggleGuideFor, shouldHideGuideFor, removeGuidePropertiesFromStateFor, NotificationActionType, sshConfigLocation, StatePropertyPrefix, dummyKeyPathLocation } from "./guidesUtil"; + +const readFile = promisify(fs.readFile); + +export class SshGuidePanel { + public static currentPanel: SshGuidePanel | undefined; + public static readonly viewType: string = "sshGuide"; + + private static readonly actionType: NotificationActionType = 'ssh-connection'; + private static readonly statePropertyPrefix: StatePropertyPrefix = 'sshGuide'; + + private static readonly webviewsFolder: string = "webviews"; + private static readonly scriptsFolder: string = "scripts"; + private static readonly stylesFolder: string = "styles"; + + private readonly panel: vscode.WebviewPanel; + private disposables: vscode.Disposable[] = []; + + public static createOrShow(context: vscode.ExtensionContext) { + if (SshGuidePanel.currentPanel) { + SshGuidePanel.currentPanel.panel.reveal(); + return; + } + + SshGuidePanel.currentPanel = new SshGuidePanel(context); + } + + private constructor(context: vscode.ExtensionContext) { + this.panel = vscode.window.createWebviewPanel( + SshGuidePanel.viewType, + "Oracle Cloud Assets", + { viewColumn: vscode.ViewColumn.One, preserveFocus: true }, + { + enableCommandUris: true, + enableScripts: true, + localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder))], + } + ); + const iconPath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, "images", "Apache_NetBeans_Logo.png")); + this.panel.iconPath = { + light: iconPath, + dark: iconPath, + }; + + this.setHtml(context); + + this.panel.onDidDispose( + () => { + this.dispose(); + removeGuidePropertiesFromStateFor(context, SshGuidePanel.statePropertyPrefix); + }, + null, + this.disposables + ); + + this.panel.onDidChangeViewState( + () => { + if (this.panel.visible) { + this.setHtml(context); + } + }, + null, + this.disposables + ); + + this.panel.webview.onDidReceiveMessage( + (message) => { + if (message.command === "showSSHGuide") { + const ocid: string = context.globalState.get(`${SshGuidePanel.statePropertyPrefix}-ocid`) || ""; + toggleGuideFor(SshGuidePanel.actionType, ocid); + } + }, + undefined, + this.disposables + ); + } + + private async setHtml(context: vscode.ExtensionContext) { + let templateFile = path.resolve(__dirname, "../../webviews/ssh-guide.handlebars"); + templateFile = await readFile(templateFile, "utf-8"); + const template = Handlebars.compile(templateFile); + const ocid: string | undefined = context.globalState.get<string>(`${SshGuidePanel.statePropertyPrefix}-ocid`); + + this.panel.webview.html = template({ + cspSource: this.panel.webview.cspSource, + publicIp: context.globalState.get<string>(`${SshGuidePanel.statePropertyPrefix}-publicIp`), + dummyKeyPathLocation, + sshConfigLocation, + showSSHGuide: shouldHideGuideFor(SshGuidePanel.actionType, ocid) ? "checked" : "", + cssUri: this.panel.webview.asWebviewUri(vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder, SshGuidePanel.stylesFolder, "guide.css"))), + javascriptUri: this.panel.webview.asWebviewUri( + vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder, SshGuidePanel.scriptsFolder, "ssh-guide.js")) + ), + }); + } + + public dispose() { + SshGuidePanel.currentPanel = undefined; + + this.panel.dispose(); + while (this.disposables.length) { Review Comment: nitpick: disposables.forEach; the disposable should not contain an undefined ... but if you want to be sure, you can use `x?.dispose()` and avoid if. ########## java/java.lsp.server/vscode/src/panels/SshGuidePanel.ts: ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from "vscode"; +import * as path from "path"; +import * as fs from "fs"; +import * as Handlebars from "handlebars"; +import { promisify } from "util"; +import { toggleGuideFor, shouldHideGuideFor, removeGuidePropertiesFromStateFor, NotificationActionType, sshConfigLocation, StatePropertyPrefix, dummyKeyPathLocation } from "./guidesUtil"; + +const readFile = promisify(fs.readFile); + +export class SshGuidePanel { + public static currentPanel: SshGuidePanel | undefined; + public static readonly viewType: string = "sshGuide"; + + private static readonly actionType: NotificationActionType = 'ssh-connection'; + private static readonly statePropertyPrefix: StatePropertyPrefix = 'sshGuide'; + + private static readonly webviewsFolder: string = "webviews"; + private static readonly scriptsFolder: string = "scripts"; + private static readonly stylesFolder: string = "styles"; + + private readonly panel: vscode.WebviewPanel; + private disposables: vscode.Disposable[] = []; + + public static createOrShow(context: vscode.ExtensionContext) { + if (SshGuidePanel.currentPanel) { + SshGuidePanel.currentPanel.panel.reveal(); + return; + } + + SshGuidePanel.currentPanel = new SshGuidePanel(context); + } + + private constructor(context: vscode.ExtensionContext) { + this.panel = vscode.window.createWebviewPanel( + SshGuidePanel.viewType, + "Oracle Cloud Assets", + { viewColumn: vscode.ViewColumn.One, preserveFocus: true }, + { + enableCommandUris: true, + enableScripts: true, + localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder))], + } + ); + const iconPath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, "images", "Apache_NetBeans_Logo.png")); + this.panel.iconPath = { + light: iconPath, + dark: iconPath, + }; + + this.setHtml(context); + + this.panel.onDidDispose( + () => { + this.dispose(); + removeGuidePropertiesFromStateFor(context, SshGuidePanel.statePropertyPrefix); + }, + null, + this.disposables + ); + + this.panel.onDidChangeViewState( + () => { + if (this.panel.visible) { + this.setHtml(context); + } + }, + null, + this.disposables + ); + + this.panel.webview.onDidReceiveMessage( + (message) => { + if (message.command === "showSSHGuide") { + const ocid: string = context.globalState.get(`${SshGuidePanel.statePropertyPrefix}-ocid`) || ""; + toggleGuideFor(SshGuidePanel.actionType, ocid); + } + }, + undefined, + this.disposables + ); + } + + private async setHtml(context: vscode.ExtensionContext) { + let templateFile = path.resolve(__dirname, "../../webviews/ssh-guide.handlebars"); + templateFile = await readFile(templateFile, "utf-8"); + const template = Handlebars.compile(templateFile); + const ocid: string | undefined = context.globalState.get<string>(`${SshGuidePanel.statePropertyPrefix}-ocid`); + + this.panel.webview.html = template({ + cspSource: this.panel.webview.cspSource, + publicIp: context.globalState.get<string>(`${SshGuidePanel.statePropertyPrefix}-publicIp`), Review Comment: I don't recommend to use `context.globalState` for this. The globalState is a (persistent) memento, while this information seenms transient. In addition, the caller currently sets a value into `globalState`, and then access **panel instance** (indirectly) through `SshGuidePanel.createOrShow` that either locates the current panel (an object instance) or creates one. So `createOrShow` could accept parameters, or Map or {} of parameters to pass down to the webview through the template context and store it in the `SshGuidePanel` instance then create the webview. ########## java/java.lsp.server/vscode/src/panels/SshGuidePanel.ts: ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from "vscode"; +import * as path from "path"; +import * as fs from "fs"; +import * as Handlebars from "handlebars"; +import { promisify } from "util"; +import { toggleGuideFor, shouldHideGuideFor, removeGuidePropertiesFromStateFor, NotificationActionType, sshConfigLocation, StatePropertyPrefix, dummyKeyPathLocation } from "./guidesUtil"; + +const readFile = promisify(fs.readFile); + +export class SshGuidePanel { + public static currentPanel: SshGuidePanel | undefined; + public static readonly viewType: string = "sshGuide"; + + private static readonly actionType: NotificationActionType = 'ssh-connection'; + private static readonly statePropertyPrefix: StatePropertyPrefix = 'sshGuide'; + + private static readonly webviewsFolder: string = "webviews"; + private static readonly scriptsFolder: string = "scripts"; + private static readonly stylesFolder: string = "styles"; + + private readonly panel: vscode.WebviewPanel; + private disposables: vscode.Disposable[] = []; + + public static createOrShow(context: vscode.ExtensionContext) { + if (SshGuidePanel.currentPanel) { + SshGuidePanel.currentPanel.panel.reveal(); + return; + } + + SshGuidePanel.currentPanel = new SshGuidePanel(context); + } + + private constructor(context: vscode.ExtensionContext) { + this.panel = vscode.window.createWebviewPanel( + SshGuidePanel.viewType, + "Oracle Cloud Assets", + { viewColumn: vscode.ViewColumn.One, preserveFocus: true }, + { + enableCommandUris: true, + enableScripts: true, + localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder))], + } + ); + const iconPath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, "images", "Apache_NetBeans_Logo.png")); + this.panel.iconPath = { + light: iconPath, + dark: iconPath, + }; + + this.setHtml(context); + + this.panel.onDidDispose( + () => { + this.dispose(); + removeGuidePropertiesFromStateFor(context, SshGuidePanel.statePropertyPrefix); + }, + null, + this.disposables + ); + + this.panel.onDidChangeViewState( + () => { + if (this.panel.visible) { + this.setHtml(context); + } + }, + null, + this.disposables + ); + + this.panel.webview.onDidReceiveMessage( + (message) => { + if (message.command === "showSSHGuide") { Review Comment: note: if doing an abstract class, the message command can be just one for all guides: the view will be talking only to its own panel instance, right ? ########## java/java.lsp.server/vscode/src/panels/SshGuidePanel.ts: ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from "vscode"; +import * as path from "path"; +import * as fs from "fs"; +import * as Handlebars from "handlebars"; +import { promisify } from "util"; +import { toggleGuideFor, shouldHideGuideFor, removeGuidePropertiesFromStateFor, NotificationActionType, sshConfigLocation, StatePropertyPrefix, dummyKeyPathLocation } from "./guidesUtil"; + +const readFile = promisify(fs.readFile); + +export class SshGuidePanel { + public static currentPanel: SshGuidePanel | undefined; + public static readonly viewType: string = "sshGuide"; + + private static readonly actionType: NotificationActionType = 'ssh-connection'; + private static readonly statePropertyPrefix: StatePropertyPrefix = 'sshGuide'; + + private static readonly webviewsFolder: string = "webviews"; + private static readonly scriptsFolder: string = "scripts"; + private static readonly stylesFolder: string = "styles"; + + private readonly panel: vscode.WebviewPanel; + private disposables: vscode.Disposable[] = []; + + public static createOrShow(context: vscode.ExtensionContext) { + if (SshGuidePanel.currentPanel) { + SshGuidePanel.currentPanel.panel.reveal(); + return; + } + + SshGuidePanel.currentPanel = new SshGuidePanel(context); + } + + private constructor(context: vscode.ExtensionContext) { + this.panel = vscode.window.createWebviewPanel( + SshGuidePanel.viewType, + "Oracle Cloud Assets", + { viewColumn: vscode.ViewColumn.One, preserveFocus: true }, + { + enableCommandUris: true, + enableScripts: true, + localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, SshGuidePanel.webviewsFolder))], + } + ); + const iconPath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, "images", "Apache_NetBeans_Logo.png")); + this.panel.iconPath = { + light: iconPath, + dark: iconPath, + }; + + this.setHtml(context); + + this.panel.onDidDispose( Review Comment: nitpick: can use `disposables.push(this.panel.onDidDispose(...))` if you don't require to pass `thisArg` ########## java/java.lsp.server/vscode/src/panels/SshGuidePanel.ts: ########## @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from "vscode"; +import * as path from "path"; +import * as fs from "fs"; +import * as Handlebars from "handlebars"; +import { promisify } from "util"; +import { toggleGuideFor, shouldHideGuideFor, removeGuidePropertiesFromStateFor, NotificationActionType, sshConfigLocation, StatePropertyPrefix, dummyKeyPathLocation } from "./guidesUtil"; + +const readFile = promisify(fs.readFile); + +export class SshGuidePanel { Review Comment: The `SshGuidePanel` and `RunImageGuidePanel` are +- the same except for constants. I suspect the Guide concept will continue to expand for more futures - it would be better to have an abstract superclass GuidePanel and either configured instances for each Guide or subclasses. ########## java/java.lsp.server/vscode/src/panels/guidesUtil.ts: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from 'vscode'; +import * as path from 'path'; +import * as os from 'os'; + +export const GUIDE_NOTIFICATION_CONFIGURATION_KEY = 'guides.notified'; + +export type NotificationActionType = 'ssh-connection' | 'run-container-image' +export type StatePropertyPrefix = 'sshGuide' | 'runContainerImageGuide' + +type GuideNotification = { + ocid: string; + actionType: NotificationActionType; +} + +export const sshConfigLocation = path.join(os.homedir(), '.ssh', 'config'); +export const dummyKeyPathLocation = path.join(os.homedir(), '<path-to-your-key>', 'ssh-key-yyyy-mm-dd.key'); + + +export function removeGuidePropertiesFromStateFor(context: vscode.ExtensionContext, statePropertyPrefix: StatePropertyPrefix) { + context.globalState.update(`${statePropertyPrefix}-publicIp`, undefined); + context.globalState.update(`${statePropertyPrefix}-ocid`, undefined); +} + +export function shouldHideGuideFor(actionType: NotificationActionType, ocid?: string) : boolean { + const notifiedGuides: Array<GuideNotification> | undefined = vscode.workspace.getConfiguration('netbeans').get<Array<GuideNotification>>(GUIDE_NOTIFICATION_CONFIGURATION_KEY); + return notifiedGuides && notifiedGuides.find((notified) => notified.ocid === ocid && notified.actionType === actionType) ? true : false; +} + +export async function toggleGuideFor(actionType: NotificationActionType, ocid: string) { + const notifiedGuides: Array<GuideNotification> = vscode.workspace.getConfiguration('netbeans').get<Array<GuideNotification>>(GUIDE_NOTIFICATION_CONFIGURATION_KEY) || []; + const notifiedForOcid = notifiedGuides.find((notified) => notified.ocid === ocid && notified.actionType === actionType) + + if (notifiedForOcid) { Review Comment: I am not sure if this belongs to (user) settings ... I think that vscode extensions usually use a `globalContext` for this kind of `do not show again` flags (i.e. welcome screens do). Depends on whether we want the user to manipulate with those settings. ########## java/java.lsp.server/vscode/src/panels/guidesUtil.ts: ########## @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as vscode from 'vscode'; +import * as path from 'path'; +import * as os from 'os'; + +export const GUIDE_NOTIFICATION_CONFIGURATION_KEY = 'guides.notified'; + +export type NotificationActionType = 'ssh-connection' | 'run-container-image' Review Comment: Question: is the text here and in StatePropertyPrefix in any way meaningful - or is it just an ID ? Why: if it's just an ID, could it be possible to use the same text value for notification type and for property prefix ? Also, while strong typed, it does not scale well: the type needs to be updated with every and each guide added in order to reuse the "seen" state support. Maybe using just text IDs, and exporting the identifier from the relevant *GuidePanel.ts would be better, although not that strict. In fact, the identifiers are already replicated in the *Guide.ts sources. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
