-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #698 from contember/feat/outdated-app-dialog
feat(playground): outdated application checker
- Loading branch information
Showing
7 changed files
with
120 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useIsApplicationOutdated' |
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,62 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
const defaultCheckIntervalMs = 30_000 | ||
|
||
|
||
export const useIsApplicationOutdated = ({ checkIntervalMs = defaultCheckIntervalMs }: { | ||
checkIntervalMs?: number | ||
} = {}) => { | ||
const [initialVersion] = useState(() => { | ||
return getVersionFromDocument(document.head) | ||
}) | ||
const [isOutdated, setIsOutdated] = useState(false) | ||
const fetchVersion = useFetchVersion() | ||
|
||
const checkVersion = useCallback(async () => { | ||
if (document.hidden) { | ||
return | ||
} | ||
const version = await fetchVersion() | ||
if (version !== initialVersion || Math.random() < 0.3) { | ||
setIsOutdated(true) | ||
} | ||
}, [fetchVersion, initialVersion]) | ||
const shouldCheck = !!initialVersion && !isOutdated | ||
|
||
useEffect(() => { | ||
if (!shouldCheck) { | ||
return | ||
} | ||
const handle = setInterval(checkVersion, checkIntervalMs) | ||
return () => clearInterval(handle) | ||
}, [checkIntervalMs, checkVersion, shouldCheck]) | ||
|
||
useEffect(() => { | ||
if (!shouldCheck) { | ||
return | ||
} | ||
document.addEventListener('visibilitychange', checkVersion) | ||
return () => document.removeEventListener('visibilitychange', checkVersion) | ||
}, [checkVersion, shouldCheck]) | ||
|
||
return isOutdated | ||
} | ||
|
||
const getVersionFromDocument = (node: ParentNode) => { | ||
const meta = node.querySelector<HTMLMetaElement>('meta[name=contember-build-version]') | ||
return meta?.content | ||
} | ||
|
||
const useFetchVersion = () => { | ||
return useCallback(async () => { | ||
try { | ||
const result = await (await fetch(window.location.href)).text() | ||
const html = document.createElement('html') | ||
html.innerHTML = result | ||
return getVersionFromDocument(html) | ||
} catch (e) { | ||
console.error(e) | ||
return undefined | ||
} | ||
}, []) | ||
} |
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
41 changes: 41 additions & 0 deletions
41
packages/playground/admin/lib/components/outdated-application-dialog.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ComponentType, useCallback, useEffect, useState } from 'react' | ||
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader } from './ui/alert-dialog' | ||
import { useIsApplicationOutdated } from '@contember/interface' | ||
import { ClockIcon, RefreshCwIcon } from 'lucide-react' | ||
import { dict } from '../dict' | ||
|
||
const postponeTimeoutMs = 60_000 * 5 | ||
const checkIntervalMs = 30_000 | ||
|
||
export const OutdatedApplicationDialog: ComponentType = () => { | ||
const isOutdated = useIsApplicationOutdated({ checkIntervalMs }) | ||
const [open, setOpen] = useState(true) | ||
|
||
const postpone = useCallback(() => { | ||
setOpen(false) | ||
setTimeout(() => setOpen(true), postponeTimeoutMs) | ||
}, []) | ||
|
||
return ( | ||
<AlertDialog open={isOutdated && open} onOpenChange={it => !it ? postpone() : null}> | ||
<AlertDialogContent> | ||
<AlertDialogHeader>{dict.outdatedApplication.title}</AlertDialogHeader> | ||
<AlertDialogDescription>{dict.outdatedApplication.description}</AlertDialogDescription> | ||
|
||
<div className="text-sm text-destructive">{dict.outdatedApplication.warning}</div> | ||
<AlertDialogFooter> | ||
<AlertDialogCancel onClick={postpone} className="gap-1"> | ||
<ClockIcon className="w-3 h-4"/> | ||
{dict.outdatedApplication.snooze} | ||
</AlertDialogCancel> | ||
<AlertDialogAction onClick={() => window.location.reload()} className="gap-1"> | ||
<RefreshCwIcon className="w-3 h-4" /> | ||
{dict.outdatedApplication.refreshNow} | ||
</AlertDialogAction> | ||
</AlertDialogFooter> | ||
</AlertDialogContent> | ||
</AlertDialog> | ||
) | ||
} | ||
|
||
|
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