From cda682446ebe02762b3301c60ca0d633d5113fce Mon Sep 17 00:00:00 2001 From: Nieky Allen Date: Mon, 8 Jan 2024 23:40:07 -0600 Subject: [PATCH 1/3] cleanup types --- shared/types/drop.ts | 26 ++++++++++---------------- shared/types/grab.ts | 17 +++++++---------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/shared/types/drop.ts b/shared/types/drop.ts index f8e044c..614b76f 100644 --- a/shared/types/drop.ts +++ b/shared/types/drop.ts @@ -7,7 +7,7 @@ import type { import type { EventObject } from 'xstate/lib/types'; import type Peer from 'peerjs'; import type { DataConnection } from 'peerjs'; -import { DropEventType, MessageType } from '../lib/constants'; +import { DropEventType } from '../lib/constants'; export type DropOptions = { decryptedAccess?: 'copy' | 'view' | 'both'; @@ -23,11 +23,11 @@ export type DropContext = BaseContext & { dropKey: CryptoKey | null; }; -export type DropEvent = EventObject & { - type: DropEventType; +export type DropEvent = EventObject & { + type: EventType; }; -export type AnyDropEvent = DropEvent & { +export type AnyDropEvent = DropEvent & { [key: string]: any; }; @@ -39,29 +39,23 @@ export interface InitDropEvent extends AnyDropEvent { nonce: string; } -export interface ConnectEvent extends DropEvent { - type: DropEventType.Connect; +export interface ConnectEvent extends DropEvent { connection: DataConnection; } -export interface WrapEvent extends DropEvent { - type: DropEventType.Wrap; +export interface WrapEvent extends DropEvent { payload: Record; integrity: string; } -export interface HandshakeEvent extends DropEvent { - type: DropEventType.Handshake; -} +export interface HandshakeEvent extends DropEvent {} -export interface HandshakeCompleteEvent extends DropEvent { - type: DropEventType.HandshakeComplete; +export interface HandshakeCompleteEvent + extends DropEvent { dropKey: CryptoKey; } -export interface CompleteEvent extends DropEvent { - type: DropEventType.Confirm; -} +export interface CompleteEvent extends DropEvent {} export type DropHandlerInputs = BaseHandlerInputs< DropContext, diff --git a/shared/types/grab.ts b/shared/types/grab.ts index 96e7318..4b0692f 100644 --- a/shared/types/grab.ts +++ b/shared/types/grab.ts @@ -4,7 +4,7 @@ import type { DecryptFile, HashFile, } from './common'; -import { GrabEventType, MessageType } from '../lib/constants'; +import { GrabEventType } from '../lib/constants'; import type { EventObject } from 'xstate/lib/types'; import type Peer from 'peerjs'; @@ -13,16 +13,15 @@ export type GrabContext = BaseContext & { dropperId: string | null; }; -export type GrabEvent = EventObject & { - type: GrabEventType; +export type GrabEvent = EventObject & { + type: EventType; }; -export type AnyGrabEvent = GrabEvent & { +export type AnyGrabEvent = GrabEvent & { [key: string]: any; }; -export interface InitGrabEvent extends GrabEvent { - type: GrabEventType.Init; +export interface InitGrabEvent extends GrabEvent { id: string; dropperId: string; peer: Peer; @@ -30,13 +29,11 @@ export interface InitGrabEvent extends GrabEvent { nonce: string; } -export interface AckHandshakeEvent extends GrabEvent { - type: GrabEventType.Handshake; +export interface AckHandshakeEvent extends GrabEvent { grabKey: CryptoKey; } -export interface ExecuteGrabEvent extends GrabEvent { - type: GrabEventType.Grab; +export interface ExecuteGrabEvent extends GrabEvent { payload: string; } From d525848c10b56a25f007cbb0eaa5618233944ae1 Mon Sep 17 00:00:00 2001 From: Nieky Allen Date: Tue, 9 Jan 2024 17:41:21 -0600 Subject: [PATCH 2/3] attempt navblock --- web/hooks/use-drop.tsx | 25 +++++++++++++++++++++++-- web/hooks/use-grab.tsx | 22 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/web/hooks/use-drop.tsx b/web/hooks/use-drop.tsx index 0cacfb4..81aba24 100644 --- a/web/hooks/use-drop.tsx +++ b/web/hooks/use-drop.tsx @@ -1,8 +1,8 @@ import type { DropContext } from '@shared/types/drop'; -import { useMemo, useRef } from 'react'; +import { useEffect, useMemo, useRef } from 'react'; import { useMachine } from '@xstate/react'; import { dropMachine, initDropContext } from '@shared/lib/machines/drop'; -import { DropState, MessageType } from '@shared/lib/constants'; +import { DropState } from '@shared/lib/constants'; import { generateGrabUrl } from 'lib/util'; import { encryptFile, hashFile } from 'lib/crypto'; import { showNotification } from '@mantine/notifications'; @@ -10,8 +10,11 @@ import { IconX } from '@tabler/icons'; import { initPeer } from 'lib/peer'; import { createDropHandlers } from '@shared/handlers/drop'; import { cleanupSession } from 'lib/session'; +import { useRouter } from 'next/router'; export const useDrop = () => { + const router = useRouter(); + const logsRef = useRef>([]); const contextRef = useRef(initDropContext()); @@ -19,6 +22,24 @@ export const useDrop = () => { const pushLog = (message: string) => logsRef.current.push(message); + useEffect(() => { + const onLeaveAttempt = () => { + throw 'Cannot navigate away, peer active'; + }; + + if (state === DropState.Ready) { + router.events.on('routeChangeStart', onLeaveAttempt); + } else if ( + [DropState.Completed, DropState.Error].includes(state as DropState) + ) { + router.events.off('routeChangeStart', onLeaveAttempt); + } + + return () => { + router.events.off('routeChangeStart', onLeaveAttempt); + }; + }, [state]); + const onRetryExceeded = () => { showNotification({ message: 'Connection may be unstable, please try again', diff --git a/web/hooks/use-grab.tsx b/web/hooks/use-grab.tsx index 32439f8..49928e9 100644 --- a/web/hooks/use-grab.tsx +++ b/web/hooks/use-grab.tsx @@ -1,8 +1,8 @@ import { grabMachine, initGrabContext } from '@shared/lib/machines/grab'; import { useMachine } from '@xstate/react'; import type { GrabContext } from '@shared/types/grab'; -import { GrabState, MessageType } from '@shared/lib/constants'; -import { useMemo, useRef } from 'react'; +import { GrabState } from '@shared/lib/constants'; +import { useEffect, useMemo, useRef } from 'react'; import { useRouter } from 'next/router'; import { decryptFile, hashFile } from 'lib/crypto'; import { showNotification } from '@mantine/notifications'; @@ -19,6 +19,24 @@ export const useGrab = () => { const [{ value: state }, send] = useMachine(grabMachine); + useEffect(() => { + const onLeaveAttempt = () => { + throw 'Cannot navigate away, peer active'; + }; + + if (state === GrabState.Ready) { + router.events.on('routeChangeStart', onLeaveAttempt); + } else if ( + [GrabState.Completed, GrabState.Error].includes(state as GrabState) + ) { + router.events.off('routeChangeStart', onLeaveAttempt); + } + + return () => { + router.events.off('routeChangeStart', onLeaveAttempt); + }; + }, [state]); + const pushLog = (message: string) => logsRef.current.push(message); const onRetryExceeded = () => { From 5f83f2016ab2f2c1efa95d0509cbff357f53e2e1 Mon Sep 17 00:00:00 2001 From: Nieky Allen Date: Fri, 9 Feb 2024 23:01:16 -0600 Subject: [PATCH 3/3] add shared workspace script alias and add code analysis script --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a113c14..3f3aed6 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,14 @@ "scripts": { "web": "yarn workspace web", "cli": "yarn workspace cli", + "shared": "yarn workspace shared", "start": "yarn web start", "build": "yarn web build", "start:prod": "yarn web start:prod", "test:e2e": "yarn web test:e2e", "cli:build": "yarn cli build", - "cli:release": "cd cli && yarn install --frozen-lockfile && yarn release" + "cli:release": "cd cli && yarn install --frozen-lockfile && yarn release", + "analyze:dup": "npx jscpd shared cli web --format \"typescript\"" }, "dependencies": { "@xstate/react": "^3.0.1",