-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
263 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { FC, useCallback, useState } from 'react'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
import { formatPercentage } from '@/utils/number-format'; | ||
import useSocket from '@/hooks/socket'; | ||
|
||
const STEPS_ORDER = [ | ||
'VALIDATING_DATA', | ||
'GEOCODING', | ||
'IMPORTING_DATA', | ||
'CALCULATING_IMPACT', | ||
] as const; | ||
|
||
const STEPS_NAMES = { | ||
VALIDATING_DATA: 'Validating Data', | ||
IMPORTING_DATA: 'Importing Data', | ||
GEOCODING: 'Geocoding', | ||
CALCULATING_IMPACT: 'Calculating Impact', | ||
} as const; | ||
|
||
type ProgressTask = { | ||
kind: 'DATA_IMPORT_PROGRESS'; | ||
data: Record< | ||
keyof typeof STEPS_NAMES, | ||
{ | ||
status: 'processing' | 'idle' | 'completed'; | ||
progress: number; | ||
} | ||
>; | ||
}; | ||
|
||
export const UploadTracker: FC = () => { | ||
const queryClient = useQueryClient(); | ||
const [tasksProgress, setTaskProgress] = useState<ProgressTask['data']>(undefined); | ||
|
||
const onProgress = useCallback(({ data }: ProgressTask) => { | ||
setTaskProgress(data); | ||
}, []); | ||
|
||
const onSettle = useCallback(() => { | ||
queryClient.invalidateQueries(['tasks']); | ||
queryClient.invalidateQueries(['sourcingLocations']); | ||
}, [queryClient]); | ||
|
||
useSocket({ | ||
DATA_IMPORT_PROGRESS: onProgress, | ||
DATA_IMPORT_COMPLETED: onSettle, | ||
DATA_IMPORT_FAILURE: onSettle, | ||
}); | ||
|
||
// if (!Object.keys(tasksProgress || {}).length) return null; | ||
|
||
return ( | ||
<div className="w-full rounded-3xl bg-white px-8 py-10"> | ||
<div className="grid grid-cols-4 gap-1"> | ||
{Object.keys(STEPS_NAMES) | ||
.sort( | ||
(a, b) => | ||
STEPS_ORDER.indexOf(a as keyof typeof STEPS_NAMES) - | ||
STEPS_ORDER.indexOf(b as keyof typeof STEPS_NAMES), | ||
) | ||
.map((key: keyof typeof STEPS_NAMES) => ( | ||
<div className="flex flex-col items-start" key={key}> | ||
<span | ||
className={cn('text-base text-gray-400 transition-colors', { | ||
'text-gray-900': ['processing', 'completed'].includes( | ||
tasksProgress?.[key]?.status, | ||
), | ||
})} | ||
> | ||
{STEPS_NAMES[key]} | ||
</span> | ||
<span className="text-sm text-gray-500">{`Progress: ${ | ||
tasksProgress?.[key]?.progress | ||
? formatPercentage(tasksProgress[key].progress / 100) | ||
: '–' | ||
}`}</span> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UploadTracker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { io, SocketOptions, Socket } from 'socket.io-client'; | ||
import { useSession } from 'next-auth/react'; | ||
|
||
import { env } from '@/env.mjs'; | ||
|
||
const useSocket = ( | ||
events: { [key: string]: (...args: unknown[]) => void } = {}, | ||
options?: SocketOptions, | ||
) => { | ||
const socketRef = useRef<Socket | undefined>(undefined); | ||
const { data: { accessToken = undefined } = {} } = useSession(); | ||
|
||
useEffect(() => { | ||
const socket = socketRef.current; | ||
|
||
if (socket || !accessToken) return () => {}; | ||
|
||
socketRef.current = io(env.NEXT_PUBLIC_API_URL, { | ||
transports: ['websocket'], | ||
extraHeaders: { | ||
authorization: accessToken, | ||
}, | ||
...options, | ||
}); | ||
|
||
return () => { | ||
socketRef.current.disconnect(); | ||
}; | ||
}, [accessToken, options]); | ||
|
||
useEffect(() => { | ||
const socket = socketRef.current; | ||
|
||
if (!socket) return () => {}; | ||
|
||
Object.entries(events).forEach(([event, handler]) => { | ||
socket.on(event, handler); | ||
}); | ||
|
||
return () => { | ||
Object.entries(events).forEach(([event, handler]) => { | ||
socket.off(event, handler); | ||
}); | ||
}; | ||
}, [events]); | ||
|
||
return socketRef.current; | ||
}; | ||
|
||
export default useSocket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.