Skip to content

Commit

Permalink
Add about cph page.
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawal-d committed Dec 24, 2024
1 parent 2009906 commit 9898c66
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 15 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"eslint.enable": true
"eslint.enable": true,
"files.associations": {
"iostream": "cpp"
},
"editor.formatOnSave": true
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-3.0-or-later",
"icon": "icon.png",
"publisher": "DivyanshuAgrawal",
"version": "2024.6.1717519178",
"version": "2077.0.0",
"engines": {
"vscode": "^1.52.0"
},
Expand Down Expand Up @@ -405,4 +405,4 @@
"type": "git",
"url": "https://github.com/agrawal-d/competitive-programming-helper/"
}
}
}
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ export const checkUnsupported = (srcPath: string): boolean => {
export const deleteProblemFile = (srcPath: string) => {
globalThis.reporter.sendTelemetryEvent(telmetry.DELETE_ALL_TESTCASES);
const probPath = getProbSaveLocation(srcPath);
console.log('Deleting problem file', probPath);
try {
if (platform() === 'win32') {
spawn('del', [probPath]);
spawn('cmd.exe', ['/c', 'del', probPath]);
} else {
spawn('rm', [probPath]);
}
Expand Down
11 changes: 11 additions & 0 deletions src/webview/JudgeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ class JudgeViewProvider implements vscode.WebviewViewProvider {
vscode.Uri.joinPath(this._extensionUri, 'dist', 'codicon.css'),
);

const generatedJsonUri = webview.asWebviewUri(
vscode.Uri.joinPath(
this._extensionUri,
'dist',
'static',
'generated.json',
),
);

const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(
this._extensionUri,
Expand Down Expand Up @@ -233,6 +242,8 @@ class JudgeViewProvider implements vscode.WebviewViewProvider {
// So, for the initial request, ask for it again.
window.vscodeApi = acquireVsCodeApi();
window.remoteMessage = '${remoteMessage}';
window.generatedJsonUri = '${generatedJsonUri}';
document.addEventListener(
'DOMContentLoaded',
(event) => {
Expand Down
96 changes: 87 additions & 9 deletions src/webview/frontend/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import {
WebViewpersistenceState,
} from '../../types';
import CaseView from './CaseView';
import Page from './Page';

declare const vscodeApi: {
postMessage: (message: WebviewToVSEvent) => void;
getState: () => WebViewpersistenceState | undefined;
setState: (state: WebViewpersistenceState) => void;
};

interface CustomWindow extends Window {
generatedJsonUri: string;
remoteMessage: string | null;
}
declare const window: CustomWindow;

// Original: www.paypal.com/ncp/payment/CMLKCFEJEMX5L
const payPalUrl = 'https://rb.gy/5iiorz';

Expand All @@ -37,6 +45,18 @@ function Judge(props: {
const [notification, setNotification] = useState<string | null>(null);
const [waitingForSubmit, setWaitingForSubmit] = useState<boolean>(false);
const [onlineJudgeEnv, setOnlineJudgeEnv] = useState<boolean>(false);
const [showInfoPage, setShowInfoPage] = useState<boolean>(false);
const [generatedJson, setGeneratedJson] = useState<any | null>(null);

useEffect(() => {
fetch(window.generatedJsonUri)
.then((res) => res.json())
.then((data) => setGeneratedJson(data))
.catch((err) =>
console.error('Failed to fetch generated JSON', err),
);
}, []);

const [webviewState, setWebviewState] = useState<WebViewpersistenceState>(
() => {
const vscodeState = vscodeApi.getState();
Expand Down Expand Up @@ -415,10 +435,73 @@ function Judge(props: {
);
};

const renderInfoPage = () => {
if (showInfoPage === false) {
return null;
}

if (generatedJson === null) {
return (
<Page
content="Loading..."
title="About CPH"
closePage={() => setShowInfoPage(false)}
/>
);
}
const contents = (
<div>
<h3>🤖 Enable AI compilation</h3>
Get 100x faster compilation using AI, please opt-in below. Your
data will be used to train cats to write JavaScript.
<br />
<br />
<button
className="btn btn-green"
onClick={(e) => {
const target = e.target as HTMLButtonElement;
target.innerText = '🪄 AI training ...';
}}
>
Enable
</button>
<hr />
<h3>Get Help</h3>
<a
className="btn"
href="https://github.com/agrawal-d/cph/blob/main/docs/user-guide.md"
>
User guide
</a>
<hr />
<h3>Commit</h3>
<pre className="selectable">{generatedJson.gitCommitHash}</pre>
<hr />
<h3>Build Time</h3>
{generatedJson.dateTime}
<hr />
<h3>License</h3>
<pre className="selectable">{generatedJson.licenseString}</pre>
<hr />
Created by Divyanshu Agrawal
<hr />
</div>
);

return (
<Page
content={contents}
title="About CPH"
closePage={() => setShowInfoPage(false)}
/>
);
};

return (
<div className="ui">
{notification && <div className="notification">{notification}</div>}
{renderDonateButton()}
{renderInfoPage()}
<div className="meta">
<h1 className="problem-name">
<a href={getHref()}>{problem.name}</a>{' '}
Expand Down Expand Up @@ -520,18 +603,13 @@ function Judge(props: {
</button>
<button
className="btn"
title="Help"
onClick={() =>
sendMessageToVSCode({
command: 'url',
url: 'https://github.com/agrawal-d/cph/blob/main/docs/user-guide.md',
})
}
title="Info"
onClick={() => setShowInfoPage(true)}
>
<span className="icon">
<i className="codicon codicon-question"></i>
<i className="codicon codicon-info"></i>
</span>{' '}
<span className="action-text">Help</span>
<span className="action-text"></span>
</button>
<button
className="btn btn-red right"
Expand Down
22 changes: 22 additions & 0 deletions src/webview/frontend/Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

export default function Page(props: {
content: React.ReactNode;
title: string;
closePage: () => void;
}) {
return (
<div className="page selectable">
<a
className="right"
title="Close dialog"
onClick={() => props.closePage()}
>
<i className="codicon codicon-close"></i>
</a>
<h2>{props.title}</h2>
<hr />
{props.content}
</div>
);
}
29 changes: 28 additions & 1 deletion src/webview/frontend/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ button:disabled {
opacity: 0.5;
font-size: 80%;
}

.expectedoutput {
filter: grayscale(100%);
position: absolute;
Expand Down Expand Up @@ -468,6 +469,32 @@ body.vscode-light .case-number {
display: inline;
}

.page {
position: fixed;
top: 0px;
left: 0px;
z-index: 1000;
background-color: var(--vscode-panel-background);
width: 100%;
height: 100%;
padding: 10px;
}

.page pre {
color: var(--vscode-menu-foreground);
background-color: var(--vscode-menu-background);
padding: 2px;
border: 0px;
margin: 5px 0px;
overflow: auto;
}

hr {
border: none;
border-top: 1px solid var(--vscode-menu-border);
margin: 10px -10px;
}

@media only screen and (max-width: 400px) {
.actions {
border-top: 1px solid rgba(0, 0, 0, 0.5);
Expand All @@ -486,4 +513,4 @@ body.vscode-light .case-number {
.fallback {
display: block !important;
}
}
}
2 changes: 1 addition & 1 deletion static/remote-message.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Now also for Google Chrome: Submit directly to Codeforces using <a href="https://github.com/agrawal-d/cph-submit">CPH-Submit</a>!
🎆 Happy new year!
23 changes: 23 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const child_process = require('child_process');
const fs = require('fs');


/**@type {import('webpack').Configuration}*/
const config = {
Expand Down Expand Up @@ -38,6 +41,26 @@ const config = {
],
},
plugins: [
{
apply: (compiler) => {
compiler.hooks.compile.tap("CompileTimeData", () => {
const gitCommitHash = child_process.execSync('git rev-parse HEAD').toString().trim() || "unknown git commit hash";
const licenseString = fs.readFileSync(path.join(__dirname, 'LICENSE'), 'utf8').toString().trim() || "unknown license";
const dateTime = new Date().toISOString();
const generatedDict = {
gitCommitHash,
licenseString,
dateTime,
};

const generatedJsonPath = path.join(__dirname, 'dist', 'static', 'generated.json');
console.log(`Writing generated.json to ${generatedJsonPath}`);
fs.mkdirSync(path.dirname(generatedJsonPath), { recursive: true });
fs.writeFileSync(generatedJsonPath, JSON.stringify(generatedDict, null, 2));
});

},
},
new CopyPlugin({
patterns: [
{
Expand Down

0 comments on commit 9898c66

Please sign in to comment.