import { ContentStatus, ContentStatusPhase } from "./db"; export default function UpdateButton({ updateStatus, update, disabled = false, }: { updateStatus: ContentStatus | null; update: () => void; disabled?: boolean; }) { const current = updateStatus?.local?.date && `Last updated: ${Intl.DateTimeFormat([], { dateStyle: "medium" }).format( Date.parse(updateStatus?.local?.date) )}`; let button: JSX.Element | null = null; let info: string | undefined; switch (updateStatus?.phase) { case ContentStatusPhase.INITIAL: info = "Checking for updates"; break; case ContentStatusPhase.IDLE: if (updateStatus?.local?.version === updateStatus?.remote?.latest) { info = "Your content is up to date"; button = Up to date; } else { if (updateStatus?.local) { info = "Update available"; button = ( {" "} Update now ); } else { info = "Start using MDN Offline by downloading the latest version of MDN Web Docs"; button = ( {" "} Download ); } } break; case ContentStatusPhase.DOWNLOAD: if (updateStatus?.local) { info = "Update in progress…"; } else { info = "Download in progress…"; } button = Downloading…; break; case ContentStatusPhase.UNPACK: if (updateStatus?.local) { info = "Update in progress…"; } else { info = "Download in progress…"; } const progress = (updateStatus?.progress || 0) * 100; button = ( Unpacking…{" "} {progress?.toLocaleString(undefined, { maximumFractionDigits: 0, })} % ); break; case ContentStatusPhase.CLEAR: info = "Clearing…"; break; } return ( Update status {current} {current && } {info} {button} ); }