From 37c44c2264cbae160080a393b29d84be2bf40cb3 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 22 Dec 2022 21:52:59 +0800 Subject: [PATCH] Update poll at point of expiry --- src/components/status.jsx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/components/status.jsx b/src/components/status.jsx index 4e7fe7d1..412cef22 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -926,6 +926,31 @@ function Poll({ poll, readOnly, onUpdate = () => {} }) { const expiresAtDate = !!expiresAt && new Date(expiresAt); + // Update poll at point of expiry + useEffect(() => { + let timeout; + if (!expired && expiresAtDate) { + const ms = expiresAtDate.getTime() - Date.now() + 1; // +1 to give it a little buffer + if (ms > 0) { + timeout = setTimeout(() => { + setUIState('loading'); + (async () => { + try { + const pollResponse = await masto.poll.fetch(id); + onUpdate(pollResponse); + } catch (e) { + // Silent fail + } + setUIState('default'); + })(); + }, ms); + } + } + return () => { + clearTimeout(timeout); + }; + }, [expired, expiresAtDate]); + const pollVotesCount = votersCount || votesCount; let roundPrecision = 0; if (pollVotesCount <= 1000) {