mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-10 06:28:50 +00:00
feat: simplied the caching panel logic and fixed permission issue
This commit is contained in:
@@ -4,7 +4,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|||||||
|
|
||||||
import { createNotification } from "@app/components/notifications";
|
import { createNotification } from "@app/components/notifications";
|
||||||
import { Badge, Button, DeleteActionModal } from "@app/components/v2";
|
import { Badge, Button, DeleteActionModal } from "@app/components/v2";
|
||||||
import { useOrgPermission } from "@app/context";
|
import { useUser } from "@app/context";
|
||||||
import { usePopUp } from "@app/hooks";
|
import { usePopUp } from "@app/hooks";
|
||||||
import { useInvalidateCache } from "@app/hooks/api";
|
import { useInvalidateCache } from "@app/hooks/api";
|
||||||
import { useGetInvalidatingCacheStatus } from "@app/hooks/api/admin/queries";
|
import { useGetInvalidatingCacheStatus } from "@app/hooks/api/admin/queries";
|
||||||
@@ -12,52 +12,28 @@ import { CacheType } from "@app/hooks/api/admin/types";
|
|||||||
|
|
||||||
export const CachingPanel = () => {
|
export const CachingPanel = () => {
|
||||||
const { mutateAsync: invalidateCache } = useInvalidateCache();
|
const { mutateAsync: invalidateCache } = useInvalidateCache();
|
||||||
const { membership } = useOrgPermission();
|
const { user } = useUser();
|
||||||
|
|
||||||
const shouldShowSuccessRef = useRef(false);
|
const shouldShowSuccessRef = useRef(false);
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
||||||
|
|
||||||
const [type, setType] = useState<CacheType | null>(null);
|
const [type, setType] = useState<CacheType | null>(null);
|
||||||
const [buttonsDisabled, setButtonsDisabled] = useState(false);
|
|
||||||
const [shouldPoll, setShouldPoll] = useState(false);
|
const [shouldPoll, setShouldPoll] = useState(false);
|
||||||
|
|
||||||
const { data: isInvalidating, refetch: refetchInvalidatingStatus } =
|
const { data: invalidationStatus, isPending } = useGetInvalidatingCacheStatus(shouldPoll);
|
||||||
useGetInvalidatingCacheStatus(shouldPoll);
|
const isInvalidating = Boolean(shouldPoll && !isPending && invalidationStatus);
|
||||||
|
|
||||||
const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
|
const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([
|
||||||
"invalidateCache"
|
"invalidateCache"
|
||||||
] as const);
|
] as const);
|
||||||
|
|
||||||
const disableButtonsTemporarily = () => {
|
|
||||||
setButtonsDisabled(true);
|
|
||||||
timeoutRef.current = setTimeout(() => setButtonsDisabled(false), 10000);
|
|
||||||
};
|
|
||||||
|
|
||||||
const success = () => {
|
|
||||||
if (!shouldShowSuccessRef.current) return;
|
|
||||||
shouldShowSuccessRef.current = false;
|
|
||||||
|
|
||||||
createNotification({ text: "Successfully invalidated cache", type: "success" });
|
|
||||||
setButtonsDisabled(false);
|
|
||||||
setShouldPoll(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleInvalidateCacheSubmit = async () => {
|
const handleInvalidateCacheSubmit = async () => {
|
||||||
if (!type) return;
|
if (!type || isInvalidating) return;
|
||||||
|
|
||||||
shouldShowSuccessRef.current = true;
|
shouldShowSuccessRef.current = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await invalidateCache({ type });
|
await invalidateCache({ type });
|
||||||
createNotification({ text: `Began invalidating ${type} cache`, type: "success" });
|
createNotification({ text: `Began invalidating ${type} cache`, type: "success" });
|
||||||
disableButtonsTemporarily();
|
|
||||||
handlePopUpClose("invalidateCache");
|
handlePopUpClose("invalidateCache");
|
||||||
|
|
||||||
if (!(await refetchInvalidatingStatus()).data) {
|
|
||||||
success();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setShouldPoll(true);
|
setShouldPoll(true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -66,34 +42,13 @@ export const CachingPanel = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refetchInvalidatingStatus();
|
if (isInvalidating) return;
|
||||||
const timer = setTimeout(() => (shouldShowSuccessRef.current = true), 1000);
|
|
||||||
return () => {
|
if (shouldPoll) {
|
||||||
clearTimeout(timer);
|
|
||||||
setShouldPoll(false);
|
setShouldPoll(false);
|
||||||
};
|
createNotification({ text: "Successfully invalidated cache", type: "success" });
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!isInvalidating) return;
|
|
||||||
|
|
||||||
clearTimeout(timeoutRef.current!);
|
|
||||||
setShouldPoll(true);
|
|
||||||
disableButtonsTemporarily();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearTimeout(timeoutRef.current!);
|
|
||||||
};
|
|
||||||
}, [isInvalidating]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (shouldShowSuccessRef.current && isInvalidating === false) {
|
|
||||||
success();
|
|
||||||
clearTimeout(timeoutRef.current!);
|
|
||||||
}
|
}
|
||||||
}, [isInvalidating]);
|
}, [isInvalidating, shouldPoll]);
|
||||||
|
|
||||||
const isAdmin = membership?.role === "admin";
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -123,12 +78,11 @@ export const CachingPanel = () => {
|
|||||||
setType(CacheType.SECRETS);
|
setType(CacheType.SECRETS);
|
||||||
handlePopUpOpen("invalidateCache");
|
handlePopUpOpen("invalidateCache");
|
||||||
}}
|
}}
|
||||||
isDisabled={!isAdmin || buttonsDisabled}
|
isDisabled={!user.superAdmin || isInvalidating}
|
||||||
>
|
>
|
||||||
Invalidate Secrets Cache
|
Invalidate Secrets Cache
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<DeleteActionModal
|
<DeleteActionModal
|
||||||
isOpen={popUp.invalidateCache.isOpen}
|
isOpen={popUp.invalidateCache.isOpen}
|
||||||
title={`Are you sure you want to invalidate ${type} cache?`}
|
title={`Are you sure you want to invalidate ${type} cache?`}
|
||||||
|
|||||||
Reference in New Issue
Block a user