|
| 1 | +import { env, ExtensionContext, Uri, window } from 'vscode'; |
| 2 | + |
| 3 | +const FIRST_SHOWN_KEY = 'r5n.migrated.firstShownAt'; |
| 4 | +const DISMISSED_KEY = 'r5n.migrated.dismissed'; |
| 5 | +const NEW_EXTENSION_ID = 'r5n.es-js-snippets'; |
| 6 | +const REMINDER_AFTER_MS = 7 * 24 * 60 * 60 * 1000; |
| 7 | + |
| 8 | +const INITIAL_MESSAGE = |
| 9 | + 'ES7+ React/Redux/React-Native snippets has moved. Install the new version (r5n.es-js-snippets) for React 17–19 support and ongoing updates.'; |
| 10 | + |
| 11 | +const FINAL_MESSAGE = |
| 12 | + 'Final reminder: this extension has moved to r5n.es-js-snippets and is no longer maintained here.'; |
| 13 | + |
| 14 | +export async function showMigrationNotice( |
| 15 | + context: ExtensionContext, |
| 16 | +): Promise<void> { |
| 17 | + if (context.globalState.get(DISMISSED_KEY) === true) return; |
| 18 | + |
| 19 | + const now = Date.now(); |
| 20 | + const firstShownAt = context.globalState.get<number>(FIRST_SHOWN_KEY); |
| 21 | + |
| 22 | + if (firstShownAt === undefined) { |
| 23 | + await context.globalState.update(FIRST_SHOWN_KEY, now); |
| 24 | + await showToast(context, INITIAL_MESSAGE, false); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (now - firstShownAt >= REMINDER_AFTER_MS) { |
| 29 | + await showToast(context, FINAL_MESSAGE, true); |
| 30 | + await context.globalState.update(DISMISSED_KEY, true); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function showToast( |
| 35 | + context: ExtensionContext, |
| 36 | + message: string, |
| 37 | + finalNudge: boolean, |
| 38 | +): Promise<void> { |
| 39 | + const action = await window.showInformationMessage( |
| 40 | + message, |
| 41 | + 'Install new version', |
| 42 | + "Don't show again", |
| 43 | + ); |
| 44 | + |
| 45 | + if (action === 'Install new version') { |
| 46 | + await env.openExternal(Uri.parse(`vscode:extension/${NEW_EXTENSION_ID}`)); |
| 47 | + await context.globalState.update(DISMISSED_KEY, true); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (action === "Don't show again" || finalNudge) { |
| 52 | + await context.globalState.update(DISMISSED_KEY, true); |
| 53 | + } |
| 54 | +} |
0 commit comments