485 lines
16 KiB
TypeScript
485 lines
16 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
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 { Skeleton } from '@/components/ui/skeleton'
|
|
import { Alert, AlertDescription } from '@/components/ui/alert'
|
|
import { ArrowRight, Globe, Loader2, Sparkles, AlertCircle, ArrowLeft } from 'lucide-react'
|
|
import type { EnhancedProductAnalysis, Keyword } from '@/lib/types'
|
|
import { useMutation } from 'convex/react'
|
|
import { api } from '@/convex/_generated/api'
|
|
|
|
const examples = [
|
|
{ name: 'Notion', url: 'https://notion.so' },
|
|
{ name: 'Stripe', url: 'https://stripe.com' },
|
|
{ name: 'Figma', url: 'https://figma.com' },
|
|
{ name: 'Linear', url: 'https://linear.app' },
|
|
]
|
|
|
|
export default function OnboardingPage() {
|
|
const router = useRouter()
|
|
const addDataSource = useMutation(api.dataSources.addDataSource)
|
|
const updateDataSourceStatus = useMutation(api.dataSources.updateDataSourceStatus)
|
|
const createAnalysis = useMutation(api.analyses.createAnalysis)
|
|
const [url, setUrl] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [progress, setProgress] = useState('')
|
|
const [error, setError] = useState('')
|
|
const [showManualInput, setShowManualInput] = useState(false)
|
|
|
|
// Manual input fields
|
|
const [manualProductName, setManualProductName] = useState('')
|
|
const [manualDescription, setManualDescription] = useState('')
|
|
const [manualFeatures, setManualFeatures] = useState('')
|
|
|
|
const persistAnalysis = async ({
|
|
analysis,
|
|
sourceUrl,
|
|
sourceName,
|
|
}: {
|
|
analysis: EnhancedProductAnalysis
|
|
sourceUrl: string
|
|
sourceName: string
|
|
}) => {
|
|
const { sourceId, projectId } = await addDataSource({
|
|
url: sourceUrl,
|
|
name: sourceName,
|
|
type: 'website',
|
|
})
|
|
|
|
await createAnalysis({
|
|
projectId,
|
|
dataSourceId: sourceId,
|
|
analysis,
|
|
})
|
|
|
|
await updateDataSourceStatus({
|
|
dataSourceId: sourceId,
|
|
analysisStatus: 'completed',
|
|
lastAnalyzedAt: Date.now(),
|
|
})
|
|
}
|
|
|
|
async function analyzeWebsite() {
|
|
if (!url) return
|
|
|
|
setLoading(true)
|
|
setError('')
|
|
setProgress('Scraping website...')
|
|
|
|
try {
|
|
const response = await fetch('/api/analyze', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ url }),
|
|
})
|
|
|
|
if (response.redirected) {
|
|
router.push('/auth?next=/onboarding')
|
|
return
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
if (data.needsManualInput) {
|
|
setShowManualInput(true)
|
|
setManualProductName(url.replace(/^https?:\/\//, '').replace(/\/$/, ''))
|
|
throw new Error(data.error)
|
|
}
|
|
throw new Error(data.error || 'Failed to analyze')
|
|
}
|
|
|
|
setProgress('Analyzing with AI...')
|
|
|
|
// Store in localStorage for dashboard
|
|
localStorage.setItem('productAnalysis', JSON.stringify(data.data))
|
|
localStorage.setItem('analysisStats', JSON.stringify(data.stats))
|
|
|
|
setProgress('Saving analysis...')
|
|
await persistAnalysis({
|
|
analysis: data.data,
|
|
sourceUrl: url,
|
|
sourceName: data.data.productName,
|
|
})
|
|
|
|
setProgress('Redirecting to dashboard...')
|
|
|
|
// Redirect to dashboard with product name in query
|
|
const params = new URLSearchParams({ product: data.data.productName })
|
|
router.push(`/dashboard?${params.toString()}`)
|
|
} catch (err: any) {
|
|
console.error('Analysis error:', err)
|
|
setError(err.message || 'Failed to analyze website')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
async function analyzeManually() {
|
|
if (!manualProductName || !manualDescription) return
|
|
|
|
setLoading(true)
|
|
setError('')
|
|
setProgress('Analyzing with AI...')
|
|
|
|
try {
|
|
// Create a mock analysis from manual input
|
|
const manualFeaturesList = manualFeatures
|
|
.split('\n')
|
|
.map((feature) => feature.trim())
|
|
.filter(Boolean)
|
|
|
|
const keywordSeed = manualProductName
|
|
.toLowerCase()
|
|
.split(' ')
|
|
.filter(Boolean)
|
|
|
|
const manualKeywords: Keyword[] = keywordSeed.map((term) => ({
|
|
term,
|
|
type: 'product',
|
|
searchVolume: 'low',
|
|
intent: 'informational',
|
|
funnel: 'awareness',
|
|
emotionalIntensity: 'curious',
|
|
}))
|
|
|
|
const manualAnalysis: EnhancedProductAnalysis = {
|
|
productName: manualProductName,
|
|
tagline: manualDescription.split('.')[0],
|
|
description: manualDescription,
|
|
category: '',
|
|
positioning: '',
|
|
features: manualFeaturesList.map((name) => ({
|
|
name,
|
|
description: '',
|
|
benefits: [],
|
|
useCases: [],
|
|
})),
|
|
problemsSolved: [],
|
|
personas: [],
|
|
keywords: manualKeywords,
|
|
useCases: [],
|
|
competitors: [],
|
|
dorkQueries: [],
|
|
scrapedAt: new Date().toISOString(),
|
|
analysisVersion: 'manual',
|
|
}
|
|
|
|
// Send to API to enhance with AI
|
|
const response = await fetch('/api/analyze-manual', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
productName: manualProductName,
|
|
description: manualDescription,
|
|
features: manualFeatures
|
|
}),
|
|
})
|
|
|
|
if (response.redirected) {
|
|
router.push('/auth?next=/onboarding')
|
|
return
|
|
}
|
|
|
|
let finalAnalysis = manualAnalysis
|
|
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
finalAnalysis = data.data
|
|
}
|
|
|
|
// Store in localStorage for dashboard
|
|
localStorage.setItem('productAnalysis', JSON.stringify(finalAnalysis))
|
|
localStorage.setItem('analysisStats', JSON.stringify({
|
|
features: finalAnalysis.features.length,
|
|
keywords: finalAnalysis.keywords.length,
|
|
personas: finalAnalysis.personas.length,
|
|
useCases: finalAnalysis.useCases.length,
|
|
competitors: finalAnalysis.competitors.length,
|
|
dorkQueries: finalAnalysis.dorkQueries.length
|
|
}))
|
|
|
|
setProgress('Saving analysis...')
|
|
await persistAnalysis({
|
|
analysis: finalAnalysis,
|
|
sourceUrl: 'manual-input',
|
|
sourceName: finalAnalysis.productName,
|
|
})
|
|
|
|
// Redirect to dashboard
|
|
const params = new URLSearchParams({ product: finalAnalysis.productName })
|
|
router.push(`/dashboard?${params.toString()}`)
|
|
} catch (err: any) {
|
|
console.error('Manual analysis error:', err)
|
|
setError(err.message || 'Failed to analyze')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
if (showManualInput) {
|
|
return (
|
|
<div className="flex min-h-screen w-full">
|
|
{/* Left Side - Content */}
|
|
<div className="w-full lg:w-1/2 flex items-center justify-center p-8 bg-background">
|
|
<div className="w-full max-w-lg space-y-6">
|
|
<div className="text-center space-y-2">
|
|
<div className="flex justify-center">
|
|
<div className="flex h-12 w-12 items-center justify-center rounded-xl bg-amber-500/20 text-amber-400">
|
|
<AlertCircle className="h-6 w-6" />
|
|
</div>
|
|
</div>
|
|
<h1 className="text-2xl font-bold text-foreground">Couldn't Reach Website</h1>
|
|
<p className="text-muted-foreground">
|
|
No problem! Tell us about your product and we'll analyze it manually.
|
|
</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<Alert variant="destructive">
|
|
<AlertCircle className="h-4 w-4" />
|
|
<AlertDescription>{error}</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
<Card className="border-border/50 shadow-none">
|
|
<CardHeader>
|
|
<CardTitle>Describe Your Product</CardTitle>
|
|
<CardDescription>
|
|
Enter your product details and we'll extract the key information.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="productName">Product Name *</Label>
|
|
<Input
|
|
id="productName"
|
|
placeholder="My Awesome Product"
|
|
value={manualProductName}
|
|
onChange={(e) => setManualProductName(e.target.value)}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="description">Description *</Label>
|
|
<Textarea
|
|
id="description"
|
|
placeholder="What does your product do? Who is it for? What problem does it solve?"
|
|
value={manualDescription}
|
|
onChange={(e) => setManualDescription(e.target.value)}
|
|
rows={4}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="features">Key Features (one per line)</Label>
|
|
<Textarea
|
|
id="features"
|
|
placeholder="- Feature 1 - Feature 2 - Feature 3"
|
|
value={manualFeatures}
|
|
onChange={(e) => setManualFeatures(e.target.value)}
|
|
rows={4}
|
|
/>
|
|
</div>
|
|
|
|
{loading && (
|
|
<div className="space-y-3 py-4">
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
{progress}
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Skeleton className="h-4 w-full" />
|
|
<Skeleton className="h-4 w-4/5" />
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex gap-2">
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => {
|
|
setShowManualInput(false)
|
|
setError('')
|
|
}}
|
|
disabled={loading}
|
|
className="flex-1"
|
|
>
|
|
<ArrowLeft className="mr-2 h-4 w-4" />
|
|
Back
|
|
</Button>
|
|
<Button
|
|
onClick={analyzeManually}
|
|
disabled={!manualProductName || !manualDescription || loading}
|
|
className="flex-1 gap-2"
|
|
>
|
|
{loading ? (
|
|
<>
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
Analyzing...
|
|
</>
|
|
) : (
|
|
<>
|
|
Analyze
|
|
<ArrowRight className="h-4 w-4" />
|
|
</>
|
|
)}
|
|
</Button>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right Side - Image */}
|
|
<div className="hidden lg:block lg:w-1/2 relative">
|
|
<img
|
|
src="/onboarding-bg.png"
|
|
alt="Abstract Background"
|
|
className="absolute inset-0 w-full h-full object-cover"
|
|
/>
|
|
<div className="absolute inset-0 bg-black/40" /> {/* Optional overlay for contrast if needed */}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen w-full">
|
|
{/* Left Side - Content */}
|
|
<div className="w-full lg:w-1/2 flex items-center justify-center p-8 bg-background">
|
|
<div className="w-full max-w-lg space-y-8">
|
|
{/* Header */}
|
|
<div className="text-center space-y-2">
|
|
<div className="flex justify-center">
|
|
<div className="flex h-12 w-12 items-center justify-center rounded-xl bg-primary text-primary-foreground">
|
|
<Sparkles className="h-6 w-6" />
|
|
</div>
|
|
</div>
|
|
<h1 className="text-3xl font-bold text-foreground tracking-tight">Welcome to Sanati</h1>
|
|
<p className="text-muted-foreground">
|
|
Enter your website URL and we'll analyze your product to find opportunities.
|
|
</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<Alert variant="destructive">
|
|
<AlertCircle className="h-4 w-4" />
|
|
<AlertDescription>{error}</AlertDescription>
|
|
</Alert>
|
|
)}
|
|
|
|
{/* Input Card */}
|
|
<div className="space-y-6">
|
|
<div className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="url">Website URL</Label>
|
|
<div className="relative">
|
|
<Globe className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input
|
|
id="url"
|
|
placeholder="https://yourproduct.com"
|
|
value={url}
|
|
onChange={(e) => setUrl(e.target.value)}
|
|
onKeyDown={(e) => e.key === 'Enter' && !loading && analyzeWebsite()}
|
|
className="pl-10 h-11"
|
|
disabled={loading}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{loading && (
|
|
<div className="space-y-3 py-4">
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
{progress}
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Skeleton className="h-4 w-full" />
|
|
<Skeleton className="h-4 w-4/5" />
|
|
<Skeleton className="h-4 w-3/5" />
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<Button
|
|
onClick={analyzeWebsite}
|
|
disabled={!url || loading}
|
|
className="w-full gap-2 h-11"
|
|
size="lg"
|
|
>
|
|
{loading ? (
|
|
<>
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
Analyzing...
|
|
</>
|
|
) : (
|
|
<>
|
|
Analyze Website
|
|
<ArrowRight className="h-4 w-4" />
|
|
</>
|
|
)}
|
|
</Button>
|
|
|
|
<div className="relative">
|
|
<div className="absolute inset-0 flex items-center">
|
|
<span className="w-full border-t" />
|
|
</div>
|
|
<div className="relative flex justify-center text-xs uppercase">
|
|
<span className="bg-background px-2 text-muted-foreground">
|
|
Or
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<Button
|
|
variant="outline"
|
|
size="lg"
|
|
onClick={() => setShowManualInput(true)}
|
|
className="w-full"
|
|
>
|
|
Enter details manually
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Examples */}
|
|
<div className="text-center">
|
|
<p className="text-sm text-muted-foreground mb-3">Try with an example:</p>
|
|
<div className="flex flex-wrap justify-center gap-2">
|
|
{examples.map((example) => (
|
|
<Button
|
|
key={example.url}
|
|
variant="ghost"
|
|
size="sm"
|
|
className="bg-muted/50 hover:bg-muted"
|
|
onClick={() => setUrl(example.url)}
|
|
disabled={loading}
|
|
>
|
|
{example.name}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Right Side - Image */}
|
|
<div className="hidden lg:block lg:w-1/2 relative bg-zinc-900">
|
|
<img
|
|
src="/onboarding-bg.png"
|
|
alt="Abstract Background"
|
|
className="absolute inset-0 w-full h-full object-cover opacity-90"
|
|
/>
|
|
<div className="absolute inset-0 bg-gradient-to-l from-transparent to-background/20" />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|