import React, { useEffect, useState } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import List, { ListItem } from 'soapbox/components/list'; import { HStack, Text, Column, FormActions, Button, Stack, Icon } from 'soapbox/components/ui'; import { unregisterSW } from 'soapbox/utils/sw'; import Indicator from './components/indicator'; const messages = defineMessages({ heading: { id: 'column.developers.service_worker', defaultMessage: 'Service Worker' }, status: { id: 'sw.status', defaultMessage: 'Status' }, url: { id: 'sw.url', defaultMessage: 'Script URL' }, }); /** Hook that returns the active ServiceWorker registration. */ const useRegistration = () => { const [isLoading, setLoading] = useState(true); const [registration, setRegistration] = useState(); const isSupported = 'serviceWorker' in navigator; useEffect(() => { if (isSupported) { navigator.serviceWorker.getRegistration() .then(r => { setRegistration(r); setLoading(false); }) .catch(() => setLoading(false)); } else { setLoading(false); } }, []); return { isLoading, registration, }; }; interface IServiceWorkerInfo { } /** Mini ServiceWorker debugging component. */ const ServiceWorkerInfo: React.FC = () => { const intl = useIntl(); const { isLoading, registration } = useRegistration(); const url = registration?.active?.scriptURL; const getState = () => { if (registration?.waiting) { return 'pending'; } else if (registration?.active) { return 'active'; } else { return 'inactive'; } }; const getMessage = () => { if (isLoading) { return ( ); } else if (!isLoading && !registration) { return ( ); } else if (registration?.waiting) { return ( ); } else if (registration?.active) { return ( ); } else { return ( ); } }; const handleRestart = async() => { await unregisterSW(); window.location.reload(); }; return ( {getMessage()} {url && ( {url} )} ); }; export default ServiceWorkerInfo;