|
1 | 1 | import React from 'react'; |
2 | 2 | import { useDispatch } from 'react-redux'; |
3 | 3 | import { Outlet } from 'react-router-dom'; |
| 4 | +import Messages, { |
| 5 | + MessageType, |
| 6 | + MessageTypeNames, |
| 7 | + sendToWebview |
| 8 | +} from 'src/features/vscode/messages'; |
4 | 9 |
|
5 | 10 | import NavigationBar from '../navigationBar/NavigationBar'; |
6 | 11 | import Constants from '../utils/Constants'; |
7 | 12 | import { useLocalStorageState, useSession } from '../utils/Hooks'; |
| 13 | +import WorkspaceActions from '../workspace/WorkspaceActions'; |
8 | 14 | import { defaultWorkspaceSettings, WorkspaceSettingsContext } from '../WorkspaceSettingsContext'; |
9 | 15 | import SessionActions from './actions/SessionActions'; |
| 16 | +import VscodeActions from './actions/VscodeActions'; |
10 | 17 |
|
11 | 18 | const Application: React.FC = () => { |
12 | 19 | const dispatch = useDispatch(); |
@@ -70,6 +77,62 @@ const Application: React.FC = () => { |
70 | 77 | }; |
71 | 78 | }, [isPWA, isMobile]); |
72 | 79 |
|
| 80 | + // Effect to handle messages from VS Code |
| 81 | + React.useEffect(() => { |
| 82 | + if (!window.confirm) { |
| 83 | + // Polyfill confirm() to instead show as VS Code notification |
| 84 | + // TODO: Pass text as a new Message to the webview |
| 85 | + window.confirm = text => { |
| 86 | + console.log(`Confirmation automatically accepted: ${text ?? 'No text provided'}`); |
| 87 | + return true; |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + const message = Messages.ExtensionPing(); |
| 92 | + sendToWebview(message); |
| 93 | + |
| 94 | + window.addEventListener('message', event => { |
| 95 | + const message: MessageType = event.data; |
| 96 | + // Only accept messages from the vscode webview |
| 97 | + if (!event.origin.startsWith('vscode-webview://')) { |
| 98 | + return; |
| 99 | + } |
| 100 | + // console.log(`FRONTEND: Message from ${event.origin}: ${JSON.stringify(message)}`); |
| 101 | + switch (message.type) { |
| 102 | + case MessageTypeNames.ExtensionPong: |
| 103 | + console.log('Received WebviewStarted message, will set vsc'); |
| 104 | + dispatch(VscodeActions.setVscode()); |
| 105 | + |
| 106 | + if (message.token) { |
| 107 | + const token = JSON.parse(message.token.trim()); |
| 108 | + console.log(`FRONTEND: WebviewStarted: ${token}`); |
| 109 | + dispatch( |
| 110 | + SessionActions.setTokens({ |
| 111 | + accessToken: token.accessToken, |
| 112 | + refreshToken: token.refreshToken |
| 113 | + }) |
| 114 | + ); |
| 115 | + dispatch(SessionActions.fetchUserAndCourse()); |
| 116 | + } |
| 117 | + break; |
| 118 | + case MessageTypeNames.Text: |
| 119 | + const code = message.code; |
| 120 | + console.log(`FRONTEND: TextMessage: ${code}`); |
| 121 | + // TODO: Don't change ace editor directly |
| 122 | + // const elements = document.getElementsByClassName('react-ace'); |
| 123 | + // if (elements.length === 0) { |
| 124 | + // return; |
| 125 | + // } |
| 126 | + // // @ts-expect-error: ace is not available at compile time |
| 127 | + // const editor = ace.edit(elements[0]); |
| 128 | + // editor.setValue(code); |
| 129 | + dispatch(WorkspaceActions.updateEditorValue('assessment', 0, code)); |
| 130 | + break; |
| 131 | + } |
| 132 | + }); |
| 133 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 134 | + }, []); |
| 135 | + |
73 | 136 | return ( |
74 | 137 | <WorkspaceSettingsContext.Provider value={[workspaceSettings, setWorkspaceSettings]}> |
75 | 138 | <div className="Application"> |
|
0 commit comments