Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote message for Chrome CPH-Submit #464

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ export type NewProblemCommand = {
problem: Problem | undefined;
};

export type RemoteMessageCommand = {
command: 'remote-message';
message: string;
};

export type VSToWebViewMessage =
| ResultCommand
| RunningCommand
Expand All @@ -211,6 +216,7 @@ export type VSToWebViewMessage =
| WaitingForSubmitCommand
| SubmitFinishedCommand
| NotRunningCommand
| RemoteMessageCommand
| NewProblemCommand;

export type CphEmptyResponse = {
Expand All @@ -232,4 +238,5 @@ export type WebViewpersistenceState = {
declare global {
var reporter: TelemetryReporter;
var context: vscode.ExtensionContext;
var remoteMessage: string | undefined;
}
1 change: 1 addition & 0 deletions src/webview/JudgeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
27 changes: 10 additions & 17 deletions src/webview/frontend/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function Judge(props: {
const [notification, setNotification] = useState<string | null>(null);
const [waitingForSubmit, setWaitingForSubmit] = useState<boolean>(false);
const [onlineJudgeEnv, setOnlineJudgeEnv] = useState<boolean>(false);
const [remoteMessage, setRemoteMessage] = useState<string>('');
const [webviewState, setWebviewState] = useState<WebViewpersistenceState>(
() => {
const vscodeState = vscodeApi.getState();
Expand Down Expand Up @@ -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;
Expand All @@ -99,6 +83,11 @@ function Judge(props: {
break;
}

case 'remote-message': {
window.remoteMessage = data.message;
break;
}

case 'running': {
handleRunning(data);
break;
Expand Down Expand Up @@ -488,7 +477,11 @@ function Judge(props: {
</small>
</div>
<div className="remote-message">
<p>{remoteMessage}</p>
<p
dangerouslySetInnerHTML={{
__html: window.remoteMessage || '',
}}
/>
</div>
</div>
<div className="actions">
Expand Down
5 changes: 5 additions & 0 deletions src/webview/frontend/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ body {
border-radius: 4px;
}

.remote-message a {
text-decoration: underline;
color: var(--vscode-textLink-foreground);
}

a.btn {
display: inline-block;
}
Expand Down
Loading