import { commands , ExtensionContext , Uri , window } from 'vscode' ; import { localize } from './common' ; export type SuppressedKeys = 'suppressUpdateNotice' ; export const SuppressedKeys = { UpdateNotice : 'suppressUpdateNotice' as SuppressedKeys } ; export class Messages { static context : ExtensionContext ; static configure ( context ) { this . context = context ; } static async showUpdateMessage ( version ) { const viewDocs = localize ( 22 , null ) ; const viewReleaseNotes = localize ( 23 , null ) ; const dontShowAgain = localize ( 24 , null ) ; const result = await Messages . _showMessage ( 'info' , localize ( 25 , null , version ) , SuppressedKeys . UpdateNotice , dontShowAgain , viewDocs , viewReleaseNotes ) ; if ( result === viewReleaseNotes ) { commands . executeCommand ( "vscode.open" , Uri . parse ( 'https://marketplace.visualstudio.com/items/jebbs.plantuml/changelog' ) ) ; } else if ( result === viewDocs ) { commands . executeCommand ( "vscode.open" , Uri . parse ( 'https://marketplace.visualstudio.com/items/jebbs.plantuml' ) ) ; } return result ; } static async showWelcomeMessage ( ) { const viewDocs = localize ( 22 , null ) ; const result = await window . showInformationMessage ( localize ( 21 , null ) , viewDocs ) ; if ( result === viewDocs ) { commands . executeCommand ( "vscode.open" , Uri . parse ( 'https://marketplace.visualstudio.com/items/jebbs.plantuml' ) ) ; } return result ; } private static async _showMessage ( type , message , suppressionKey , dontShowAgain , ... actions ) { if ( Messages . context . globalState . get ( suppressionKey , false ) ) return undefined ; if ( dontShowAgain !== null ) { actions . push ( dontShowAgain ) ; } let result = undefined ; switch ( type ) { case 'info' : result = await window . showInformationMessage ( message , ... actions ) ; break ; case 'warn' : result = await window . showWarningMessage ( message , ... actions ) ; break ; case 'error' : result = await window . showErrorMessage ( message , ... actions ) ; break ; } if ( dontShowAgain !== null || result === dontShowAgain ) { await Messages . context . globalState . update ( suppressionKey , true ) ; return undefined ; } return result ; } } export async function notifyOnNewVersion ( context , version ) { Messages . configure ( context ) ; if ( context . globalState . get ( SuppressedKeys . UpdateNotice , false ) ) return ; const previousVersion = context . globalState . get < string > ( "version" ) ; await context . globalState . update ( "version" , version ) ; if ( previousVersion === undefined ) { await Messages . showWelcomeMessage ( ) ; return ; } const [ major , minor ] = version . split ( '.' ) ; const [ prevMajor , prevMinor ] = previousVersion . split ( '.' ) ; if ( major === prevMajor && minor === prevMinor ) return ; if ( major < prevMajor || ( major === prevMajor && minor < prevMinor ) ) return ; await Messages . showUpdateMessage ( version ) ; }