|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog"; |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { supabase } from "@/integrations/supabase/client"; |
| 5 | +import { Send, CheckCircle, RefreshCw, Copy, Plus } from "lucide-react"; |
| 6 | +import { toast } from "sonner"; |
| 7 | + |
| 8 | +interface TelegramLinkingDialogProps { |
| 9 | + isOpen: boolean; |
| 10 | + onClose: () => void; |
| 11 | +} |
| 12 | + |
| 13 | +const TelegramLinkingDialog = ({ isOpen, onClose }: TelegramLinkingDialogProps) => { |
| 14 | + const [loading, setLoading] = useState(false); |
| 15 | + const [profile, setProfile] = useState<any>(null); |
| 16 | + const [generating, setGenerating] = useState(false); |
| 17 | + |
| 18 | + const fetchProfile = async () => { |
| 19 | + setLoading(true); |
| 20 | + const { data: { user } } = await supabase.auth.getUser(); |
| 21 | + if (user) { |
| 22 | + const { data, error } = await supabase |
| 23 | + .from('profiles') |
| 24 | + .select('*') |
| 25 | + .eq('id', user.id) |
| 26 | + .single(); |
| 27 | + |
| 28 | + if (data) { |
| 29 | + setProfile(data); |
| 30 | + } else if (error && error.code === 'PGRST116') { // No rows found |
| 31 | + // Create a default profile if it doesn't exist |
| 32 | + const defaultUsername = user.email?.split('@')[0] || "user_" + user.id.slice(0, 5); |
| 33 | + const { data: newProfile, error: insertError } = await supabase |
| 34 | + .from('profiles') |
| 35 | + .insert({ id: user.id, username: defaultUsername }) |
| 36 | + .select() |
| 37 | + .single(); |
| 38 | + |
| 39 | + if (newProfile) { |
| 40 | + setProfile(newProfile); |
| 41 | + } else if (insertError) { |
| 42 | + console.error("Error creating profile:", insertError); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + setLoading(false); |
| 47 | + }; |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (isOpen) { |
| 51 | + fetchProfile(); |
| 52 | + } |
| 53 | + }, [isOpen]); |
| 54 | + |
| 55 | + const generateCode = async () => { |
| 56 | + setGenerating(true); |
| 57 | + const code = Math.random().toString(36).substring(2, 8).toUpperCase(); |
| 58 | + const { data: { user } } = await supabase.auth.getUser(); |
| 59 | + |
| 60 | + if (user) { |
| 61 | + const { error } = await supabase |
| 62 | + .from('profiles') |
| 63 | + .update({ link_code: code }) |
| 64 | + .eq('id', user.id); |
| 65 | + |
| 66 | + if (error) { |
| 67 | + toast.error("Failed to generate code: " + error.message); |
| 68 | + } else { |
| 69 | + setProfile({ ...profile, link_code: code }); |
| 70 | + toast.success("New linking code generated!"); |
| 71 | + } |
| 72 | + } |
| 73 | + setGenerating(false); |
| 74 | + }; |
| 75 | + |
| 76 | + const copyToClipboard = (text: string) => { |
| 77 | + navigator.clipboard.writeText(text); |
| 78 | + toast.success("Code copied to clipboard!"); |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Dialog open={isOpen} onOpenChange={onClose}> |
| 83 | + <DialogContent className="sm:max-width-[425px]"> |
| 84 | + <DialogHeader> |
| 85 | + <DialogTitle className="flex items-center gap-2"> |
| 86 | + <Send className="h-5 w-5 text-sky-500" /> |
| 87 | + Link Telegram Bot |
| 88 | + </DialogTitle> |
| 89 | + <DialogDescription> |
| 90 | + Connect your account to the Maantis Telegram bot to schedule events via chat, voice, or photos. |
| 91 | + </DialogDescription> |
| 92 | + </DialogHeader> |
| 93 | + |
| 94 | + <div className="py-6 flex flex-col items-center gap-6"> |
| 95 | + {loading ? ( |
| 96 | + <RefreshCw className="h-8 w-8 animate-spin text-muted-foreground" /> |
| 97 | + ) : !profile ? ( |
| 98 | + <div className="flex flex-col items-center gap-2 text-center"> |
| 99 | + <Plus className="h-12 w-12 text-muted-foreground" /> |
| 100 | + <h3 className="font-semibold text-lg">Login Required</h3> |
| 101 | + <p className="text-sm text-muted-foreground"> |
| 102 | + Please sign in to your account to link Telegram. |
| 103 | + </p> |
| 104 | + </div> |
| 105 | + ) : profile?.telegram_chat_id ? ( |
| 106 | + <div className="flex flex-col items-center gap-2 text-center"> |
| 107 | + <CheckCircle className="h-12 w-12 text-green-500" /> |
| 108 | + <h3 className="font-semibold text-lg">Connected!</h3> |
| 109 | + <p className="text-sm text-muted-foreground"> |
| 110 | + Your account is successfully linked to Telegram. |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + ) : ( |
| 114 | + <div className="w-full space-y-4"> |
| 115 | + <div className="bg-red-50 border border-red-100 p-3 rounded text-xs text-red-600 font-medium"> |
| 116 | + ⚠️ IMPORTANT: Do NOT send your email to the bot. Use the generated code below. |
| 117 | + </div> |
| 118 | + |
| 119 | + <div className="bg-muted p-4 rounded-lg flex flex-col items-center gap-3"> |
| 120 | + <span className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">Your Linking Code</span> |
| 121 | + {profile?.link_code ? ( |
| 122 | + <div className="flex items-center gap-3 bg-background border rounded-md px-4 py-2 w-full justify-between"> |
| 123 | + <span className="font-mono text-2xl font-bold tracking-widest">{profile.link_code}</span> |
| 124 | + <Button variant="ghost" size="icon" onClick={() => copyToClipboard(`/link ${profile.link_code}`)}> |
| 125 | + <Copy className="h-4 w-4" /> |
| 126 | + </Button> |
| 127 | + </div> |
| 128 | + ) : ( |
| 129 | + <span className="text-sm italic text-muted-foreground">No code generated yet</span> |
| 130 | + )} |
| 131 | + <Button |
| 132 | + variant="outline" |
| 133 | + size="sm" |
| 134 | + onClick={generateCode} |
| 135 | + disabled={generating} |
| 136 | + className="w-full" |
| 137 | + > |
| 138 | + {generating ? "Generating..." : profile?.link_code ? "Regenerate Code" : "Generate Code"} |
| 139 | + </Button> |
| 140 | + </div> |
| 141 | + |
| 142 | + <div className="space-y-3"> |
| 143 | + <h4 className="text-sm font-semibold">How to link:</h4> |
| 144 | + <ol className="text-sm space-y-2 list-decimal list-inside text-muted-foreground"> |
| 145 | + <li>Open the <a href="https://t.me/maantis_bot" target="_blank" className="text-primary hover:underline">Maantis Bot</a> on Telegram.</li> |
| 146 | + <li>Click **Start** or type **/start**.</li> |
| 147 | + <li>Send your linking code: <code className="bg-muted px-1 rounded">/link {profile?.link_code || "XXXXXX"}</code></li> |
| 148 | + </ol> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + )} |
| 152 | + </div> |
| 153 | + |
| 154 | + <DialogFooter> |
| 155 | + <Button onClick={onClose} variant="secondary">Close</Button> |
| 156 | + </DialogFooter> |
| 157 | + </DialogContent> |
| 158 | + </Dialog> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export default TelegramLinkingDialog; |
0 commit comments