|
1 | 1 | import React from 'react'; |
2 | 2 | import { useDispatch } from 'react-redux'; |
3 | 3 | import { Outlet } from 'react-router'; |
4 | | -import Messages, { |
5 | | - MessageType, |
6 | | - MessageTypeNames, |
7 | | - sendToWebview |
8 | | -} from 'src/features/vscode/messages'; |
9 | 4 |
|
10 | 5 | import NavigationBar from '../navigationBar/NavigationBar'; |
11 | 6 | import Constants from '../utils/Constants'; |
12 | 7 | import { useLocalStorageState, useSession } from '../utils/Hooks'; |
13 | | -import WorkspaceActions from '../workspace/WorkspaceActions'; |
| 8 | +import { useVscodeIntegration } from '../utils/hooks/useVscodeIntegration'; |
14 | 9 | import { defaultWorkspaceSettings, WorkspaceSettingsContext } from '../WorkspaceSettingsContext'; |
15 | 10 | import SessionActions from './actions/SessionActions'; |
16 | | -import VscodeActions from './actions/VscodeActions'; |
17 | 11 |
|
18 | 12 | const Application: React.FC = () => { |
19 | 13 | const dispatch = useDispatch(); |
@@ -77,71 +71,7 @@ const Application: React.FC = () => { |
77 | 71 | }; |
78 | 72 | }, [isPWA, isMobile]); |
79 | 73 |
|
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(window.origin); |
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 { workspaceLocation, code } = message; |
120 | | - console.log(`FRONTEND: TextMessage: ${code}`); |
121 | | - dispatch(WorkspaceActions.updateEditorValue(workspaceLocation, 0, code)); |
122 | | - break; |
123 | | - case MessageTypeNames.EvalEditor: |
124 | | - dispatch(WorkspaceActions.evalEditor(message.workspaceLocation)); |
125 | | - break; |
126 | | - case MessageTypeNames.Navigate: |
127 | | - window.location.pathname = message.route; |
128 | | - // TODO: Figure out why this doesn't work, this is faster in theory |
129 | | - // navigate(message.route); |
130 | | - break; |
131 | | - case MessageTypeNames.McqQuestion: |
132 | | - dispatch(WorkspaceActions.showMcqPane(message.workspaceLocation, message.options)); |
133 | | - break; |
134 | | - case MessageTypeNames.McqAnswer: |
135 | | - console.log(`FRONTEND: MCQAnswerMessage: ${message}`); |
136 | | - dispatch(SessionActions.submitAnswer(message.questionId, message.choice)); |
137 | | - break; |
138 | | - case MessageTypeNames.AssessmentAnswer: |
139 | | - dispatch(SessionActions.submitAnswer(message.questionId, message.answer)); |
140 | | - break; |
141 | | - } |
142 | | - }); |
143 | | - // eslint-disable-next-line react-hooks/exhaustive-deps |
144 | | - }, []); |
| 74 | + useVscodeIntegration(); |
145 | 75 |
|
146 | 76 | return ( |
147 | 77 | <WorkspaceSettingsContext.Provider value={[workspaceSettings, setWorkspaceSettings]}> |
|
0 commit comments