test/mocks/cockpit: add permission function

Add the types relevant types for the `cockpit.permission` function so
that we can establish whether the user has the correct privileges to run
the frontend on-prem.
This commit is contained in:
Gianluca Zuccarelli 2025-05-02 13:50:40 +01:00 committed by Klara Simickova
parent 96eb0fe767
commit 49b2196c7b
3 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,26 @@
import { useEffect, useState } from 'react';
import cockpit from 'cockpit';
export const useIsCockpitAdmin = () => {
const [isAdmin, setIsAdmin] = useState(false);
useEffect(() => {
const permission = cockpit.permission({ admin: true });
const onChangeListener = () => {
setIsAdmin(permission.allowed);
};
permission.addEventListener('changed', onChangeListener);
// Check the initial state
onChangeListener();
return () => {
permission.removeEventListener('changed', onChangeListener);
};
}, []);
return isAdmin;
};