Skip to content

Modern Docker Management, Designed for Everyone

6.3 KB
Raw Blame
<script lang="ts">
	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import PruneModePicker from '#lib/components/prune/prune-mode-picker.svelte';
	import SettingsRow from '#lib/components/settings/settings-row.svelte';
	import SettingsSection from '#lib/components/settings/settings-section.svelte';
	import { m } from '#lib/paraglide/messages.js';
	import type { SystemPruneRequest } from '#lib/types/automation.js';
	import type { Settings } from '#lib/types/settings.js';

	interface Props {
		defaults?: Settings | null;
		isPruning?: boolean;
		onConfirm?: (request: SystemPruneRequest) => void;
		onCancel?: () => void;
	}

	let { defaults = null, isPruning = false, onConfirm = () => {}, onCancel = () => {} }: Props = $props();

	function getInitialSelectionInternal() {
		return {
			containerMode: defaults?.pruneContainerMode ?? 'stopped',
			containerUntil: defaults?.pruneContainerUntil ?? '',
			imageMode: defaults?.pruneImageMode ?? 'dangling',
			imageUntil: defaults?.pruneImageUntil ?? '',
			networkMode: defaults?.pruneNetworkMode ?? 'unused',
			networkUntil: defaults?.pruneNetworkUntil ?? '',
			volumeMode: defaults?.pruneVolumeMode ?? 'none',
			buildCacheMode: defaults?.pruneBuildCacheMode ?? 'none',
			buildCacheUntil: defaults?.pruneBuildCacheUntil ?? ''
		} as const;
	}

	const initialSelectionInternal = getInitialSelectionInternal();

	let containerMode = $state<'none' | 'stopped' | 'olderThan'>(initialSelectionInternal.containerMode);
	let containerUntil = $state(initialSelectionInternal.containerUntil);
	let imageMode = $state<'none' | 'dangling' | 'all' | 'olderThan'>(initialSelectionInternal.imageMode);
	let imageUntil = $state(initialSelectionInternal.imageUntil);
	let networkMode = $state<'none' | 'unused' | 'olderThan'>(initialSelectionInternal.networkMode);
	let networkUntil = $state(initialSelectionInternal.networkUntil);
	let volumeMode = $state<'none' | 'anonymous' | 'all'>(initialSelectionInternal.volumeMode);
	let buildCacheMode = $state<'none' | 'unused' | 'all' | 'olderThan'>(initialSelectionInternal.buildCacheMode);
	let buildCacheUntil = $state(initialSelectionInternal.buildCacheUntil);

	const containerModes = [
		{ value: 'none', label: m.none() },
		{ value: 'stopped', label: m.prune_stopped_containers() },
		{ value: 'olderThan', label: m.prune_mode_older_than() }
	];
	const imageModes = [
		{ value: 'none', label: m.none() },
		{ value: 'dangling', label: m.prune_images_mode_dangling() },
		{ value: 'all', label: m.all_unused() },
		{ value: 'olderThan', label: m.prune_mode_older_than() }
	];
	const networkModes = [
		{ value: 'none', label: m.none() },
		{ value: 'unused', label: m.unused_networks() },
		{ value: 'olderThan', label: m.prune_mode_older_than() }
	];
	const volumeModes = [
		{ value: 'none', label: m.none() },
		{ value: 'anonymous', label: m.prune_volumes_mode_anonymous(), destructive: true },
		{ value: 'all', label: m.all_unused(), destructive: true }
	];
	const buildCacheModes = [
		{ value: 'none', label: m.none() },
		{ value: 'unused', label: m.prune_build_cache_mode_unused() },
		{ value: 'all', label: m.prune_build_cache_mode_all() },
		{ value: 'olderThan', label: m.prune_mode_older_than() }
	];

	function buildPruneRequestInternal(): SystemPruneRequest {
		const request: SystemPruneRequest = {};
		if (containerMode !== 'none') {
			request.containers = {
				mode: containerMode,
				...(containerMode === 'olderThan' ? { until: containerUntil } : {})
			};
		}
		if (imageMode !== 'none') {
			request.images = {
				mode: imageMode,
				...(imageMode === 'olderThan' ? { until: imageUntil } : {})
			};
		}
		if (networkMode !== 'none') {
			request.networks = {
				mode: networkMode,
				...(networkMode === 'olderThan' ? { until: networkUntil } : {})
			};
		}
		if (volumeMode !== 'none') {
			request.volumes = { mode: volumeMode };
		}
		if (buildCacheMode !== 'none') {
			request.buildCache = {
				mode: buildCacheMode,
				...(buildCacheMode === 'olderThan' ? { until: buildCacheUntil } : {})
			};
		}
		return request;
	}

	let selectedCountInternal = $derived(Object.keys(buildPruneRequestInternal()).length);

	function handleConfirmInternal() {
		const pruneRequest = buildPruneRequestInternal();
		if (Object.keys(pruneRequest).length > 0 && !isPruning) {
			onConfirm(pruneRequest);
		}
	}

	function handleCancelInternal() {
		if (!isPruning) {
			onCancel();
		}
	}
</script>

<div class="space-y-4">
	<SettingsSection>
		<SettingsRow label={m.containers()} description={m.scheduled_prune_containers_description()} layout="wide">
			<PruneModePicker
				modeOptions={containerModes}
				bind:value={containerMode}
				bind:untilValue={containerUntil}
				disabled={isPruning}
			/>
		</SettingsRow>
		<SettingsRow label={m.images()} description={m.prune_images_dialog_description()} layout="wide">
			<PruneModePicker modeOptions={imageModes} bind:value={imageMode} bind:untilValue={imageUntil} disabled={isPruning} />
		</SettingsRow>
		<SettingsRow label={m.resource_networks_cap()} description={m.scheduled_prune_networks_description()} layout="wide">
			<PruneModePicker modeOptions={networkModes} bind:value={networkMode} bind:untilValue={networkUntil} disabled={isPruning} />
		</SettingsRow>
		<SettingsRow label={m.resource_volumes_cap()} description={m.prune_volumes_guidance()} layout="wide">
			<PruneModePicker
				modeOptions={volumeModes}
				bind:value={volumeMode}
				disabled={isPruning}
				warningTitle={m.prune_volumes_warning_title()}
				warningDescription={volumeMode === 'all'
					? m.prune_volumes_warning_description_all()
					: m.prune_volumes_warning_description()}
			/>
		</SettingsRow>
		<SettingsRow label={m.build_cache()} description={m.scheduled_prune_build_cache_description()} layout="wide">
			<PruneModePicker
				modeOptions={buildCacheModes}
				bind:value={buildCacheMode}
				bind:untilValue={buildCacheUntil}
				disabled={isPruning}
			/>
		</SettingsRow>
	</SettingsSection>

	<div class="grid grid-cols-2 gap-3">
		<ArcaneButton action="cancel" onclick={handleCancelInternal} disabled={isPruning} class="w-full" />
		<ArcaneButton
			action="remove"
			onclick={handleConfirmInternal}
			disabled={selectedCountInternal === 0 || isPruning}
			loading={isPruning}
			customLabel={m.prune_button({ count: selectedCountInternal })}
			loadingLabel={m.common_action_pruning()}
			class="w-full"
		/>
	</div>
</div>