<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
import { useGlobalStore } from '@agent/state/global';
import { humanTimeDiff } from '@wordpress/date';
import { useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { error, Icon } from '@wordpress/icons';

export const UsageMessage = ({ onReady }) => {
	const { retryAfter, isChatAvailable } = useGlobalStore();
	const [time, setTime] = useState(null);

	useEffect(() => {
		if (!retryAfter) return;
		const interval = setInterval(() => {
			if (isChatAvailable()) onReady();
			setTime(() =>
				humanTimeDiff(new Date(Number(retryAfter) + 45_000), new Date()),
			);
		}, 1000);
		return () => clearInterval(interval);
	}, [isChatAvailable, retryAfter, onReady]);

	if (!retryAfter || !time) return;
	return (
		<div className="flex gap-2 rounded-lg bg-gray-100 p-3 text-sm text-gray-900">
			<Icon icon={error} className="flex-none" />
			<div className="grow">
				<div>
					{sprintf(
						// translators: %s is a human-readable time difference, e.g. "2 hours from now"
						__(
							'You have reached your daily messages limit. Your limit will reset in %s.',
							'extendify-local',
						),
						time,
					)}
				</div>
			</div>
		</div>
	);
};
