import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import Layout from '../../components/Layout'; import { supabase } from '../../lib/supabase'; import { useAuth } from '../../context/AuthContext'; export default function NewProject() { const { currentUser } = useAuth(); const navigate = useNavigate(); const companyOptions = currentUser.companies?.length ? currentUser.companies : (currentUser.company ? [currentUser.company] : []); const [selectedCompanyId, setSelectedCompanyId] = useState(companyOptions[0]?.id || ''); const [name, setName] = useState(''); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); if (!companyOptions.length) { return ( Account Not Yet Active Your account hasn't been linked to a company yet. Please contact the Fourge team to get set up. ); } const handleSubmit = async (e) => { e.preventDefault(); if (saving) return; const trimmedName = name.trim(); if (!trimmedName) return; setSaving(true); setError(null); const { data, error: insertError } = await supabase .from('projects') .insert({ company_id: selectedCompanyId, name: trimmedName }) .select('id') .single(); if (insertError) { setError(insertError.message); setSaving(false); return; } navigate(`/my-projects/${data.id}`); }; return ( New Project Create a project to organise your work. {companyOptions.length > 1 && ( Company * setSelectedCompanyId(e.target.value)} required > {companyOptions.map(c => {c.name})} )} Project Name * setName(e.target.value)} autoFocus required /> {error && ( {error} )} {saving ? 'Creating...' : 'Create Project'} navigate('/my-projects')}> Cancel ); }
Your account hasn't been linked to a company yet. Please contact the Fourge team to get set up.