import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Header } from '@/components/Header'; import { Footer } from '@/components/Footer'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { Separator } from '@/components/ui/separator'; import { useAuth } from '@/contexts/AuthContext'; import { showSuccess, showError } from '@/utils/toast'; import { generateId } from '@/lib/utils'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { vscDarkPlus, vs } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { useTheme } from '@/contexts/ThemeContext'; import { Code2, FileText, Monitor, Globe, Plus, X, Save, Eye } from 'lucide-react'; export default function SubmitScript() { const { user } = useAuth(); const navigate = useNavigate(); const { resolvedTheme } = useTheme(); const [isLoading, setIsLoading] = useState(false); const [showPreview, setShowPreview] = useState(false); const [formData, setFormData] = useState({ name: '', description: '', code: '', installation: '', usage: '', compatibleOs: [] as string[], categories: [] as string[], tags: [] as string[], gitRepositoryUrl: '', version: '1.0.0', license: 'MIT', readme: '' }); const [errors, setErrors] = useState>({}); const [newTag, setNewTag] = useState(''); const [newCategory, setNewCategory] = useState(''); const operatingSystems = ['Linux', 'macOS', 'Windows', 'BSD', 'Android']; const availableCategories = ['DevOps', 'Automation', 'System Admin', 'Development', 'Security', 'Networking', 'Data Processing', 'Web Development', 'Mobile Development', 'Game Development']; // State for preview toggles const [showInstallationPreview, setShowInstallationPreview] = useState(false); const [showUsagePreview, setShowUsagePreview] = useState(false); const licenses = ['MIT', 'Apache 2.0', 'GPL v3', 'GPL v2', 'BSD 3-Clause', 'BSD 2-Clause', 'Unlicense', 'Custom']; const validateForm = () => { const newErrors: Record = {}; if (!formData.name.trim()) { newErrors.name = 'Script name is required'; } else if (formData.name.length < 3) { newErrors.name = 'Script name must be at least 3 characters'; } if (!formData.description.trim()) { newErrors.description = 'Description is required'; } else if (formData.description.length < 20) { newErrors.description = 'Description must be at least 20 characters'; } if (!formData.code.trim()) { newErrors.code = 'Script code is required'; } if (formData.compatibleOs.length === 0) { newErrors.compatibleOs = 'At least one compatible OS is required'; } if (formData.categories.length === 0) { newErrors.categories = 'At least one category is required'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } setIsLoading(true); try { // Simulate API call await new Promise(resolve => setTimeout(resolve, 2000)); // In a real app, this would send the data to your backend const scriptData = { id: generateId(), ...formData, authorId: user?.id || '', authorName: user?.username || '', createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), isApproved: false, isPublic: false, viewCount: 0, downloadCount: 0, rating: 0, ratingCount: 0 }; // Save to localStorage for demo purposes const existingScripts = JSON.parse(localStorage.getItem('scripts') || '[]'); existingScripts.push(scriptData); localStorage.setItem('scripts', JSON.stringify(existingScripts)); showSuccess('Script submitted successfully! It will be reviewed by our team.'); navigate('/my-scripts'); } catch (error) { showError('Failed to submit script. Please try again.'); } finally { setIsLoading(false); } }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); // Clear error when user starts typing if (errors[name]) { setErrors(prev => ({ ...prev, [name]: '' })); } }; const handleSelectChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const toggleOs = (os: string) => { setFormData(prev => ({ ...prev, compatibleOs: prev.compatibleOs.includes(os) ? prev.compatibleOs.filter(o => o !== os) : [...prev.compatibleOs, os] })); }; const toggleCategory = (category: string) => { setFormData(prev => ({ ...prev, categories: prev.categories.includes(category) ? prev.categories.filter(c => c !== category) : [...prev.categories, category] })); }; const addTag = () => { if (newTag.trim() && !formData.tags.includes(newTag.trim())) { setFormData(prev => ({ ...prev, tags: [...prev.tags, newTag.trim()] })); setNewTag(''); } }; const removeTag = (tagToRemove: string) => { setFormData(prev => ({ ...prev, tags: prev.tags.filter(tag => tag !== tagToRemove) })); }; const addCategory = () => { if (newCategory.trim() && !formData.categories.includes(newCategory.trim())) { setFormData(prev => ({ ...prev, categories: [...prev.categories, newCategory.trim()] })); setNewCategory(''); } }; if (!user) { return (
{}} />

Authentication Required

Please log in to submit a script.

); } return (
{}} />
{/* Header */}

Submit Your Script

Share your automation scripts with the community. Make sure to provide clear documentation and examples.

{/* Form */} Script Information Fill in the details below to submit your script for review
{/* Basic Information */}

Basic Information

{errors.name && (

{errors.name}

)}