'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/lib/auth'; import { limqRequest } from '@/lib/api'; import { TeamSelector } from '@/app/components/ui/TeamSelector'; import { ProjectSelector } from '@/app/components/ui/ProjectSelector'; import ClientRouteGuard from '@/app/components/ClientRouteGuard'; interface ShortUrlData { originalUrl: string; customSlug?: string; title: string; description?: string; tags?: string[]; teamId: string; projectId: string; domain: string; } export default function CreateShortUrlPage() { return ( ); } function CreateShortUrlForm() { const router = useRouter(); const { user } = useAuth(); const [formData, setFormData] = useState({ originalUrl: '', customSlug: '', title: '', description: '', tags: [], teamId: '', projectId: '', domain: 'googleads.link', }); const [tagInput, setTagInput] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); // Use useEffect to add user information to form data on load useEffect(() => { if (user) { console.log('Current user:', user.email); // Can add user-related data to the form here } }, [user]); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleTagKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && tagInput.trim()) { e.preventDefault(); addTag(); } }; const addTag = () => { if (tagInput.trim() && !formData.tags?.includes(tagInput.trim())) { setFormData(prev => ({ ...prev, tags: [...(prev.tags || []), tagInput.trim()] })); setTagInput(''); } }; const removeTag = (tagToRemove: string) => { setFormData(prev => ({ ...prev, tags: prev.tags?.filter(tag => tag !== tagToRemove) })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setError(null); try { // Validate required fields if (!formData.originalUrl) { throw new Error('Original URL is required'); } if (!formData.title) { throw new Error('Title is required'); } if (!formData.teamId) { throw new Error('Team is required'); } if (!formData.projectId) { throw new Error('Project is required'); } if (!formData.domain) { throw new Error('Domain is required'); } // Construct request data according to API requirements const requestData = { type: "shorturl", attributes: { // Can add any additional attributes, but attributes cannot be empty icon: "" }, shortUrl: { url: formData.originalUrl, slug: formData.customSlug || undefined, title: formData.title, name: formData.title, description: formData.description || "", domain: formData.domain }, teamId: formData.teamId, projectId: formData.projectId, tagIds: formData.tags && formData.tags.length > 0 ? formData.tags : undefined }; // Call API to create shorturl resource const response = await limqRequest('resource/shorturl', 'POST', requestData as unknown as Record); console.log('Created successfully:', response); setSuccess(true); // Redirect to links list page after 2 seconds setTimeout(() => { router.push('/links'); }, 2000); } catch (err) { console.error('Failed to create short URL:', err); setError(err instanceof Error ? err.message : 'Failed to create short URL, please try again later'); } finally { setIsSubmitting(false); } }; return (

Create Short URL

Create a new short URL resource for tracking and analytics

{error && (

{error}

)} {success && (

Short URL created successfully! Redirecting...

)}
{/* Title */}
{/* Original URL */}
{/* Custom Short Link */}
{formData.domain}/

Leave blank to generate a random short link

{/* Domain */}
{/* Team Selection */}
{ setFormData(prev => ({ ...prev, teamId: teamId as string, // Clear selected project when team changes projectId: '' })); }} />
{/* Project Selection */}
{ setFormData(prev => ({ ...prev, projectId: projectId as string })); }} />
{/* Description */}