Skip to content

Commit

Permalink
attempt navblock
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jan 9, 2024
1 parent cda6824 commit d525848
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
25 changes: 23 additions & 2 deletions web/hooks/use-drop.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
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';
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<Array<string>>([]);
const contextRef = useRef<DropContext>(initDropContext());

const [{ value: state }, send] = useMachine(dropMachine);

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',
Expand Down
22 changes: 20 additions & 2 deletions web/hooks/use-grab.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = () => {
Expand Down

0 comments on commit d525848

Please sign in to comment.