Skip to content

Commit

Permalink
feat: add view proof
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino committed May 16, 2024
1 parent 36451be commit 50f6d2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 55 deletions.
53 changes: 31 additions & 22 deletions src/entries/Background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export enum BackgroundActiontype {
run_plugin = 'run_plugin',
get_plugin_hashes = 'get_plugin_hashes',
open_popup = 'open_popup',
change_route = 'change_route',
}

export type BackgroundAction = {
Expand Down Expand Up @@ -105,7 +106,6 @@ export type RequestHistory = {
export const initRPC = () => {
browser.runtime.onMessage.addListener(
async (request, sender, sendResponse) => {
console.log(request);
switch (request.type) {
case BackgroundActiontype.get_requests:
return handleGetRequests(request, sendResponse);
Expand Down Expand Up @@ -487,29 +487,38 @@ async function handleRunPlugin(
return JSON.parse(out.string());
}

let cachePopup: browser.Windows.Window | null = null;

async function handleOpenPopup(request: BackgroundAction) {
console.log('opening popup');
const tab = await browser.tabs.create({
url: browser.runtime.getURL('popup.html'),
active: false,
});
if (cachePopup) {
browser.windows.update(cachePopup.id!, {
focused: true,
});
} else {
const tab = await browser.tabs.create({
url: browser.runtime.getURL('popup.html') + '#' + request.data.route,
active: false,
});

console.log(request.data);
const popup = await browser.windows.create({
tabId: tab.id,
type: 'popup',
focused: true,
width: 480,
height: 640,
left: request.data.position.left,
top: request.data.position.top,
});
const popup = await browser.windows.create({
tabId: tab.id,
type: 'popup',
focused: true,
width: 480,
height: 640,
left: request.data.position.left,
top: request.data.position.top,
});

cachePopup = popup;

// browser.action.openPopup({
// width: 480,
// height: 640,
// })
const onPopUpClose = (windowId: number) => {
if (windowId === popup.id) {
cachePopup = null;
browser.windows.onRemoved.removeListener(onPopUpClose);
}
};

console.log(popup);
return popup;
browser.windows.onRemoved.addListener(onPopUpClose);
}
}
24 changes: 24 additions & 0 deletions src/entries/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ProofViewer from '../../pages/ProofViewer';
import History from '../../pages/History';
import ProofUploader from '../../pages/ProofUploader';
import browser from 'webextension-polyfill';
import store from '../../utils/store';

const Popup = () => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -49,6 +50,29 @@ const Popup = () => {
})();
}, []);

useEffect(() => {
chrome.runtime.onMessage.addListener((request) => {
console.log(request);
switch (request.type) {
case BackgroundActiontype.push_action: {
if (
request.data.tabId === store.getState().requests.activeTab?.id ||
request.data.tabId === 'background'
) {
store.dispatch(request.action);
}
break;
}
case BackgroundActiontype.change_route: {
if (request.data.tabId === 'background') {
navigate(request.route);
break;
}
}
}
});
}, []);

return (
<div className="flex flex-col w-full h-full overflow-hidden">
<div className="flex flex-nowrap flex-shrink-0 flex-row items-center relative gap-2 h-9 p-2 cursor-default justify-center bg-slate-300 w-full">
Expand Down
15 changes: 0 additions & 15 deletions src/entries/Popup/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { HashRouter } from 'react-router-dom';

import Popup from './Popup';
import './index.scss';
import { Provider } from 'react-redux';
Expand All @@ -11,20 +10,6 @@ import { BackgroundActiontype } from '../Background/rpc';
const container = document.getElementById('app-container');
const root = createRoot(container!); // createRoot(container!) if you use TypeScript

chrome.runtime.onMessage.addListener((request) => {
switch (request.type) {
case BackgroundActiontype.push_action: {
if (
request.data.tabId === store.getState().requests.activeTab?.id ||
request.data.tabId === 'background'
) {
store.dispatch(request.action);
}
break;
}
}
});

root.render(
<Provider store={store}>
<HashRouter>
Expand Down
34 changes: 16 additions & 18 deletions src/entries/SidePanel/SidePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import './sidePanel.scss';
import browser from 'webextension-polyfill';
import {
fetchPluginConfigByHash,
getCookiesByHost,
runPlugin,
} from '../../utils/rpc';
import { fetchPluginConfigByHash, runPlugin } from '../../utils/rpc';
import { PluginConfig, StepConfig } from '../../utils/misc';
import { PluginList } from '../../components/PluginList';
import DefaultPluginIcon from '../../assets/img/default-plugin-icon.png';
Expand Down Expand Up @@ -43,7 +39,6 @@ export default function SidePanel(): ReactElement {
</div>
{!config && <PluginList />}
{config && <PluginBody hash={hash} config={config} />}
{/*<PluginList />*/}
</div>
);
}
Expand Down Expand Up @@ -110,7 +105,6 @@ function StepContent(
hash,
setResponse,
lastResponse,
responses,
prover,
} = props;
const [completed, setCompleted] = useState(false);
Expand Down Expand Up @@ -153,32 +147,29 @@ function StepContent(

const viewProofInPopup = useCallback(async () => {
if (!notaryRequest) return;
const popup = await browser.runtime.sendMessage({
chrome.runtime.sendMessage<any, string>({
type: BackgroundActiontype.verify_prove_request,
data: notaryRequest,
});
await browser.runtime.sendMessage({
type: BackgroundActiontype.open_popup,
data: {
position: {
left: window.screen.width / 2 - 240,
top: window.screen.height / 2 - 300,
},
route: `/verify/${notaryRequest.id}`,
},
});
console.log(popup);
}, [notaryRequest]);
}, [notaryRequest, notarizationId]);

useEffect(() => {
processStep();
}, [processStep]);

let btnContent = null;

if (notaryRequest?.status === 'pending' || pending) {
btnContent = (
<button className="button mt-2 w-fit flex flex-row flex-nowrap items-center gap-2 cursor-default">
<Icon className="animate-spin" fa="fa-solid fa-spinner" size={1} />
<span className="text-sm">{cta}</span>
</button>
);
} else if (completed) {
if (completed) {
btnContent = (
<button
className={classNames(
Expand All @@ -201,6 +192,13 @@ function StepContent(
<span className="text-sm">View Proof</span>
</button>
);
} else if (notaryRequest?.status === 'pending' || pending || notarizationId) {
btnContent = (
<button className="button mt-2 w-fit flex flex-row flex-nowrap items-center gap-2 cursor-default">
<Icon className="animate-spin" fa="fa-solid fa-spinner" size={1} />
<span className="text-sm">{cta}</span>
</button>
);
} else {
btnContent = (
<button
Expand Down

0 comments on commit 50f6d2d

Please sign in to comment.