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 (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj authored Sep 20, 2023
1 parent cebb0db commit 65703fd
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 53 deletions.
8 changes: 1 addition & 7 deletions intuita-webview/src/codemodList/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,7 @@ export const App = memo(
<PanelResizeHandle className="resize-handle" />
<SectionHeader
title="Private Registry"
commands={[
{
icon: 'clear-all',
title: 'Clear all',
command: 'intuita.clearPrivateCodemods',
},
]}
commands={[]}
collapsed={props.privateRegistryCollapsed}
onClick={(event) => {
event.preventDefault();
Expand Down
18 changes: 18 additions & 0 deletions intuita-webview/src/codemodList/CodemodNodeRenderer/Codemod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ const renderActionButtons = (
});
};

const handleRemovePrivateCodemod = () => {
vscode.postMessage({
kind: 'webview.main.removePrivateCodemod',
hashDigest,
});
};

return (
<>
<ActionButton
Expand Down Expand Up @@ -108,6 +115,15 @@ const renderActionButtons = (
>
<span className={cn('codicon', 'codicon-link')} />
</ActionButton>
{isPrivate && (
<ActionButton
id={`${hashDigest}-deleteButton`}
content={'Remove from Private Registry'}
onClick={handleRemovePrivateCodemod}
>
<span className={cn('codicon', 'codicon-trash')} />
</ActionButton>
)}
</>
);
}
Expand Down Expand Up @@ -231,10 +247,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
15 changes: 15 additions & 0 deletions src/components/webview/MainProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ 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.main.removePrivateCodemod') {
commands.executeCommand(
'intuita.removePrivateCodemod',
message.hashDigest,
);
}

if (message.kind === 'webview.global.flipSelectedExplorerNode') {
this.__store.dispatch(
actions.flipSelectedExplorerNode([
Expand Down
16 changes: 16 additions & 0 deletions src/components/webview/webviewEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ 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.removePrivateCodemod';
hashDigest: CodemodNodeHashDigest;
}>
| 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
114 changes: 75 additions & 39 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 @@ -850,27 +860,48 @@ export async function activate(context: vscode.ExtensionContext) {
);

context.subscriptions.push(
vscode.commands.registerCommand('intuita.clearPrivateCodemods', () => {
const state = store.getState();
const hashDigests = state.privateCodemods.ids as CodemodHash[];
hashDigests.forEach((hashDigest) => {
const codemodPath = join(homedir(), '.intuita', hashDigest);
if (existsSync(codemodPath)) {
rmSync(codemodPath, { recursive: true, force: true });
}
});
vscode.commands.registerCommand(
'intuita.removePrivateCodemod',
(arg0: unknown) => {
try {
const hashDigest: string | null =
typeof arg0 === 'string' ? arg0 : null;

const codemodNamesPath = join(
homedir(),
'.intuita',
'privateCodemodNames.json',
);
if (existsSync(codemodNamesPath)) {
rmSync(codemodNamesPath);
}
if (hashDigest === null) {
throw new Error(
'Did not pass the hashDigest into the command.',
);
}
const codemodPath = join(homedir(), '.intuita', hashDigest);
if (existsSync(codemodPath)) {
rmSync(codemodPath, { recursive: true, force: true });
}

store.dispatch(actions.removePrivateCodemods(hashDigests));
}),
const codemodNamesPath = join(
homedir(),
'.intuita',
'privateCodemodNames.json',
);
if (existsSync(codemodNamesPath)) {
rmSync(codemodNamesPath);
}

store.dispatch(
actions.removePrivateCodemods([
hashDigest as CodemodHash,
]),
);
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
vscode.window.showErrorMessage(message);

vscodeTelemetry.sendError({
kind: 'failedToExecuteCommand',
commandName: 'intuita.removePrivateCodemod',
});
}
},
),
);

context.subscriptions.push(
Expand Down Expand Up @@ -1140,8 +1171,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
Loading

0 comments on commit 65703fd

Please sign in to comment.