import { useState, useRef, useEffect } from 'react'; import { safeCall } from '../../helpers/safeCall.js'; import { withContext } from '../../helpers/errorMessage.js'; import { startContainer as svcStartContainer, stopContainer as svcStopContainer, restartContainer as svcRestartContainer, removeContainer as svcRemoveContainer, } from '../../helpers/dockerService/serviceComponents/containerActions.js'; /** * Custom hook to manage container actions (start, stop, restart, remove). * Handles feedback messages and exposes helpers for each action. * * @param {Object} [params] * @param {Function} [params.onAction] - Callback after action is performed * @returns {Object} Action helpers and feedback state */ export function useContainerActions({ onAction } = {}) { const [message, setMessage] = useState(''); const [messageColor, setMessageColor] = useState('yellow'); const messageTimerRef = useRef(null); function setTimedMessage(msg, color = 'yellow', ms = 4000) { clearTimeout(messageTimerRef.current); setMessage(msg); if (color !== undefined) setMessageColor(color); if (msg) { messageTimerRef.current = setTimeout(() => setMessage(''), ms); } } /** * Show a message that waits for an answer instead of expiring. * * `setMessage` on its own does not cancel the timer a previous * `setTimedMessage` armed, so a question written over a "Working..." line * used to be wiped seconds later while the answer it was asking about stayed * armed. * * @param {string} msg * @param {string} [color] */ function setPersistentMessage(msg, color = 'yellow') { clearTimeout(messageTimerRef.current); setMessage(msg); setMessageColor(color); } useEffect(() => () => clearTimeout(messageTimerRef.current), []); /** * Handles a generic container action and sets feedback. * @param {Function} actionFn - The async action function (start, stop, etc) * @param {string} actionLabel - Gerund shown while it runs ('Stopping') * @param {string} actionVerb - Infinitive used in the failure message ('stop') * @param {Object} container - The container to act on, or nullish when nothing * is selected. The early exit below is what keeps a keystroke from reaching * Docker with nothing to act on. * @param {Function} [stateCheck] - Optional function to check state before action */ async function handleAction({ actionFn, actionLabel, actionVerb, container, stateCheck, }) { if (!container) return; if (stateCheck) { const checkMsg = stateCheck(container); if (checkMsg) { setMessage(checkMsg); setMessageColor('yellow'); return; } } setMessage(`${actionLabel} container...`); setMessageColor('yellow'); try { await actionFn(container.id); setTimedMessage(`${actionLabel} container successful.`, 'green'); safeCall(onAction); } catch (err) { setTimedMessage( withContext( `Failed to ${actionVerb ?? actionLabel} container`, err.message ), 'red' ); } } return { message, setMessage, setTimedMessage, setPersistentMessage, messageColor, setMessageColor, handleAction, // Expose concrete actions so callers can use actions.startContainer(id) startContainer: async (id) => { return await svcStartContainer(id); }, stopContainer: async (id) => { return await svcStopContainer(id); }, restartContainer: async (id) => { return await svcRestartContainer(id); }, removeContainer: async (id) => { return await svcRemoveContainer(id); }, }; }