-
Notifications
You must be signed in to change notification settings - Fork 2
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
6 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
46 changes: 27 additions & 19 deletions
46
frontend/components/Layout/InitializationStatusChecker.tsx
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 |
---|---|---|
@@ -1,44 +1,52 @@ | ||
import { FC, useContext, useEffect, useState } from "react" | ||
|
||
import { NotificationContext } from "@/context/NotificationContext" | ||
import { Notification, NotificationContext } from "@/context/NotificationContext" | ||
import { useLocalStorage } from "@/hooks/useLocalStorage" | ||
import { useInitializationStatus } from "@/repositories/initializationStatusRepository" | ||
import { usePid } from "@/repositories/pidRepository" | ||
|
||
const INITIAL_KEY = 'InitializationStatusChecker-closed' | ||
|
||
export const InitializationStatusChecker: FC = () => { | ||
const { setNotification } = useContext(NotificationContext) | ||
|
||
const [closed, setClosed] = useState<boolean>(false) | ||
const { pid } = usePid() | ||
const key = pid ? `InitializationStatusChecker-closed-${pid}` : INITIAL_KEY | ||
const [closed, setClosed] = useLocalStorage<boolean>(key, false) | ||
const [initialized, setInitialized] = useState<boolean>(false) | ||
|
||
// Stop loading if initialization process is finished | ||
const { initializationStatus } = useInitializationStatus((initialized || closed) ? 0 : 100) | ||
|
||
useEffect(() => { | ||
if (!initializationStatus) return; | ||
if (!initializationStatus || !pid) return; | ||
|
||
let notification: Notification | undefined = undefined | ||
|
||
if (initializationStatus.total === initializationStatus.loaded) { | ||
setInitialized(true) | ||
|
||
if (!closed) { | ||
setNotification( | ||
{ | ||
type: 'success', | ||
message: `Successfully loaded ${initializationStatus.loaded} definitions!`, | ||
onClose: () => setClosed(true) | ||
} | ||
) | ||
notification = { | ||
type: 'success', | ||
message: `Successfully loaded ${initializationStatus.loaded} definitions!`, | ||
onClose: () => setClosed(true) | ||
} | ||
} else if (!closed) { | ||
const progress = Math.round(initializationStatus.loaded / initializationStatus.total * 100) | ||
|
||
setNotification( | ||
{ | ||
type: 'info', | ||
message: `Loading definitions... ${progress}% (${initializationStatus.loaded}/${initializationStatus.total})`, | ||
onClose: () => setClosed(true) | ||
} | ||
) | ||
notification = { | ||
type: 'info', | ||
message: `Loading definitions... ${progress}% (${initializationStatus.loaded}/${initializationStatus.total})`, | ||
onClose: () => setClosed(true) | ||
} | ||
} | ||
|
||
if (closed) { | ||
setNotification(null) | ||
} else if (notification) { | ||
setNotification(notification) | ||
} | ||
}, [closed, initializationStatus, setNotification]) | ||
}, [pid, closed, setClosed, initializationStatus, setNotification]) | ||
|
||
return null | ||
} |
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,18 @@ | ||
import useSWR from 'swr' | ||
|
||
import { path } from '@/constants/path' | ||
|
||
import { get } from './httpRequest' | ||
|
||
type PidReponse = { | ||
pid: number | ||
} | ||
|
||
export const usePid = () => { | ||
const { data } = useSWR(path.api.pid(), async (): Promise<number> => { | ||
const response = await get<PidReponse>(path.api.pid()) | ||
return response.pid | ||
}) | ||
|
||
return { pid: data } | ||
} |
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