async function handleDeploymentOperationActions(event, target, action, repository) { if (action === "deploy-profile") { if (repository && String(repository.id) !== String(ui.selectedRepoId)) selectRepository(repository.id, false); const profile = repository?.deploymentProfiles?.find( (item) => item.id === target.dataset.profileId, ) || selectedProfile(repository); ui.selectedProfileId = profile?.id || null; if (!profile) return; const report = await runDeploymentPreflight(repository, profile.id, { showModal: true, }); if (!report) return; } else if (action === "continue-after-preflight") { const profile = selectedRepository()?.deploymentProfiles?.find( (item) => item.id === target.dataset.profileId, ); if (!profile || !ui.deploymentPreflight?.summary?.ready) return; if (profile.confirmationRequired !== false) { ui.modal = { type: "deploy-confirm", profileId: profile.id }; render(); } else await executeDeployment(profile.id); } else if (action === "confirm-deploy") await executeDeployment(target.dataset.profileId); else if (action === "rollback-profile") { if (!repository) repository = profileRepository(target.dataset.profileId); if (repository && String(repository.id) !== String(ui.selectedRepoId)) selectRepository(repository.id, false); ui.modal = { type: "rollback-confirm", profileId: target.dataset.profileId, }; render(); } else if (action === "confirm-rollback") await executeRollback(target.dataset.profileId); else if (action === "refresh-profile-state") { if (!repository) repository = profileRepository(target.dataset.profileId); const profile = repository?.deploymentProfiles?.find( (item) => item.id === target.dataset.profileId, ); setLoading(true, `Checking ${profile?.environment || "environment"}…`); try { const state = await window.forgeflow.refreshProfileState( repository.fullName, target.dataset.profileId, ); profile.state = state; showToast( "Environment checked", state.healthy === false ? "Healthcheck reports an unhealthy state." : state.liveSha ? `Server reports ${shortSha(state.liveSha)}.` : "Connection checked; no live SHA reported.", state.healthy === false ? "error" : "success", ); } catch (error) { showToast("Status check failed", error.message, "error"); } setLoading(false); } else if (action === "repair-missing-dockerman") { const targets = ui.repositories.flatMap((candidate) => (candidate.deploymentProfiles || []) .filter( (profile) => profile.provider === "ssh-unraid" && profile.state?.containerRunning && !dockerManIntegration(profile).ready, ) .map((profile) => ({ repository: candidate, profile })), ); if (!targets.length) return; if ( !confirm( `Recreate ${targets.length} running container${targets.length === 1 ? "" : "s"} with the missing DockerMan WebUI, icon and template metadata?`, ) ) return; setLoading(true, "Repairing missing DockerMan integrations…"); let repaired = 0; const failures = []; for (const item of targets) { try { await window.forgeflow.applyDockerManMetadata( item.repository, item.profile.id, ); repaired += 1; } catch (error) { failures.push(`${item.repository.name}: ${error.message}`); } } await refreshDeploymentTruth(false); showToast( failures.length ? "DockerMan repair partially completed" : "DockerMan integrations repaired", failures.length ? `${repaired} repaired, ${failures.length} failed.` : `${repaired} running container${repaired === 1 ? "" : "s"} updated.`, failures.length ? "error" : "success", ); setLoading(false); } else if (action === "apply-dockerman-metadata") { if (!repository) repository = profileRepository(target.dataset.profileId); setLoading( true, "Applying DockerMan labels, template, icon and WebUI metadata…", ); try { await window.forgeflow.applyDockerManMetadata( repository, target.dataset.profileId, ); await refreshRepositories(false); showToast( "DockerMan integration repaired", "The container was recreated with labels, a persistent template, WebUI and icon metadata.", "success", ); } catch (error) { showToast( "Could not repair DockerMan integration", error.message, "error", ); } setLoading(false); } else if (action === "reconcile-deployment") { if (!repository) repository = profileRepository(target.dataset.profileId); setLoading(true, "Reconciling ForgeFlow with the live Unraid container…"); try { await window.forgeflow.reconcileDeployment( repository.fullName, target.dataset.profileId, ); await refreshActiveOperations(false); await refreshRepositories(false); showToast( "Deployment reconciled", "Live SHA, container health and operation status were refreshed.", "success", ); } catch (error) { showToast("Could not reconcile deployment", error.message, "error"); } setLoading(false); } else if (action === "open-profile-webui") await window.forgeflow.openExternal(target.dataset.url); else if (action === "open-operation") { const operation = await window.forgeflow.getOperation( target.dataset.operationId, ); if (operation) { ui.activeDeployment = operation; ui.currentView = "deployment-run"; render(); startOperationPolling(); } } else if (action === "refresh-current-operation") { setLoading(true, "Refreshing deployment status…"); try { const operation = await window.forgeflow.refreshOperations( ui.activeDeployment.id, ); updateOperationInState(operation); if (!isTerminalOperation(operation.status)) startOperationPolling(); } catch (error) { showToast("Status refresh failed", error.message, "error"); } setLoading(false); } else if (action === "open-run-url") await window.forgeflow.openExternal(ui.activeDeployment.runUrl); else if (action === "close-deployment") { stopOperationPolling(); ui.activeDeployment = null; ui.currentView = selectedRepository() ? "repository" : "deployments"; render(); } else return false; return true; }