Utilities: add useComposerStatus hook

Add a custom hook to check to see if the osbuild-composer.socket
is enabled & running.
This commit is contained in:
Gianluca Zuccarelli 2025-02-04 12:43:35 +00:00 committed by Sanne Raymaekers
parent abca3ac760
commit 87ce805c74

View file

@ -0,0 +1,44 @@
import { useEffect, useState } from 'react';
import cockpit from 'cockpit';
export const useGetComposerSocketStatus = () => {
const [enabled, setEnabled] = useState(false);
const [started, setStarted] = useState(false);
useEffect(() => {
const isEnabled = async () => {
try {
const result = await cockpit.spawn(
['systemctl', 'is-enabled', 'osbuild-composer.socket'],
{ superuser: 'try' }
);
setEnabled((result as string).trim() === 'enabled');
} catch {
// error code 1 means disabled
setEnabled(false);
}
};
const isStarted = async () => {
try {
const result = await cockpit.spawn(
['systemctl', 'is-active', 'osbuild-composer.socket'],
{ superuser: 'try' }
);
setStarted((result as string).trim() === 'active');
} catch {
// exit code 3 means not active
setStarted(false);
}
};
isEnabled();
isStarted();
}, []);
return {
enabled,
started,
};
};