diff --git a/extensions/lib/hello-someone/hello-someone.ts b/extensions/lib/hello-someone/hello-someone.ts index e601850c51..34193a5b8c 100644 --- a/extensions/lib/hello-someone/hello-someone.ts +++ b/extensions/lib/hello-someone/hello-someone.ts @@ -1,7 +1,13 @@ import papi from 'papi'; -import type { WebViewContentType } from 'shared/data/web-view.model'; +import type { + WebViewContentType, + WebViewDefinition, + SavedWebViewDefinition, +} from 'shared/data/web-view.model'; import { UnsubscriberAsync } from 'shared/utils/papi-util'; import type IDataProvider from 'shared/models/data-provider.interface'; +import type { IWebViewProvider } from 'shared/models/web-view-provider.model'; +import type { ExecutionActivationContext } from 'extension-host/extension-types/extension-activation-context.model'; // @ts-expect-error ts(1192) this file has no default export; the text is exported by rollup import helloSomeoneHtmlWebView from './hello-someone.web-view.ejs'; @@ -45,7 +51,28 @@ const greetingsDataProviderEngine = { }, }; -export async function activate() { +const peopleWebViewType = 'hello-someone.people-viewer'; +const peopleWebViewIdKey = 'people-web-view-id'; + +/** + * Simple web view provider that provides People web views when papi requests them + */ +const peopleWebViewProvider: IWebViewProvider = { + async getWebView(savedWebView: SavedWebViewDefinition): Promise { + if (savedWebView.webViewType !== peopleWebViewType) + throw new Error( + `${peopleWebViewType} provider received request to provide a ${savedWebView.webViewType} web view`, + ); + return { + ...savedWebView, + title: 'People', + contentType: 'html' as WebViewContentType.HTML, + content: helloSomeoneHtmlWebView, + }; + }, +}; + +export async function activate(context: ExecutionActivationContext) { logger.info('Hello Someone is activating!'); const greetingsDataProviderPromise = papi.dataProvider.registerEngine( @@ -53,14 +80,9 @@ export async function activate() { greetingsDataProviderEngine, ); - await papi.webViews.addWebView( - { - id: 'Hello Someone', - title: 'Hello Someone HTML', - contentType: 'html' as WebViewContentType.HTML, - content: helloSomeoneHtmlWebView, - }, - { type: 'panel', direction: 'top' }, + const peopleWebViewProviderPromise = papi.webViews.registerWebViewProvider( + peopleWebViewType, + peopleWebViewProvider, ); const unsubPromises: Promise[] = [ @@ -80,11 +102,47 @@ export async function activate() { ), ]; - // For now, let's just make things easy and await the data provider promise at the end so we don't hold everything else up + // Create a webview or get the existing webview if ours already exists + // Note: here, we are storing a created webview's id when we create it, and using that id on + // `existingId` to look specifically for the webview that we previously created if we have ever + // created one in a previous session. This means that, if someone else creates a people web view, + // it will be distinct from this one. We are creating our own web view here. See `hello-world.ts` + // for an example of getting any webview with the specified `webViewType` + + // Get existing webview id if we previously created a webview for this type + let existingPeopleWebViewId: string | undefined; + try { + existingPeopleWebViewId = await papi.storage.readUserData( + context.executionToken, + peopleWebViewIdKey, + ); + } catch (e) { + existingPeopleWebViewId = undefined; + } + + // Get the existing web view if one exists or create a new one + const peopleWebViewId = await papi.webViews.getWebView( + peopleWebViewType, + { type: 'panel', direction: 'top' }, + { existingId: existingPeopleWebViewId }, + ); + + // Save newly acquired webview id + await papi.storage.writeUserData( + context.executionToken, + peopleWebViewIdKey, + peopleWebViewId || '', + ); + + // For now, let's just make things easy and await the registration promises at the end so we don't hold everything else up const greetingsDataProvider = await greetingsDataProviderPromise; + const peopleWebViewProviderResolved = await peopleWebViewProviderPromise; const combinedUnsubscriber: UnsubscriberAsync = papi.util.aggregateUnsubscriberAsyncs( - (await Promise.all(unsubPromises)).concat([greetingsDataProvider.dispose]), + (await Promise.all(unsubPromises)).concat([ + greetingsDataProvider.dispose, + peopleWebViewProviderResolved.dispose, + ]), ); logger.info('Hello Someone is finished activating!'); return combinedUnsubscriber; diff --git a/extensions/lib/hello-someone/hello-someone.web-view.ejs b/extensions/lib/hello-someone/hello-someone.web-view.ejs index 6a463ff698..6a55b7b712 100644 --- a/extensions/lib/hello-someone/hello-someone.web-view.ejs +++ b/extensions/lib/hello-someone/hello-someone.web-view.ejs @@ -19,6 +19,8 @@
Any Greetings Updates: 0
Any Greetings Updates (via Bill): 0
Bill Greetings Updates: 0
+
+