Files
codexbar-mobile/components/ResetCountdown.tsx
T

31 lines
904 B
TypeScript

import { useEffect, useState } from "react";
import { Text } from "react-native";
import { formatResetCountdown, formatResetCountdownISO } from "@/lib/timeUtils";
interface ResetCountdownProps {
resetAtSeconds?: number;
resetAtISO?: string;
}
export function ResetCountdown({ resetAtSeconds, resetAtISO }: ResetCountdownProps) {
const getLabel = () => {
if (resetAtSeconds !== undefined) return formatResetCountdown(resetAtSeconds);
if (resetAtISO) return formatResetCountdownISO(resetAtISO);
return "";
};
const [label, setLabel] = useState(getLabel);
useEffect(() => {
setLabel(getLabel());
const interval = setInterval(() => setLabel(getLabel()), 60_000);
return () => clearInterval(interval);
}, [resetAtSeconds, resetAtISO]);
if (!label) return null;
return (
<Text className="text-xs text-neutral-500 dark:text-neutral-400">{label}</Text>
);
}