mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-12 23:31:02 +00:00
baf69da27f
A calm panel under the card's facts: a dot with the state, the tier, a bar of today's use against the limit, what is left and the local time the count starts again. It reads when the card comes into view and every 30 seconds while it stays there and the tab is in front, says why when the services are off, revoked or cannot be read, and offers a new identity behind a confirmation unless the key comes from the environment.
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import type { ServiceIdentityStatus } from '@trek/shared';
|
|
import { useCallback, useEffect, useState, type RefObject } from 'react';
|
|
import { useElementVisible } from '../../hooks/useElementVisible';
|
|
import { serviceIdentityRepo } from '../../repo/serviceIdentityRepo';
|
|
|
|
const REFRESH_MS = 30_000;
|
|
|
|
/**
|
|
* The installation's standing with the TREK services for the admin card:
|
|
* read when the card comes into view, then every 30 seconds while it stays
|
|
* there. Admin configuration, online only like the rest of the settings tab.
|
|
*/
|
|
export function useServiceIdentity(card: RefObject<Element | null>) {
|
|
const visible = useElementVisible(card);
|
|
const [status, setStatus] = useState<ServiceIdentityStatus | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
const [regenerating, setRegenerating] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!visible) return;
|
|
const controller = new AbortController();
|
|
const refresh = () => {
|
|
serviceIdentityRepo.status(controller.signal).then(
|
|
(next) => {
|
|
setStatus(next);
|
|
setFailed(false);
|
|
},
|
|
() => {
|
|
if (!controller.signal.aborted) setFailed(true);
|
|
}
|
|
);
|
|
};
|
|
refresh();
|
|
const timer = setInterval(refresh, REFRESH_MS);
|
|
return () => {
|
|
controller.abort();
|
|
clearInterval(timer);
|
|
};
|
|
}, [visible]);
|
|
|
|
const regenerate = useCallback(async (): Promise<void> => {
|
|
setRegenerating(true);
|
|
try {
|
|
setStatus(await serviceIdentityRepo.regenerate());
|
|
} finally {
|
|
setRegenerating(false);
|
|
}
|
|
}, []);
|
|
|
|
return { status, failed, regenerating, regenerate };
|
|
}
|