From 69a8091b1d5f46fbae1dbe8b572672713b99f2d8 Mon Sep 17 00:00:00 2001 From: Divyanshu Agrawal Date: Mon, 22 Jul 2024 19:07:12 +0530 Subject: [PATCH] Add remote message --- src/config.ts | 3 +++ src/extension.ts | 18 ++++++++++++++++++ src/types.ts | 7 +++++++ src/webview/JudgeView.ts | 1 + src/webview/frontend/App.tsx | 27 ++++++++++----------------- src/webview/frontend/app.css | 5 +++++ 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/config.ts b/src/config.ts index 70161ac..6cd223d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,9 @@ */ export default { + remoteMessageUrl: new URL( + 'https://raw.githubusercontent.com/agrawal-d/cph/main/static/remote-message.txt', + ), telemetryKey: '', port: 27121, // companion listener server timeout: 10000, // for a testcase run diff --git a/src/extension.ts b/src/extension.ts index 1a735f8..6931862 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -73,6 +73,8 @@ export function activate(context: vscode.ExtensionContext) { console.log('cph: activate() execution started'); globalThis.context = context; + downloadRemoteMessage(); + const statusBarItem = vscode.window.createStatusBarItem( vscode.StatusBarAlignment.Left, 1000, @@ -106,3 +108,19 @@ export function activate(context: vscode.ExtensionContext) { return; } + +async function downloadRemoteMessage() { + try { + console.log('Fetching remote message'); + globalThis.remoteMessage = await ( + await fetch(config.remoteMessageUrl) + ).text(); + getJudgeViewProvider().extensionToJudgeViewMessage({ + command: 'remote-message', + message: globalThis.remoteMessage, + }); + console.log('Remote message fetched', globalThis.remoteMessage); + } catch (e) { + console.error('Error fetching remote message', e); + } +} diff --git a/src/types.ts b/src/types.ts index 533429c..a881498 100644 --- a/src/types.ts +++ b/src/types.ts @@ -202,6 +202,11 @@ export type NewProblemCommand = { problem: Problem | undefined; }; +export type RemoteMessageCommand = { + command: 'remote-message'; + message: string; +}; + export type VSToWebViewMessage = | ResultCommand | RunningCommand @@ -211,6 +216,7 @@ export type VSToWebViewMessage = | WaitingForSubmitCommand | SubmitFinishedCommand | NotRunningCommand + | RemoteMessageCommand | NewProblemCommand; export type CphEmptyResponse = { @@ -232,4 +238,5 @@ export type WebViewpersistenceState = { declare global { var reporter: TelemetryReporter; var context: vscode.ExtensionContext; + var remoteMessage: string | undefined; } diff --git a/src/webview/JudgeView.ts b/src/webview/JudgeView.ts index 38ed362..cdb2e23 100644 --- a/src/webview/JudgeView.ts +++ b/src/webview/JudgeView.ts @@ -228,6 +228,7 @@ class JudgeViewProvider implements vscode.WebviewViewProvider { // Since the react script takes time to load, the problem is sent to the webview before it has even loaded. // So, for the initial request, ask for it again. window.vscodeApi = acquireVsCodeApi(); + window.remoteMessage = ${globalThis.remoteMessage}; document.addEventListener( 'DOMContentLoaded', (event) => { diff --git a/src/webview/frontend/App.tsx b/src/webview/frontend/App.tsx index d971a60..6af6fa3 100644 --- a/src/webview/frontend/App.tsx +++ b/src/webview/frontend/App.tsx @@ -37,7 +37,6 @@ function Judge(props: { const [notification, setNotification] = useState(null); const [waitingForSubmit, setWaitingForSubmit] = useState(false); const [onlineJudgeEnv, setOnlineJudgeEnv] = useState(false); - const [remoteMessage, setRemoteMessage] = useState(''); const [webviewState, setWebviewState] = useState( () => { const vscodeState = vscodeApi.getState(); @@ -75,21 +74,6 @@ function Judge(props: { vscodeApi.postMessage(message); }; - useEffect(() => { - console.log('Fetching remote text message'); - const url = - 'https://github.com/agrawal-d/cph/raw/main/static/remote-message.txt'; - try { - fetch(url, { mode: 'no-cors' }).then((response) => { - response.text().then((text) => { - setRemoteMessage(text); - }); - }); - } catch (err) { - console.error('Error fetching remote-message.txt: ', err); - } - }, []); - useEffect(() => { const fn = (event: any) => { const data: VSToWebViewMessage = event.data; @@ -99,6 +83,11 @@ function Judge(props: { break; } + case 'remote-message': { + window.remoteMessage = data.message; + break; + } + case 'running': { handleRunning(data); break; @@ -488,7 +477,11 @@ function Judge(props: {
-

{remoteMessage}

+

diff --git a/src/webview/frontend/app.css b/src/webview/frontend/app.css index 8ed145d..4cd5934 100644 --- a/src/webview/frontend/app.css +++ b/src/webview/frontend/app.css @@ -88,6 +88,11 @@ body { border-radius: 4px; } +.remote-message a { + text-decoration: underline; + color: var(--vscode-textLink-foreground); +} + a.btn { display: inline-block; }