Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions public/obs-overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
}
});

setInterval(() => {
if (active && typeof active.remaining === 'number') {
active.remaining = Math.max(0, active.remaining - 1000);
render();
}
}, 1000);

function render() {
const el = document.getElementById('timer');
if (!active || active.remaining == null) {
Expand Down
7 changes: 6 additions & 1 deletion public/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ self.addEventListener('install', (event) => {
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
event.respondWith(
fetch(event.request).catch(() => caches.match(event.request).then((response) => response || caches.match(OFFLINE_URL)))
fetch(event.request).catch(() => {
if (event.request.mode === 'navigate') {
return caches.match(OFFLINE_URL);
}
return caches.match(event.request);
Comment on lines 10 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Return a Response for cache misses in fetch handler

The new fetch handler now returns caches.match(event.request) for non‑navigation requests but does not provide a fallback when the resource is missing from the cache. If the network is offline and the request is not cached, the promise resolves to undefined, which causes event.respondWith to reject with "The provided promise did not resolve with a Response" and the request fails even when an offline page or empty response would suffice. A response object should always be returned on the catch path, e.g. fall back to the offline page or a new Response('', {status: 503}), to avoid service worker errors during offline use.

Useful? React with 👍 / 👎.

})
);
});
3 changes: 3 additions & 0 deletions src/components/TimerImportExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const TimerImportExport: React.FC = () => {
const format = ext === 'csv' ? 'csv' : 'json';
const timers = importTimers(text, format);
dispatch({ type: 'setAll', timers });
import('../services/timerSync').then(({ saveTimer }) => {
timers.forEach((tmr) => saveTimer(tmr));
});
} catch (err) {
console.error(err);
alert(t('timerList.importError'));
Expand Down
8 changes: 4 additions & 4 deletions src/components/TimerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const TimerList: React.FC = () => {

return (
<div>
{state.timers.map((t) => (
<div key={t.id}>
<Timer config={t} />
<button onClick={() => dispatch({ type: 'remove', id: t.id })}>
{state.timers.map((timer) => (
<div key={timer.id}>
<Timer config={timer} />
<button onClick={() => dispatch({ type: 'remove', id: timer.id })}>
{t('timerList.remove')}
</button>
</div>
Expand Down
12 changes: 9 additions & 3 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const unsubscribe = auth.onAuthStateChanged(async (u) => {
setUser(u);
if (u) {
const r = await getUserRole(u);
setRole(r);
try {
const r = await getUserRole(u);
setRole(r);
} catch {
setRole(null);
} finally {
setLoading(false);
}
} else {
setRole(null);
setLoading(false);
}
setLoading(false);
});
return unsubscribe;
}, []);
Expand Down
3 changes: 3 additions & 0 deletions src/context/MessagesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ export const MessagesProvider: React.FC<{ children: React.ReactNode }> = ({ chil

useEffect(() => {
let unsub: (() => void) | undefined;
let isMounted = true;
import('../services/messageSync').then(({ listenMessage }) => {
if (!isMounted) return;
unsub = listenMessage((msg) => dispatch({ type: 'set', message: msg }));
});
return () => {
isMounted = false;
if (unsub) unsub();
};
}, []);
Expand Down
3 changes: 3 additions & 0 deletions src/context/TimersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ export const TimersProvider: React.FC<{ children: React.ReactNode }> = ({ childr
// Listen to remote updates
React.useEffect(() => {
let unsubscribe: (() => void) | undefined;
let isMounted = true;
import('../services/timerSync').then(({ listenTimers }) => {
if (!isMounted) return;
unsubscribe = listenTimers((timers) =>
localDispatch({ type: 'setAll', timers })
);
});
return () => {
isMounted = false;
if (unsubscribe) unsubscribe();
};
}, []);
Expand Down
18 changes: 14 additions & 4 deletions src/hooks/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const useTimer = (
const storageKey = `timer-progress-${id}`;
const [elapsed, setElapsed] = useState(0);
const [running, setRunning] = useState(false);
const intervalRef = useRef<NodeJS.Timeout | null>(null);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const autoStarted = useRef(false);

// Load persisted progress
useEffect(() => {
Expand Down Expand Up @@ -49,21 +50,30 @@ export const useTimer = (
}
}, [elapsed, running, storageKey]);

// reset auto-start flag if schedule changes
useEffect(() => {
autoStarted.current = false;
}, [startAt]);

// start the timer at a scheduled time if provided
useEffect(() => {
if (startAt === undefined || running) return;
if (startAt === undefined || running || autoStarted.current) return;
const now = warpedNow();
const startTime = startAt + getSyncDelay();
if (startTime <= now) {
setRunning(true);
autoStarted.current = true;
} else {
const timeout = setTimeout(() => setRunning(true), startTime - now);
const timeout = setTimeout(() => {
setRunning(true);
autoStarted.current = true;
}, startTime - now);
return () => clearTimeout(timeout);
}
}, [startAt, running]);

useEffect(() => {
if (!running || kind === 'clock') return;
if (!running && kind !== 'clock') return;
intervalRef.current = setInterval(() => {
setElapsed((e) => e + 1000);
}, 1000);
Expand Down
5 changes: 4 additions & 1 deletion src/services/timerSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function listenTimers(callback: (timers: TimerConfig[]) => void) {
// Add or update a timer document
export function saveTimer(timer: TimerConfig) {
const docRef = doc(db, 'timers', timer.id);
return setDoc(docRef, timer);
const data = Object.fromEntries(
Object.entries(timer).filter(([, v]) => v !== undefined)
) as TimerConfig;
return setDoc(docRef, data);
}

// Remove a timer by id
Expand Down