Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
INT-1898 Use custom notifs for auth flow (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj authored Sep 20, 2023
1 parent cebb0db commit 5cf0d91
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ const Codemod = ({
toast.update(progress.codemodHash, {
progress: value,
render: `Processed ${progress.processedFileNumber} / ${progress.totalFileNumber} files`,
containerId: 'codemodListToastContainer',
});
} else {
toast(`Processed 0 / ${progress.totalFileNumber} files`, {
toastId: progress.codemodHash,
containerId: 'codemodListToastContainer',
progress: 0,
});
}
Expand Down
6 changes: 6 additions & 0 deletions intuita-webview/src/main/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@
.vscode-panel-view {
padding: 0px;
}

.toasterComponent {
display: flex;
flex-direction: column;
align-items: center;
}
62 changes: 55 additions & 7 deletions intuita-webview/src/main/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { toast } from 'react-toastify';
import { useEffect, useRef, useState } from 'react';

import {
VSCodeButton,
VSCodePanels,
VSCodePanelTab,
VSCodePanelView,
Expand All @@ -18,6 +20,17 @@ import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { useTheme } from '../shared/Snippet/useTheme';

const toastContainerProps = {
pauseOnHover: false,
pauseOnFocusLoss: false,
hideProgressBar: false,
closeOnClick: false,
closeButton: false,
draggable: false,
autoClose: false as const,
enableMultiContainer: true,
};

declare global {
interface Window {
mainWebviewViewProps: MainWebviewViewProps;
Expand Down Expand Up @@ -76,6 +89,41 @@ function App() {
};
}, []);

useEffect(() => {
if (mainWebviewViewProps.toaster === null) {
return;
}

const { content, ...toasterProps } = mainWebviewViewProps.toaster;
let componentToRender = null;

if (toasterProps.toastId === 'handleSignedInUser') {
componentToRender = (
<div className="toasterComponent">
<p>{content}</p>
<VSCodeButton
appearance="secondary"
onClick={() => {
toast.dismiss(toasterProps.toastId);
vscode.postMessage({
kind: 'webview.main.signOut',
});
}}
>
Sign out
</VSCodeButton>
</div>
);
}
toast(componentToRender ?? content, toasterProps);

// remove the current toaster props from Redux state
vscode.postMessage({
kind: 'webview.main.setToaster',
value: null,
});
}, [mainWebviewViewProps.toaster]);

const handlePanelTabClick = (id: ActiveTabId) => {
vscode.postMessage({
kind: 'webview.main.setActiveTabId',
Expand Down Expand Up @@ -126,16 +174,10 @@ function App() {
/>
) : null}
<ToastContainer
{...toastContainerProps}
containerId="codemodListToastContainer"
pauseOnHover={false}
pauseOnFocusLoss={false}
autoClose={false}
hideProgressBar={false}
position="bottom-right"
closeOnClick={false}
closeButton={false}
theme={theme === 'vs-light' ? 'light' : 'dark'}
draggable={false}
/>
</VSCodePanelView>
<VSCodePanelView
Expand Down Expand Up @@ -166,6 +208,12 @@ function App() {
) : null}
</VSCodePanelView>
</VSCodePanels>
<ToastContainer
{...toastContainerProps}
containerId="primarySidebarToastContainer"
theme={theme === 'vs-light' ? 'light' : 'dark'}
position="top-right"
/>
</main>
);
}
Expand Down
8 changes: 8 additions & 0 deletions src/components/webview/MainProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ export class MainViewProvider implements WebviewViewProvider {
);
}

if (message.kind === 'webview.main.setToaster') {
this.__store.dispatch(actions.setToaster(message.value));
}

if (message.kind === 'webview.main.signOut') {
commands.executeCommand('intuita.signOut');
}

if (message.kind === 'webview.global.flipSelectedExplorerNode') {
this.__store.dispatch(
actions.flipSelectedExplorerNode([
Expand Down
12 changes: 12 additions & 0 deletions src/components/webview/webviewEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ export type WebviewResponse =
kind: 'webview.main.setActiveTabId';
activeTabId: ActiveTabId;
}>
| Readonly<{
kind: 'webview.main.setToaster';
value: {
containerId: string;
toastId: string;
content: string;
autoClose: number;
} | null;
}>
| Readonly<{
kind: 'webview.main.signOut';
}>
| Readonly<{
kind:
| 'webview.main.setCodemodRunsPanelGroupSettings'
Expand Down
4 changes: 4 additions & 0 deletions src/data/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const getInitialState = (): RootState => {
collapsedExplorerNodes: {},
reviewedExplorerNodes: {},
focusedExplorerNodes: {},
toaster: null,
};
};

Expand Down Expand Up @@ -664,6 +665,9 @@ const rootSlice = createSlice({
) {
state.sourceControl = action.payload;
},
setToaster(state, action: PayloadAction<RootState['toaster']>) {
state.toaster = action.payload;
},
collapseResultsPanel(state, action: PayloadAction<boolean>) {
state.codemodRunsTab.resultsCollapsed = action.payload;
},
Expand Down
55 changes: 35 additions & 20 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,35 @@ export async function activate(context: vscode.ExtensionContext) {
);

context.subscriptions.push(
vscode.commands.registerCommand(
'intuita.handleSignedInUser',
async () => {
const decision = await vscode.window.showInformationMessage(
'You are already signed-in.',
'Do you want to sign out?',
);
vscode.commands.registerCommand('intuita.signOut', () => {
userService.unlinkUserIntuitaAccount();
vscode.commands.executeCommand(
'setContext',
'intuita.signedIn',
false,
);
store.dispatch(
actions.setToaster({
toastId: 'signOut',
containerId: 'primarySidebarToastContainer',
content: 'Signed out',
autoClose: 3000,
}),
);
}),
);

if (decision === 'Do you want to sign out?') {
userService.unlinkUserIntuitaAccount();
vscode.commands.executeCommand(
'setContext',
'intuita.signedIn',
false,
);
vscode.window.showInformationMessage('You are signed out.');
}
},
),
context.subscriptions.push(
vscode.commands.registerCommand('intuita.handleSignedInUser', () => {
store.dispatch(
actions.setToaster({
toastId: 'handleSignedInUser',
containerId: 'primarySidebarToastContainer',
content: 'Already signed-in',
autoClose: 5000,
}),
);
}),
);

context.subscriptions.push(
Expand Down Expand Up @@ -1140,8 +1150,13 @@ export async function activate(context: vscode.ExtensionContext) {
'intuita.signedIn',
true,
);
vscode.window.showInformationMessage(
'You are successfully signed in.',
store.dispatch(
actions.setToaster({
toastId: 'signIn',
containerId: 'primarySidebarToastContainer',
content: 'Successfully signed in',
autoClose: 3000,
}),
);
} else {
await routeUserToStudioToAuthenticate();
Expand Down
12 changes: 12 additions & 0 deletions src/persistedState/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ export const persistedStateCodecNew = buildTypeCodec({
kind: 'IDLENESS',
},
),
toaster: withFallback(
t.union([
buildTypeCodec({
containerId: t.string,
toastId: t.string,
content: t.string,
autoClose: t.number,
}),
t.null,
]),
null,
),
caseHashJobHashes: withFallback(t.readonlyArray(t.string), []),
caseHashInProgress: withFallback(t.union([caseHashCodec, t.null]), null),
applySelectedInProgress: withFallback(t.boolean, false),
Expand Down
4 changes: 4 additions & 0 deletions src/selectors/selectMainWebviewViewProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const selectMainWebviewViewProps = (
if (state.activeTabId === 'codemods') {
return {
activeTabId: state.activeTabId,
toaster: state.toaster,
searchPhrase: state.codemodDiscoveryView.searchPhrase,
autocompleteItems,
codemodTree: selectCodemodTree(
Expand All @@ -39,6 +40,7 @@ export const selectMainWebviewViewProps = (
if (state.activeTabId === 'codemodRuns') {
return {
activeTabId: state.activeTabId,
toaster: state.toaster,
applySelectedInProgress: state.applySelectedInProgress,
codemodRunsTree:
rootUri !== null
Expand All @@ -61,6 +63,7 @@ export const selectMainWebviewViewProps = (

return {
activeTabId: state.activeTabId,
toaster: state.toaster,
title: sourceControlTabProps?.title ?? '',
body: sourceControlTabProps?.body ?? '',
loading: sourceControlTabProps?.loading ?? false,
Expand All @@ -69,6 +72,7 @@ export const selectMainWebviewViewProps = (

return {
activeTabId: state.activeTabId,
toaster: state.toaster,
};
};

Expand Down

0 comments on commit 5cf0d91

Please sign in to comment.