-
Notifications
You must be signed in to change notification settings - Fork 131
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
1 parent
1a0c169
commit 0ce8c1f
Showing
15 changed files
with
127 additions
and
20 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 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,5 +1,6 @@ | ||
import { PropsWithChildren } from 'react'; | ||
import { PropsWithChildren, ReactNode } from 'react'; | ||
|
||
export interface PageProps extends PropsWithChildren { | ||
title?: string; | ||
footer?: ReactNode; | ||
} |
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,4 @@ | ||
export * from './updates.context'; | ||
export * from './updates.provider'; | ||
export * from './updates.context'; | ||
export * from './updates.hooks'; |
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,11 @@ | ||
import { createContext } from 'react'; | ||
import { UpdatesContextProps } from './updates.context.types'; | ||
import { UpdateStatus } from 'types/updates.types'; | ||
|
||
export const UpdatesContext = createContext<UpdatesContextProps>({ | ||
isLoading: false, | ||
inProgress: false, | ||
status: UpdateStatus.Pending, | ||
setStatus: () => {}, | ||
recheck: () => {}, | ||
}); |
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,10 @@ | ||
import { GetUpdatesResponse, UpdateStatus } from 'types/updates.types'; | ||
|
||
export interface UpdatesContextProps { | ||
isLoading: boolean; | ||
inProgress: boolean; | ||
status: UpdateStatus; | ||
setStatus: (status: UpdateStatus) => void; | ||
versionInfo?: GetUpdatesResponse; | ||
recheck: () => void; | ||
} |
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,4 @@ | ||
import { useContext } from 'react'; | ||
import { UpdatesContext } from './updates.context'; | ||
|
||
export const useUpdates = () => useContext(UpdatesContext); |
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,41 @@ | ||
import { FC, PropsWithChildren, useEffect, useMemo, useState } from 'react'; | ||
import { UpdatesContext } from './updates.context'; | ||
import { UpdateStatus } from 'types/updates.types'; | ||
import { useCheckUpdates } from 'hooks/api/useUpdates'; | ||
|
||
export const UpdatesProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [status, setStatus] = useState(UpdateStatus.Pending); | ||
const { isLoading, data, error, isRefetching, refetch } = useCheckUpdates(); | ||
const inProgress = useMemo( | ||
() => | ||
status === UpdateStatus.Updating || | ||
status === UpdateStatus.Restarting || | ||
status === UpdateStatus.Completed, | ||
[status] | ||
); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
setStatus(UpdateStatus.Error); | ||
} else if (isLoading) { | ||
setStatus(UpdateStatus.Checking); | ||
} else if (data && data?.installed.version === data?.latest?.version) { | ||
setStatus(UpdateStatus.UpToDate); | ||
} | ||
}, [data, error, isLoading]); | ||
|
||
return ( | ||
<UpdatesContext.Provider | ||
value={{ | ||
isLoading: isLoading || isRefetching, | ||
inProgress, | ||
status, | ||
setStatus, | ||
versionInfo: data, | ||
recheck: refetch, | ||
}} | ||
> | ||
{children} | ||
</UpdatesContext.Provider> | ||
); | ||
}; |
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,6 @@ | ||
export const Messages = { | ||
version: (version: string) => `PMM ${version}`, | ||
inProgress: 'Update in progress...', | ||
checkedOn: (date: string) => `Checked on: ${date}`, | ||
checkNow: 'Check updates now', | ||
}; |
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,30 @@ | ||
import { Link, Stack, Typography } from '@mui/material'; | ||
import { useUpdates } from 'contexts/updates'; | ||
import { FC } from 'react'; | ||
import { formatCheckDate } from './UpdateFooter.utils'; | ||
import { Messages } from './UpdateFooter.messages'; | ||
|
||
export const UpdateFooter: FC = () => { | ||
const { inProgress, versionInfo, recheck } = useUpdates(); | ||
|
||
console.log({ versionInfo }); | ||
if (!versionInfo) return null; | ||
|
||
return ( | ||
<Stack direction="row" gap={2}> | ||
<Typography variant="body2"> | ||
{Messages.version(versionInfo.installed.version)} | ||
</Typography> | ||
<Typography variant="body2" color="text.disabled"> | ||
{inProgress | ||
? Messages.inProgress | ||
: Messages.checkedOn(formatCheckDate(versionInfo.lastCheck))} | ||
</Typography> | ||
{!inProgress && ( | ||
<Typography variant="body2"> | ||
<Link onClick={recheck}>{Messages.checkNow}</Link> | ||
</Typography> | ||
)} | ||
</Stack> | ||
); | ||
}; |
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,2 @@ | ||
export const formatCheckDate = (date: string) => | ||
new Date(date).toLocaleDateString('en-US'); |
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 @@ | ||
export * from './UpdateFooter'; |
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