feat: Implement data source management and analysis flow, allowing users to add and analyze websites for project opportunities.

This commit is contained in:
2026-02-03 20:35:03 +00:00
parent 885bbbf954
commit c47614bc66
9 changed files with 587 additions and 54 deletions

View File

@@ -1,20 +1,36 @@
"use client"
import { useQuery } from "convex/react"
import { useState } from "react"
import { api } from "@/convex/_generated/api"
import { useProject } from "@/components/project-context"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import { useMutation } from "convex/react"
export default function Page() {
const { selectedProjectId } = useProject()
const projects = useQuery(api.projects.getProjects)
const dataSources = useQuery(
api.dataSources.getProjectDataSources,
selectedProjectId ? { projectId: selectedProjectId as any } : "skip"
)
const searchContext = useQuery(
api.projects.getSearchContext,
selectedProjectId ? { projectId: selectedProjectId as any } : "skip"
)
const updateDataSourceStatus = useMutation(api.dataSources.updateDataSourceStatus)
const createAnalysis = useMutation(api.analyses.createAnalysis)
const [reanalyzingId, setReanalyzingId] = useState<string | null>(null)
const analysis = useQuery(
api.analyses.getLatestByProject,
selectedProjectId ? { projectId: selectedProjectId as any } : "skip"
)
const selectedProject = projects?.find((project) => project._id === selectedProjectId)
const selectedSourceIds = selectedProject?.dorkingConfig?.selectedSourceIds || []
const isLoading = selectedProjectId && analysis === undefined
if (!selectedProjectId && projects && projects.length === 0) {
@@ -47,6 +63,53 @@ export default function Page() {
)
}
const handleReanalyze = async (source: any) => {
if (!selectedProjectId) return
setReanalyzingId(source._id)
await updateDataSourceStatus({
dataSourceId: source._id,
analysisStatus: "pending",
lastError: undefined,
lastAnalyzedAt: undefined,
})
try {
const response = await fetch("/api/analyze", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url: source.url }),
})
const data = await response.json()
if (!response.ok) {
await updateDataSourceStatus({
dataSourceId: source._id,
analysisStatus: "failed",
lastError: data.error || "Analysis failed",
lastAnalyzedAt: Date.now(),
})
return
}
await createAnalysis({
projectId: selectedProjectId as any,
dataSourceId: source._id,
analysis: data.data,
})
await updateDataSourceStatus({
dataSourceId: source._id,
analysisStatus: "completed",
lastError: undefined,
lastAnalyzedAt: Date.now(),
})
} finally {
setReanalyzingId(null)
}
}
return (
<div className="flex flex-1 flex-col gap-6 p-4 lg:p-8">
<div className="space-y-2">
@@ -60,6 +123,14 @@ export default function Page() {
<p className="max-w-3xl text-sm text-muted-foreground">{analysis.description}</p>
</div>
{searchContext?.missingSources?.length > 0 && (
<Alert>
<AlertDescription>
Some selected sources don&apos;t have analysis yet. Run onboarding or re-analyze them for best results.
</AlertDescription>
</Alert>
)}
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-5">
<Card>
<CardHeader className="pb-2">
@@ -93,6 +164,53 @@ export default function Page() {
</Card>
</div>
<Card>
<CardHeader>
<CardTitle className="text-base">Data Sources</CardTitle>
</CardHeader>
<CardContent className="space-y-2 text-sm text-muted-foreground">
{dataSources && dataSources.length > 0 ? (
dataSources.map((source: any) => (
<div key={source._id} className="flex items-center justify-between gap-3">
<span className="truncate">{source.name || source.url}</span>
<div className="flex items-center gap-2">
<Badge variant={selectedSourceIds.includes(source._id) ? "secondary" : "outline"}>
{selectedSourceIds.includes(source._id) ? "active" : "inactive"}
</Badge>
<Badge variant={source.analysisStatus === "completed" ? "secondary" : "outline"}>
{source.analysisStatus}
</Badge>
<Button
size="sm"
variant="outline"
onClick={() => handleReanalyze(source)}
disabled={reanalyzingId === source._id}
>
{reanalyzingId === source._id ? "Analyzing..." : "Re-analyze"}
</Button>
</div>
</div>
))
) : (
<p>No data sources yet. Add a source during onboarding.</p>
)}
</CardContent>
</Card>
{searchContext?.context && (
<Card>
<CardHeader>
<CardTitle className="text-base">Aggregated Context</CardTitle>
</CardHeader>
<CardContent className="grid gap-3 text-sm text-muted-foreground md:grid-cols-2">
<div>Keywords: <span className="text-foreground font-medium">{searchContext.context.keywords.length}</span></div>
<div>Problems: <span className="text-foreground font-medium">{searchContext.context.problemsSolved.length}</span></div>
<div>Competitors: <span className="text-foreground font-medium">{searchContext.context.competitors.length}</span></div>
<div>Use Cases: <span className="text-foreground font-medium">{searchContext.context.useCases.length}</span></div>
</CardContent>
</Card>
)}
<div className="grid gap-4 lg:grid-cols-2">
<Card>
<CardHeader>