From 2a8e8f84764faaa7c10859aa41db556e25275d3b Mon Sep 17 00:00:00 2001 From: Sasha Mysak Date: Tue, 16 Jan 2024 11:33:44 +0100 Subject: [PATCH] wip --- intuita-webview/src/codemodList/App.tsx | 23 ++++ .../CodemodNodeRenderer/Codemod.tsx | 5 +- src/components/webview/MainProvider.ts | 2 +- src/components/webview/webviewEvents.ts | 4 + src/extension.ts | 100 ++++++++++-------- 5 files changed, 86 insertions(+), 48 deletions(-) diff --git a/intuita-webview/src/codemodList/App.tsx b/intuita-webview/src/codemodList/App.tsx index 7ebb6318..620fa388 100644 --- a/intuita-webview/src/codemodList/App.tsx +++ b/intuita-webview/src/codemodList/App.tsx @@ -17,6 +17,7 @@ import type { MainWebviewViewProps } from '../../../src/selectors/selectMainWebv import cn from 'classnames'; import { SectionHeader } from '../shared/SectionHeader'; import styles from './style.module.css'; +import { WebviewMessage } from '../shared/types'; const setSearchPhrase = (searchPhrase: string) => { vscode.postMessage({ @@ -35,6 +36,28 @@ export const App = memo( const publicRegistryRef = useRef(null); const privateRegistryRef = useRef(null); + useEffect(() => { + const handler = (e: MessageEvent) => { + const message = e.data; + + if (message.kind === 'webview.global.scrollToCodemod') { + const element = document.getElementById( + `${message.hashDigest}-body`, + ); + + if (element) { + element.scrollIntoView(); + } + } + }; + + window.addEventListener('message', handler); + + return () => { + window.removeEventListener('message', handler); + }; + }, []); + useEffect(() => { if (props.publicRegistryCollapsed) { publicRegistryRef.current?.collapse(); diff --git a/intuita-webview/src/codemodList/CodemodNodeRenderer/Codemod.tsx b/intuita-webview/src/codemodList/CodemodNodeRenderer/Codemod.tsx index 098c4b06..c9294527 100644 --- a/intuita-webview/src/codemodList/CodemodNodeRenderer/Codemod.tsx +++ b/intuita-webview/src/codemodList/CodemodNodeRenderer/Codemod.tsx @@ -245,7 +245,10 @@ const Codemod = ({ )} - + | Readonly<{ kind: 'webview.global.codemodExecutionHalted'; + }> + | Readonly<{ + kind: 'webview.global.scrollToCodemod'; + hashDigest: string; }>; export type WebviewResponse = diff --git a/src/extension.ts b/src/extension.ts index 8964e8c6..0d743755 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1098,60 +1098,63 @@ export async function activate(context: vscode.ExtensionContext) { ); // Expand collapsed parent directories of the relevant codemod - if (codemodHashDigest !== null) { - const privateCodemod = - state.privateCodemods.entities[codemodHashDigest] ?? - null; - - if (privateCodemod !== null) { - store.dispatch( - actions.setFocusedCodemodHashDigest( - codemodHashDigest as unknown as CodemodNodeHashDigest, - ), - ); - return; - } + const privateCodemod = + state.privateCodemods.entities[codemodHashDigest] ?? + null; - const codemod = - state.codemod.entities[codemodHashDigest] ?? null; - if (codemod === null) { - return; - } - const { name } = codemod; - const sep = name.indexOf('/') !== -1 ? '/' : ':'; + if (privateCodemod !== null) { + store.dispatch( + actions.setFocusedCodemodHashDigest( + codemodHashDigest as unknown as CodemodNodeHashDigest, + ), + ); - const pathParts = name - .split(sep) - .filter((part) => part !== ''); + mainViewProvider.__postMessage({ + kind: 'webview.global.scrollToCodemod', + hashDigest: codemodHashDigest, + }); + return; + } - if (pathParts.length === 0) { - return; - } + const codemod = + state.codemod.entities[codemodHashDigest] ?? null; + if (codemod === null) { + return; + } + const { name } = codemod; + const sep = name.indexOf('/') !== -1 ? '/' : ':'; - pathParts.forEach((name, idx) => { - const path = pathParts.slice(0, idx + 1).join(sep); + const pathParts = name + .split(sep) + .filter((part) => part !== ''); - if (idx === pathParts.length - 1) { - return; - } + if (pathParts.length === 0) { + return; + } - const parentHashDigest = buildHash( - [path, name].join('_'), - ) as CodemodNodeHashDigest; + pathParts.forEach((name, idx) => { + const path = pathParts.slice(0, idx + 1).join(sep); + + if (idx === pathParts.length - 1) { + return; + } - if ( - state.codemodDiscoveryView.expandedNodeHashDigests.includes( - parentHashDigest, - ) - ) { - return; - } + const parentHashDigest = buildHash( + [path, name].join('_'), + ) as CodemodNodeHashDigest; - store.dispatch( - actions.flipCodemodHashDigest(parentHashDigest), - ); - }); - } + if ( + state.codemodDiscoveryView.expandedNodeHashDigests.includes( + parentHashDigest, + ) + ) { + return; + } + + store.dispatch( + actions.flipCodemodHashDigest(parentHashDigest), + ); + }); if (state.codemodDiscoveryView.searchPhrase.length > 0) { store.dispatch(actions.setCodemodSearchPhrase('')); @@ -1162,6 +1165,11 @@ export async function activate(context: vscode.ExtensionContext) { codemodHashDigest as unknown as CodemodNodeHashDigest, ), ); + + mainViewProvider.__postMessage({ + kind: 'webview.global.scrollToCodemod', + hashDigest: codemodHashDigest, + }); } else if (accessToken !== null) { const routeUserToStudioToAuthenticate = async () => { const result = await vscode.window.showErrorMessage(