import { Decimal } from 'decimal.js'; import { useState, useEffect } from 'react'; import { generateSecretKey } from 'nostr-tools'; import { nip19 } from 'nostr-tools'; import { Bot, Check, Sparkles, ArrowRight, ArrowLeft } from 'lucide-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Skeleton } from '@/components/ui/skeleton'; import { ScrollArea } from '@/components/ui/scroll-area'; import { useAISettings } from '@/hooks/useAISettings'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useLoginActions } from '@/hooks/useLoginActions'; import { useProviderModels } from '@/hooks/useProviderModels'; import { CreditsDialog } from '@/components/CreditsDialog'; interface OnboardingDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } type OnboardingStep = 'welcome' | 'model-selection' | 'conclusion'; const SHAKESPEARE_PROVIDER = { id: "shakespeare", baseURL: "https://ai.shakespeare.diy/v1", nostr: true, }; export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps) { const [step, setStep] = useState('welcome'); const [selectedModel, setSelectedModel] = useState(''); const [isSettingUp, setIsSettingUp] = useState(false); const [showCreditsDialog, setShowCreditsDialog] = useState(false); const { setProvider, addRecentlyUsedModel } = useAISettings(); const { user } = useCurrentUser(); const login = useLoginActions(); const { models, isLoading: isLoadingModels } = useProviderModels(); // Filter models to only show Shakespeare provider models const shakespeareModels = models.filter(model => model.provider === 'shakespeare'); const handleGetStarted = async () => { setIsSettingUp(true); try { // If user is not logged in, generate and login with secret key if (!user) { const secretKey = generateSecretKey(); const nsec = nip19.nsecEncode(secretKey); login.nsec(nsec); } // Add Shakespeare provider to their config setProvider(SHAKESPEARE_PROVIDER); // Move to model selection setStep('model-selection'); } catch (error) { console.error('Failed to set up user:', error); } finally { setIsSettingUp(false); } }; const handleModelSelect = (modelFullId: string) => { setSelectedModel(modelFullId); }; const handleContinue = () => { if (!selectedModel) return; // Add model to recently used addRecentlyUsedModel(selectedModel); // Find the selected model to check if it's free const model = shakespeareModels.find(m => m.fullId === selectedModel); const isFreeModel = !model?.pricing || ( model.pricing.prompt.equals(0) && model.pricing.completion.equals(0) ); if (isFreeModel) { // Free model, go straight to conclusion setStep('conclusion'); } else { // Paid model, show credits dialog setShowCreditsDialog(true); } }; const handleCreditsDialogClose = () => { setShowCreditsDialog(false); setStep('conclusion'); }; const handleFinish = () => { onOpenChange(false); }; const formatPrice = (price: Decimal | undefined) => { if (!price) return 'unknown'; if (price.equals(0)) return 'Free'; // Convert from per-token to per-1M tokens and format with $ prefix const pricePerMillion = price.times(1_000_000); return '$' + pricePerMillion.toFixed(2).toString().replace(/\.00$/, '') + '/M tokens'; }; // Reset state when dialog opens/closes useEffect(() => { if (open) { setStep('welcome'); setSelectedModel(''); setIsSettingUp(false); setShowCreditsDialog(false); } }, [open]); return ( <> {step === 'model-selection' && ( )} {step === 'welcome' && 'Welcome to Shakespeare!'} {step === 'model-selection' && 'Choose Your AI Model'} {step === 'conclusion' && "You're All Set!"}
{step === 'welcome' && (
🎭

Welcome to Shakespeare!

Your AI-powered development assistant for building custom Nostr applications. Simply describe what you want to build, and AI will help you create it.

💡

AI-Powered Development

âš¡

Nostr Integration

🚀

Instant Preview

)} {step === 'model-selection' && (

Choose an AI model to power your development experience. Different models offer various capabilities and pricing.

{isLoadingModels ? ( <> {Array.from({ length: 3 }).map((_, i) => (
))} ) : shakespeareModels.length === 0 ? (

No models available. Please check your connection and try again.

) : ( shakespeareModels.map((model) => { const isSelected = selectedModel === model.fullId; const isFree = model.pricing?.prompt.equals(0) && model.pricing?.completion.equals(0); return ( handleModelSelect(model.fullId)} >
{model.name ?? model.id}
{isFree && ( Free )} {isSelected && (
)}
{model.description && (

{model.description}

)}
{model.pricing && ( <> Input: {formatPrice(model.pricing.prompt)} Output: {formatPrice(model.pricing.completion)} )} {model.contextLength && ( Context: {model.contextLength.toLocaleString()} tokens )}
); }) )}
)} {step === 'conclusion' && (
🎉

Now you're ready to build!

Your AI assistant is configured and ready to help. Just enter your prompt to start building amazing Nostr applications.

"Create a farming equipment marketplace for local farmers to buy and sell tractors, tools, and supplies..."

)}
{/* Credits Dialog */} {showCreditsDialog && selectedModel && ( { if (!open) { handleCreditsDialogClose(); } }} providerId="shakespeare" connection={SHAKESPEARE_PROVIDER} /> )} ); }