"use client"; import Link from "next/link"; import { useEffect, useRef, useState } from "react"; import { scanOnDemand, scheduleDaily } from "@/actions/scans"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/shadcn/select/select"; import { Spinner } from "@/components/shadcn/spinner/spinner"; import { TreeStatusIcon } from "@/components/shadcn/tree-view/tree-status-icon"; import { ToastAction, useToast } from "@/components/ui"; import { useProviderWizardStore } from "@/store/provider-wizard/store"; import { SCAN_JOBS_TAB } from "@/types"; import { TREE_ITEM_STATUS } from "@/types/tree"; import { WIZARD_FOOTER_ACTION_TYPE, WizardFooterConfig, } from "./footer-controls"; const SCAN_SCHEDULE = { DAILY: "daily", SINGLE: "single", } as const; type ScanScheduleOption = (typeof SCAN_SCHEDULE)[keyof typeof SCAN_SCHEDULE]; interface LaunchStepProps { onBack: () => void; onClose: () => void; onFooterChange: (config: WizardFooterConfig) => void; } export function LaunchStep({ onBack, onClose, onFooterChange, }: LaunchStepProps) { const { toast } = useToast(); const { providerId } = useProviderWizardStore(); const [isLaunching, setIsLaunching] = useState(false); const [scheduleOption, setScheduleOption] = useState( SCAN_SCHEDULE.DAILY, ); const launchActionRef = useRef<() => void>(() => {}); const handleLaunchScan = async () => { if (!providerId) { return; } setIsLaunching(true); const formData = new FormData(); formData.set("providerId", providerId); const result = scheduleOption === SCAN_SCHEDULE.DAILY ? await scheduleDaily(formData) : await scanOnDemand(formData); if (result?.error) { setIsLaunching(false); toast({ variant: "destructive", title: "Unable to launch scan", description: String(result.error), }); return; } setIsLaunching(false); onClose(); toast({ title: "Scan Launched", description: scheduleOption === SCAN_SCHEDULE.DAILY ? "Daily scan scheduled successfully." : "Single scan launched successfully.", action: ( Go to scans ), }); }; launchActionRef.current = () => { void handleLaunchScan(); }; useEffect(() => { onFooterChange({ showBack: true, backLabel: "Back", backDisabled: isLaunching, onBack, showAction: true, actionLabel: isLaunching ? "Launching scans..." : "Launch scan", actionDisabled: isLaunching || !providerId, actionType: WIZARD_FOOTER_ACTION_TYPE.BUTTON, onAction: () => { launchActionRef.current(); }, }); }, [isLaunching, onBack, onFooterChange, providerId]); if (isLaunching) { return (

Launching scans...

); } return (

Connection validated!

Choose how you want to launch scans for this provider.

{!providerId && (

Provider data is missing. Go back and test the connection again.

)}

Scan schedule

); }