Skip to content
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

Compass 23919 Onboarding flow implement custom bridge method #149

9 changes: 9 additions & 0 deletions ui/src/components/AuthPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useAppContext } from '../../hooks/useAppContext';
import { IncomingWebhookSectionMessage } from '../IncomingWebhookSectionMessage';
import { GitLabRoles } from '../../types';
import { CopyIconWrapper, FormWrapper, ReloadButtonWrapper, SectionMessageWrapper, TokenRoleWrapper } from './styles';
import { checkOnboardingRedirection } from '../onboarding-flow-context-helper';

const buildValidationMethod = (errorType: ErrorTypes) => {
switch (errorType) {
Expand Down Expand Up @@ -113,6 +114,12 @@ export const AuthPage = () => {
});
};

const handleOnboardingRedirectIfFailedAuth = async () => {
await checkOnboardingRedirection('CONFIGURATION_ERROR').catch((err) => {
console.error('Error checking if context is in onboarding flow:', err);
});
};

const handleNavigateToConnectedPage = () => {
navigate(`..${ApplicationState.CONNECTED}`, { replace: true });
};
Expand All @@ -132,6 +139,7 @@ export const AuthPage = () => {
}
} else {
setErrorType((errors && errors[0].errorType) || AuthErrorTypes.UNEXPECTED_ERROR);
await handleOnboardingRedirectIfFailedAuth();
}

setLoadingConnectGroup(false);
Expand Down Expand Up @@ -161,6 +169,7 @@ export const AuthPage = () => {
handleNavigateToConnectedPage();
} else {
setErrorType((errors && errors[0].errorType) || AuthErrorTypes.UNEXPECTED_ERROR);
await handleOnboardingRedirectIfFailedAuth();
}

setLoadingConnectWebhook(false);
Expand Down
18 changes: 18 additions & 0 deletions ui/src/components/ImportResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useImportResult } from '../../hooks/useImportResult';
import { CenterWrapper, SectionWrapper, TableWrapper } from '../styles';
import { clearResult } from '../../services/invokes';
import { ErrorTypes, ImportErrorTypes } from '../../resolverTypes';
import { checkOnboardingRedirection } from '../onboarding-flow-context-helper';

export const ImportResult: FunctionComponent = () => {
const [error, setError] = useState<ErrorTypes | null>();
Expand Down Expand Up @@ -43,6 +44,23 @@ export const ImportResult: FunctionComponent = () => {
}
}, [failedProjects, totalProjects]);

useEffect(() => {
const redirectOnboarding = async () => {
try {
if (failedProjects.length > 0) {
await checkOnboardingRedirection('IMPORT_ERROR');
} else {
await checkOnboardingRedirection();
}
} catch (err) {
console.error('Error checking if context is in onboarding flow:', err);
}
};
redirectOnboarding().catch((e) => {
console.error('Error redirecting to onboarding:', e);
});
}, [failedProjects]);

if (isLoading) {
return (
<CenterWrapper>
Expand Down
4 changes: 4 additions & 0 deletions ui/src/components/SelectImportPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SelectorItem } from './screens/SelectProjectsScreen/buildGroupsSelector
import { useAppContext } from '../../hooks/useAppContext';
import { useComponentTypes } from '../../hooks/useComponentTypes';
import { getComponentTypeOption } from '../utils';
import { checkOnboardingRedirection } from '../onboarding-flow-context-helper';
import { getAvailableImportComponentTypes } from './utils';
import { useProjects } from '../../hooks/useProjects';
import { useTeamsForImport } from '../../hooks/useTeamsForImport';
Expand Down Expand Up @@ -262,6 +263,9 @@ export const SelectImportPage = () => {
};

const handleNavigateToConnectedPage = async () => {
await checkOnboardingRedirection('CONFIGURATION_ERROR').catch((error) => {
console.error('Error checking if context is in onboarding flow:', error);
});
await router.navigate('/compass/components');
};

Expand Down
20 changes: 20 additions & 0 deletions ui/src/components/onboarding-flow-context-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { view } from '@forge/bridge';
import { getCallBridge } from '@forge/bridge/out/bridge';

export async function isRenderingInOnboardingFlow(): Promise<boolean> {
try {
const context = await view.getContext();
return context.extension.renderingLocation === 'onboardingFlow';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a TODO: comment here with a link to the task to import the package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think due to the open source nature of this repo, it may be impossible to import that package which is not published to npm

} catch (error) {
console.error('Error fetching onboarding flow context:', error);
return false;
}
}

export async function checkOnboardingRedirection(err?: string): Promise<void> {
const isInOnboardingFlow = await isRenderingInOnboardingFlow();
if (isInOnboardingFlow) {
const params = err ? { error: err } : undefined;
await getCallBridge()('redirectOnboardingTube', params);
}
}
1 change: 1 addition & 0 deletions ui/src/helpers/mockHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const mockGetContext = (moduleKey: string) => {
getContext.mockImplementation(async () => {
return {
moduleKey,
extension: {},
};
});
};
Loading