Skip to content

Commit

Permalink
progress on grab MVP for vscode sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Oct 30, 2023
1 parent 377a36d commit 02a3b33
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
2 changes: 2 additions & 0 deletions vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@typescript-eslint/parser": "^5.45.0",
"@vscode/test-electron": "^2.2.0",
"dotenv": "^16.0.3",
"dotenv-webpack": "^8.0.1",
"eslint": "^8.28.0",
"glob": "^8.0.3",
"mocha": "^10.1.0",
Expand All @@ -74,6 +75,7 @@
},
"dependencies": {
"axios": "^1.3.4",
"buffer": "^6.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
61 changes: 37 additions & 24 deletions vscode-extension/src/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
import { useRef, useState } from 'react';
import { useMemo, useRef, useState } from 'react';
import './base.css';
import { DropDetails } from '@shared/types/common';
import { generateId } from '@shared/lib/util';
import { useMachine } from '@xstate/react/lib/useMachine';
import { GrabContext } from '@shared/types/grab';
import { grabMachine, initGrabContext } from '@shared/lib/machines/grab';
import { MessageType } from '@shared/lib/constants';
import { createGrabHandlers } from '@shared/handlers/grab';
import { initPeer } from './lib/peer';
import { decryptFile, hashFile } from '@shared/lib/crypto/browser';

export function Sidebar() {
const [mode, setMode] = useState<string | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const [peerId, setPeerId] = useState<string>();
const contextRef = useRef<GrabContext>(initGrabContext());
const timersRef = useRef(new Map<MessageType, NodeJS.Timeout>());

const connect = async () => {
const id = generateId();
const [{ value: state }, send] = useMachine(grabMachine);

const { initPeer } = await import('./lib/peer');

const peer = await initPeer(id);

console.log(peer);
};
const { init } = useMemo(
() =>
createGrabHandlers({
ctx: contextRef.current,
timers: timersRef.current,
sendEvent: send,
logger: {
info: console.info,
error: console.error,
debug: console.debug,
},
file: {
decrypt: decryptFile,
hash: hashFile,
},
initPeer,
cleanupSession: () => {},
apiUri: process.env.REACT_APP_DEADDROP_API_URL!,
}),
[],
);

const startGrab = async () => {
const dropId = inputRef.current!.value;

const res = await fetch(
`${process.env.REACT_APP_DEADDROP_API_URL!}?id=${dropId}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
},
);

const data = await res.json();
contextRef.current.id = dropId;

setPeerId(data.peerId);
await init();
};

return (
Expand All @@ -42,7 +53,9 @@ export function Sidebar() {
{mode === 'drop' ? (
<>
<p>dropping</p>
<button onClick={connect}>connect</button>
<button onClick={() => console.log('CONNECT')}>
connect
</button>
</>
) : mode === 'grab' ? (
<>
Expand Down
4 changes: 2 additions & 2 deletions vscode-extension/src/lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { createPeer } from '@shared/lib/peer';

// NOTE may need shim

export const initPeer = (id: string) =>
createPeer(id, process.env.REACT_APP_PEER_SERVER_URL!);
export const initPeer = () =>
createPeer(process.env.REACT_APP_PEER_SERVER_URL!);
7 changes: 3 additions & 4 deletions vscode-extension/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const DotenvWebpackPlugin = require('dotenv-webpack');

/**@type {import('webpack').Configuration}*/
const config = {
Expand All @@ -19,9 +19,7 @@ const config = {
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
plugins: [
new TsconfigPathsPlugin({ configFile: './tsconfig.json' }),
],
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })],
},
module: {
rules: [
Expand All @@ -41,6 +39,7 @@ const config = {
},
],
},
plugins: [new DotenvWebpackPlugin()],
};

module.exports = config;

0 comments on commit 02a3b33

Please sign in to comment.