-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(onboarding): Add useIntegrationLauncher hook for multi-provider flows #112741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jaydgoss
wants to merge
1
commit into
jaygoss/vdy-69-scm-more-dropdown
from
jaygoss/vdy-69-integration-launcher-hook
+146
−59
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
128 changes: 128 additions & 0 deletions
128
static/app/views/settings/organizationIntegrations/useIntegrationLauncher.tsx
This file contains hidden or 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,128 @@ | ||
| import {useCallback, useEffect, useRef} from 'react'; | ||
| import * as qs from 'query-string'; | ||
|
|
||
| import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator'; | ||
| import {openPipelineModal} from 'sentry/components/pipeline/modal'; | ||
| import {t} from 'sentry/locale'; | ||
| import {ConfigStore} from 'sentry/stores/configStore'; | ||
| import type {IntegrationProvider, IntegrationWithConfig} from 'sentry/types/integrations'; | ||
| import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil'; | ||
|
|
||
| import type {AddIntegrationParams} from './addIntegration'; | ||
| import {computeCenteredWindow, getApiPipelineProvider} from './addIntegration'; | ||
|
|
||
| type UseIntegrationLauncherParams = Omit< | ||
| AddIntegrationParams, | ||
| 'provider' | 'account' | 'modalParams' | ||
| >; | ||
|
|
||
| /** | ||
| * Launches integration install flows for any provider passed at call time. | ||
| * | ||
| * Unlike {@link useAddIntegration}, which binds to a single provider at hook | ||
| * initialization, this hook accepts the provider as an argument to `startFlow`. | ||
| * This makes it suitable for data-driven UIs (dropdowns, menus) that need to | ||
| * launch flows for multiple providers from a single hook instance. | ||
| * | ||
| * Only one legacy popup flow can be active at a time. Starting a new flow | ||
| * while one is pending will replace the active provider context. | ||
| */ | ||
| export function useIntegrationLauncher({ | ||
| organization, | ||
| onInstall, | ||
| analyticsParams, | ||
| }: UseIntegrationLauncherParams) { | ||
| const dialogRef = useRef<Window | null>(null); | ||
| const activeProviderRef = useRef<IntegrationProvider | null>(null); | ||
| const onInstallRef = useRef(onInstall); | ||
| onInstallRef.current = onInstall; | ||
| const analyticsParamsRef = useRef(analyticsParams); | ||
| analyticsParamsRef.current = analyticsParams; | ||
|
|
||
| useEffect(() => { | ||
| function handleMessage(message: MessageEvent) { | ||
| const validOrigins = [ | ||
| ConfigStore.get('links').sentryUrl, | ||
| ConfigStore.get('links').organizationUrl, | ||
| document.location.origin, | ||
| ]; | ||
| if (!validOrigins.includes(message.origin)) { | ||
| return; | ||
| } | ||
| if (message.source !== dialogRef.current) { | ||
| return; | ||
| } | ||
|
|
||
| const {success, data} = message.data; | ||
| dialogRef.current = null; | ||
| const provider = activeProviderRef.current; | ||
| activeProviderRef.current = null; | ||
|
|
||
| if (!success) { | ||
| addErrorMessage(data?.error ?? t('An unknown error occurred')); | ||
| return; | ||
| } | ||
| if (!data || !provider) { | ||
| return; | ||
| } | ||
|
|
||
| trackIntegrationAnalytics('integrations.installation_complete', { | ||
| integration: provider.key, | ||
| integration_type: 'first_party', | ||
| organization, | ||
| ...analyticsParamsRef.current, | ||
| }); | ||
| addSuccessMessage(t('%s added', provider.name)); | ||
| onInstallRef.current(data); | ||
| } | ||
|
|
||
| window.addEventListener('message', handleMessage); | ||
| return () => { | ||
| window.removeEventListener('message', handleMessage); | ||
| dialogRef.current?.close(); | ||
| }; | ||
| }, [organization]); | ||
|
|
||
| const startFlow = useCallback( | ||
| (provider: IntegrationProvider, urlParams?: Record<string, string>) => { | ||
| trackIntegrationAnalytics('integrations.installation_start', { | ||
| integration: provider.key, | ||
| integration_type: 'first_party', | ||
| organization, | ||
| ...analyticsParams, | ||
| }); | ||
|
|
||
| const pipelineProvider = getApiPipelineProvider(organization, provider.key); | ||
| if (pipelineProvider !== null) { | ||
| openPipelineModal({ | ||
| type: 'integration', | ||
| provider: pipelineProvider, | ||
| onComplete: (data: IntegrationWithConfig) => { | ||
| trackIntegrationAnalytics('integrations.installation_complete', { | ||
| integration: provider.key, | ||
| integration_type: 'first_party', | ||
| organization, | ||
| ...analyticsParamsRef.current, | ||
| }); | ||
| addSuccessMessage(t('%s added', provider.name)); | ||
| onInstallRef.current(data); | ||
| }, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Legacy popup flow | ||
| const {url, width, height} = provider.setupDialog; | ||
| const {left, top} = computeCenteredWindow(width, height); | ||
| const installUrl = `${url}?${qs.stringify(urlParams ?? {})}`; | ||
| const opts = `scrollbars=yes,width=${width},height=${height},top=${top},left=${left}`; | ||
|
|
||
| activeProviderRef.current = provider; | ||
| dialogRef.current = window.open(installUrl, 'sentryAddIntegration', opts); | ||
| dialogRef.current?.focus(); | ||
| }, | ||
| [organization, analyticsParams] | ||
| ); | ||
|
|
||
| return {startFlow}; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we could actually just refactor useIntegration to have this API, there's not too many call-sites I think so it wouldn't be a huge lift right?