Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | 1x 1x 1x | import { palette } from "@palette";
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { useEffect, useRef, useCallback, useState } from "react";
// Matches EMAIL_VERIFICATION_RESEND_COOLDOWN on the API (60s). It used to be
// 300, which locked the buyer out for the token's whole 5-minute life and
// unlocked exactly as the outstanding link expired.
const TIMER_OFFSET = 60;
const STORAGE_KEY = "buttonWrapperTimer";
// Returns undefined if not present or invalid.
function readTimerFromSession(): number | undefined {
try {
const raw = sessionStorage.getItem(STORAGE_KEY);
if (raw == null) return undefined;
const n = Number(raw);
if (!Number.isFinite(n) || n < 0) return undefined;
return Math.floor(n);
} catch {
return undefined;
}
}
// Write timer to sessionStorage. If timer <= 0, remove the key.
function writeTimerToSession(timer: number): void {
try {
if (timer > 0) {
sessionStorage.setItem(STORAGE_KEY, String(Math.floor(timer)));
} else {
sessionStorage.removeItem(STORAGE_KEY);
}
} catch {
// Ignore write errors
}
}
const ButtonWrapper = ({
shouldHaveTimer,
label,
onClick,
onTimerEndLabel,
disabled,
}: {
label: string;
onClick?: () => void | Promise<unknown>;
shouldHaveTimer?: boolean;
onTimerEndLabel?: string;
disabled?: boolean;
}) => {
const { isMobileView } = useCustomTheme();
const [timer, setTimer] = useState<number>(TIMER_OFFSET);
const intervalRef = useRef<number | null>(null);
useEffect(() => {
if (!shouldHaveTimer) return;
const stored = readTimerFromSession();
if (typeof stored === "number") {
setTimer(stored);
} else {
// Ensure initial offset is persisted so a refresh right away keeps the value
writeTimerToSession(TIMER_OFFSET);
setTimer(TIMER_OFFSET);
}
}, []);
const tick = useCallback(() => {
setTimer((prev) => {
const next = prev - 1;
// Persist each tick
writeTimerToSession(next);
return next;
});
}, []);
useEffect(() => {
if (intervalRef.current != null) {
window.clearInterval(intervalRef.current);
intervalRef.current = null;
}
if (shouldHaveTimer && timer > 0) {
intervalRef.current = window.setInterval(tick, 1000);
}
return () => {
if (intervalRef.current != null) {
window.clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [shouldHaveTimer, timer > 0, tick]);
useEffect(() => {
if (timer <= 0) {
writeTimerToSession(0);
}
}, [timer]);
useEffect(() => {
const persistNow = () => writeTimerToSession(timer);
const onBeforeUnload = () => writeTimerToSession(timer);
document.addEventListener("visibilitychange", persistNow, {
passive: true,
});
window.addEventListener("beforeunload", onBeforeUnload, { passive: true });
return () => {
document.removeEventListener(
"visibilitychange",
persistNow as EventListener,
);
window.removeEventListener(
"beforeunload",
onBeforeUnload as EventListener,
);
};
}, [timer]);
const handleClick = async () => {
try {
await onClick?.();
} catch {
// Starting the cooldown for a mail that never went out takes away the
// retry that could still work. The failure is reported by the caller.
return;
}
if (shouldHaveTimer) {
setTimer(TIMER_OFFSET);
writeTimerToSession(TIMER_OFFSET);
}
};
return (
<GiveButton
variant="outline"
disabled={disabled || (shouldHaveTimer && timer > 0)}
label={
<GiveText
variant="body"
fontSize={isMobileView ? 12 : 14}
lineHeight={isMobileView ? "19.2px" : "21.6px"}
color={palette.black[100]}
sx={{
color:
timer === 0 || !shouldHaveTimer
? "rgba(41, 41, 40, 1)"
: "rgba(153, 153, 151, 1)",
textAlign: "center",
fontWeight: 400,
}}
>
{shouldHaveTimer && timer === 0 ? onTimerEndLabel ?? label : label}
{shouldHaveTimer && timer > 0 && (
<>
{" "}
<span
style={{
cursor: "pointer",
}}
>
{`${Math.floor(timer / 60)}:${(timer % 60)
.toString()
.padStart(2, "0")}`}
</span>
</>
)}
</GiveText>
}
onClick={handleClick}
sx={{
borderRadius: "40px",
borderWidth: "1.5px",
padding: "11px 20px",
"&:disabled": {
opacity: 1,
},
}}
/>
);
};
export { ButtonWrapper };
|