|
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { useDispatch } from 'react-redux'; |
| 3 | +import SessionActions from 'src/commons/application/actions/SessionActions'; |
| 4 | +import VscodeActions from 'src/commons/application/actions/VscodeActions'; |
| 5 | +import WorkspaceActions from 'src/commons/workspace/WorkspaceActions'; |
| 6 | +import Messages, { |
| 7 | + MessageType, |
| 8 | + MessageTypeNames, |
| 9 | + sendToWebview |
| 10 | +} from 'src/features/vscode/messages'; |
| 11 | + |
| 12 | +/** |
| 13 | + * To handle messages from VS Code. |
| 14 | + */ |
| 15 | +export const useIsVscode = () => { |
| 16 | + const dispatch = useDispatch(); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const originalConfirm = window.confirm; |
| 20 | + |
| 21 | + if (!window.confirm) { |
| 22 | + // Polyfill confirm() to instead show as VS Code notification |
| 23 | + // TODO: Pass text as a new Message to the webview |
| 24 | + window.confirm = text => { |
| 25 | + console.log(`Confirmation automatically accepted: ${text ?? 'No text provided'}`); |
| 26 | + return true; |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + const message = Messages.ExtensionPing(window.origin); |
| 31 | + sendToWebview(message); |
| 32 | + |
| 33 | + const listener = (event: MessageEvent<MessageType>) => { |
| 34 | + const message = event.data; |
| 35 | + // Only accept messages from the vscode webview |
| 36 | + if (!event.origin.startsWith('vscode-webview://')) { |
| 37 | + return; |
| 38 | + } |
| 39 | + // console.log(`FRONTEND: Message from ${event.origin}: ${JSON.stringify(message)}`); |
| 40 | + switch (message.type) { |
| 41 | + case MessageTypeNames.ExtensionPong: |
| 42 | + console.log('Received WebviewStarted message, will set vsc'); |
| 43 | + dispatch(VscodeActions.setVscode()); |
| 44 | + |
| 45 | + if (message.token) { |
| 46 | + const token = JSON.parse(message.token.trim()); |
| 47 | + console.log(`FRONTEND: WebviewStarted: ${token}`); |
| 48 | + dispatch( |
| 49 | + SessionActions.setTokens({ |
| 50 | + accessToken: token.accessToken, |
| 51 | + refreshToken: token.refreshToken |
| 52 | + }) |
| 53 | + ); |
| 54 | + dispatch(SessionActions.fetchUserAndCourse()); |
| 55 | + } |
| 56 | + break; |
| 57 | + case MessageTypeNames.Text: |
| 58 | + const { workspaceLocation, code } = message; |
| 59 | + console.log(`FRONTEND: TextMessage: ${code}`); |
| 60 | + dispatch(WorkspaceActions.updateEditorValue(workspaceLocation, 0, code)); |
| 61 | + break; |
| 62 | + case MessageTypeNames.EvalEditor: |
| 63 | + dispatch(WorkspaceActions.evalEditor(message.workspaceLocation)); |
| 64 | + break; |
| 65 | + case MessageTypeNames.Navigate: |
| 66 | + window.location.pathname = message.route; |
| 67 | + // TODO: Figure out why this doesn't work, this is faster in theory |
| 68 | + // navigate(message.route); |
| 69 | + break; |
| 70 | + case MessageTypeNames.McqQuestion: |
| 71 | + dispatch(WorkspaceActions.showMcqPane(message.workspaceLocation, message.options)); |
| 72 | + break; |
| 73 | + case MessageTypeNames.McqAnswer: |
| 74 | + console.log(`FRONTEND: MCQAnswerMessage: ${message}`); |
| 75 | + dispatch(SessionActions.submitAnswer(message.questionId, message.choice)); |
| 76 | + break; |
| 77 | + case MessageTypeNames.AssessmentAnswer: |
| 78 | + dispatch(SessionActions.submitAnswer(message.questionId, message.answer)); |
| 79 | + break; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + window.addEventListener('message', listener); |
| 84 | + return () => { |
| 85 | + window.removeEventListener('message', listener); |
| 86 | + // Reset confirm to the original function |
| 87 | + window.confirm = originalConfirm; |
| 88 | + }; |
| 89 | + }, [dispatch]); |
| 90 | +}; |
0 commit comments