(props: Props)
| 12 | } |
| 13 | |
| 14 | const QuotaView = (props: Props) => { |
| 15 | const [quota, setQuota] = useState<Quota>({ current: 0, limit: 0 }); |
| 16 | const { t } = useTranslation(); |
| 17 | const { data: session } = useSession(); |
| 18 | |
| 19 | const showSupplyOwnKey = !session || quota.current >= quota.limit; |
| 20 | const expired = session?.user?.subscription?.expireAt && session?.user?.subscription?.expireAt < Date.now(); |
| 21 | const showActionButton = !session || session.user.subscription.plan === "FREE" || expired; |
| 22 | |
| 23 | const refreshQuota = async (userId: string) => { |
| 24 | let quota: Quota = { current: 0, limit: 0 }; |
| 25 | try { |
| 26 | const { data } = await axios.get("/api/usage", { |
| 27 | headers: { Authorization: `Bearer ${userId}` }, |
| 28 | }); |
| 29 | quota = data; |
| 30 | } catch (error) { |
| 31 | // do nth |
| 32 | } |
| 33 | setQuota(quota); |
| 34 | }; |
| 35 | |
| 36 | getEventEmitter().on("usage.update", () => { |
| 37 | if (session?.user.id) { |
| 38 | refreshQuota(session.user.id); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | useEffect(() => { |
| 43 | if (session?.user.id) { |
| 44 | refreshQuota(session.user.id); |
| 45 | } |
| 46 | }, [session]); |
| 47 | |
| 48 | return ( |
| 49 | <div className="px-4 py-3 space-y-2 rounded-lg border border-indigo-400 flex flex-col dark:text-gray-300 hover:bg-white dark:hover:bg-zinc-800 bg-white dark:bg-zinc-800"> |
| 50 | <div className="flex justify-between"> |
| 51 | <span className="rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20"> |
| 52 | {session ? t(`setting.plan.${session.user.subscription.plan.toLowerCase()}`) : t("setting.plan.guest")} |
| 53 | </span> |
| 54 | {!!expired && ( |
| 55 | <span className="rounded-full bg-yellow-50 px-2 py-0.5 text-xs font-medium text-yellow-700 ring-1 ring-inset ring-yellow-600/20"> |
| 56 | {t("setting.plan.expired")} |
| 57 | </span> |
| 58 | )} |
| 59 | </div> |
| 60 | <div className="flex justify-between pt-1"> |
| 61 | <div>{t("common.quota")}</div> |
| 62 | <div className={quota.current >= quota.limit ? "text-red-600" : "text-black dark:text-gray-300"}> |
| 63 | {quota.current}/{quota.limit} |
| 64 | </div> |
| 65 | </div> |
| 66 | {!!showActionButton && |
| 67 | (session ? ( |
| 68 | <Link |
| 69 | href="/setting" |
| 70 | className="rounded bg-indigo-600 px-2 py-1 text-center text-base font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" |
| 71 | > |
nothing calls this directly
no test coverage detected