Public Access
1
0

feat: add Supabase-backed local lead finder app

This commit is contained in:
pguerrerox
2026-03-26 22:55:43 +00:00
commit 0e4910805a
23 changed files with 4773 additions and 0 deletions
+216
View File
@@ -0,0 +1,216 @@
import React, { useCallback, useEffect, useState } from 'react';
import type { User } from '@supabase/supabase-js';
import { AlertCircle, CheckCircle2, History, Loader2, MapPin, Play } from 'lucide-react';
import { listSearchJobs, runSearch } from '../lib/database';
import type { SearchJob } from '../types';
interface SearchSetupProps {
user: User;
onSelectJob: (jobId: string) => void;
}
export function SearchSetup({ user, onSelectJob }: SearchSetupProps) {
const [name, setName] = useState('');
const [location, setLocation] = useState('');
const [radius, setRadius] = useState(5);
const [businessType, setBusinessType] = useState('');
const [keywords, setKeywords] = useState('');
const [isSearching, setIsSearching] = useState(false);
const [isLoadingHistory, setIsLoadingHistory] = useState(true);
const [jobs, setJobs] = useState<SearchJob[]>([]);
const [error, setError] = useState<string | null>(null);
const refreshJobs = useCallback(async () => {
setIsLoadingHistory(true);
try {
const nextJobs = await listSearchJobs(user.id, 10);
setJobs(nextJobs);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load search history.');
} finally {
setIsLoadingHistory(false);
}
}, [user.id]);
useEffect(() => {
void refreshJobs();
}, [refreshJobs]);
const handleRunSearch = async (e: React.FormEvent) => {
e.preventDefault();
setIsSearching(true);
setError(null);
try {
const response = await runSearch({
name: name.trim() || undefined,
location: location.trim(),
radiusKm: radius,
businessType: businessType.trim(),
keywords: keywords.trim() || undefined,
});
await refreshJobs();
onSelectJob(response.job.id);
} catch (err) {
setError(err instanceof Error ? err.message : 'Search failed.');
} finally {
setIsSearching(false);
}
};
return (
<div className="flex-1 overflow-y-auto p-8">
<div className="mx-auto max-w-4xl space-y-8">
<header>
<h1 className="text-3xl font-bold text-stone-900">Search Setup</h1>
<p className="mt-2 text-stone-600">Submit a search job to Supabase and review the results in the dashboard or map view.</p>
</header>
<div className="grid grid-cols-1 gap-8 md:grid-cols-3">
<div className="md:col-span-2">
<form onSubmit={handleRunSearch} className="space-y-6 rounded-2xl border border-stone-200 bg-white p-8 shadow-sm">
<div className="grid grid-cols-1 gap-6 sm:grid-cols-2">
<div className="space-y-2">
<label className="text-sm font-semibold text-stone-700">Location</label>
<div className="relative">
<MapPin className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-400" />
<input
type="text"
required
placeholder="City, address, or zip"
value={location}
onChange={(e) => setLocation(e.target.value)}
className="w-full rounded-xl border border-stone-200 bg-stone-50 py-2 pl-10 pr-4 outline-none transition-all focus:border-emerald-500 focus:ring-2 focus:ring-emerald-500"
/>
</div>
</div>
<div className="space-y-2">
<label className="text-sm font-semibold text-stone-700">Radius (km)</label>
<input
type="number"
min="1"
max="50"
value={radius}
onChange={(e) => setRadius(Number.parseInt(e.target.value, 10) || 1)}
className="w-full rounded-xl border border-stone-200 bg-stone-50 px-4 py-2 outline-none transition-all focus:border-emerald-500 focus:ring-2 focus:ring-emerald-500"
/>
</div>
<div className="space-y-2">
<label className="text-sm font-semibold text-stone-700">Business Type</label>
<input
type="text"
required
placeholder="e.g. coffee shop, plumber"
value={businessType}
onChange={(e) => setBusinessType(e.target.value)}
className="w-full rounded-xl border border-stone-200 bg-stone-50 px-4 py-2 outline-none transition-all focus:border-emerald-500 focus:ring-2 focus:ring-emerald-500"
/>
</div>
<div className="space-y-2">
<label className="text-sm font-semibold text-stone-700">Keywords</label>
<input
type="text"
placeholder="e.g. organic, emergency, family-owned"
value={keywords}
onChange={(e) => setKeywords(e.target.value)}
className="w-full rounded-xl border border-stone-200 bg-stone-50 px-4 py-2 outline-none transition-all focus:border-emerald-500 focus:ring-2 focus:ring-emerald-500"
/>
</div>
</div>
<div className="space-y-2">
<label className="text-sm font-semibold text-stone-700">Job Name</label>
<input
type="text"
placeholder="Give this search a memorable name"
value={name}
onChange={(e) => setName(e.target.value)}
className="w-full rounded-xl border border-stone-200 bg-stone-50 px-4 py-2 outline-none transition-all focus:border-emerald-500 focus:ring-2 focus:ring-emerald-500"
/>
</div>
<div className="rounded-xl border border-stone-200 bg-stone-50 px-4 py-3 text-sm text-stone-600">
Searches now run through a Supabase Edge Function, so the browser no longer writes leads directly into the database.
</div>
{error && (
<div className="flex items-center gap-3 rounded-xl border border-red-100 bg-red-50 p-4 text-sm text-red-700">
<AlertCircle className="h-5 w-5 flex-shrink-0" />
<span>{error}</span>
</div>
)}
<button
type="submit"
disabled={isSearching}
className="flex w-full items-center justify-center gap-2 rounded-xl bg-emerald-600 py-3 font-semibold text-white shadow-sm transition-all hover:bg-emerald-700 disabled:cursor-not-allowed disabled:opacity-50"
>
{isSearching ? (
<>
<Loader2 className="h-5 w-5 animate-spin" />
Running Search...
</>
) : (
<>
<Play className="h-5 w-5" />
Run Search
</>
)}
</button>
</form>
</div>
<div className="space-y-4">
<div className="flex items-center gap-2 font-bold text-stone-900">
<History className="h-5 w-5" />
<h2>Recent Searches</h2>
</div>
{isLoadingHistory ? (
<div className="flex items-center gap-2 rounded-xl border border-stone-200 bg-white p-4 text-sm text-stone-500 shadow-sm">
<Loader2 className="h-4 w-4 animate-spin" />
Loading history...
</div>
) : jobs.length === 0 ? (
<p className="text-sm italic text-stone-500">No search history yet.</p>
) : (
<div className="space-y-3">
{jobs.map((job) => (
<button
key={job.id}
onClick={() => onSelectJob(job.id)}
className="group w-full space-y-2 rounded-xl border border-stone-200 bg-white p-4 text-left shadow-sm transition-all hover:border-emerald-500 hover:shadow-md"
>
<div className="flex items-start justify-between gap-2">
<h3 className="truncate text-sm font-bold text-stone-900 group-hover:text-emerald-700">{job.name}</h3>
{job.status === 'completed' ? (
<CheckCircle2 className="h-4 w-4 flex-shrink-0 text-emerald-500" />
) : job.status === 'running' ? (
<Loader2 className="h-4 w-4 flex-shrink-0 animate-spin text-emerald-500" />
) : (
<AlertCircle className="h-4 w-4 flex-shrink-0 text-red-500" />
)}
</div>
<div className="flex items-center gap-2 text-xs text-stone-500">
<MapPin className="h-3 w-3" />
<span className="truncate">{job.city} ({job.radiusKm}km)</span>
</div>
<div className="flex items-center justify-between border-t border-stone-50 pt-2">
<span className="text-xs font-medium text-stone-400">{new Date(job.createdAt).toLocaleDateString()}</span>
<span className="text-xs font-bold text-emerald-600">{job.totalResults} leads</span>
</div>
</button>
))}
</div>
)}
</div>
</div>
</div>
</div>
);
}