-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
172 lines (156 loc) · 5.88 KB
/
Copy pathApp.tsx
File metadata and controls
172 lines (156 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useEffect, useCallback } from "react";
import { motion } from "framer-motion";
import ChatList from "./components/ChatList";
import ChatWindow from "./components/ChatWindow";
import NewChatModal from "./components/NewChatModal";
import Auth from "./components/Auth";
import { useAuth } from "./components/AuthContext";
import { supabase, credentialsProvided } from "./supabaseClient";
import DarkModeToggle from "./components/DarkModeToggle";
import Header from "./components/Header";
const App: React.FC = () => {
const { user } = useAuth();
const [threads, setThreads] = useState<any[]>([]);
const [selectedThreadId, setSelectedThreadId] = useState<number | null>(null);
const [loadingThreads, setLoadingThreads] = useState(false);
const [isNewChatModalOpen, setIsNewChatModalOpen] = useState(false);
// 📡 Fetch Threads
const fetchThreads = useCallback(async () => {
if (!user) return;
setLoadingThreads(true);
const { data, error } = await supabase.rpc("get_threads_for_user");
if (error) console.error("Error fetching threads:", error.message);
setThreads(
(data || []).map((t: any) => ({
id: t.id,
otherUser: t.other_user,
lastMessage: t.last_message,
}))
);
setLoadingThreads(false);
}, [user]);
useEffect(() => {
if (user) fetchThreads();
}, [user, fetchThreads]);
const handleSelectThread = (id: number) => setSelectedThreadId(id);
const handleBackToList = () => setSelectedThreadId(null);
const handleNewChat = () => setIsNewChatModalOpen(true);
const handleNewChatCreated = (threadId: number) => {
fetchThreads().then(() => {
setSelectedThreadId(threadId);
setIsNewChatModalOpen(false);
});
};
// ❌ Missing Supabase Keys Screen
if (!credentialsProvided) {
return (
<div className="flex flex-col items-center justify-center h-screen bg-gray-100 dark:bg-zinc-900 text-center">
<div className="p-8 bg-white dark:bg-zinc-800 rounded-xl shadow-lg max-w-md">
<h2 className="text-2xl font-bold text-red-500 mb-4">⚙️ Configuration Required</h2>
<p className="text-gray-700 dark:text-gray-300">
Your Supabase credentials are missing. Please open
<code className="mx-1 bg-gray-200 dark:bg-zinc-700 rounded px-2 py-1 text-sm">supabaseClient.ts</code>
and add your project URL and anon key.
</p>
</div>
</div>
);
}
// 🔐 Auth Screen
if (!user) {
return <Auth />;
}
// 🧱 Layout
const selectedThread = threads.find((t) => t.id === selectedThreadId);
return (
<div className="flex flex-col md:flex-row h-screen w-full bg-gray-50 dark:bg-zinc-900 text-gray-800 dark:text-gray-100">
{/* Left Sidebar (Chat List) */}
<motion.div
className={`${
selectedThreadId ? "hidden md:flex" : "flex"
} flex-col w-full md:w-[360px] border-r border-gray-200 dark:border-zinc-700 bg-white dark:bg-zinc-800`}
initial={{ x: -50, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ duration: 0.3 }}
>
<div className="flex items-center justify-between px-4 py-3 border-b dark:border-zinc-700 bg-gradient-to-r from-emerald-500 to-teal-400 text-white">
<h1 className="text-lg font-semibold">💬 SupaChat+</h1>
<DarkModeToggle />
<button
onClick={handleNewChat}
className="bg-white/20 hover:bg-white/30 px-3 py-1.5 rounded-md text-sm font-medium"
>
New Chat
</button>
</div>
<ChatList
threads={threads}
loading={loadingThreads}
selectedThreadId={selectedThreadId}
onSelectThread={handleSelectThread}
onNewChat={handleNewChat}
/>
<div className="flex flex-col h-screen bg-gray-50 dark:bg-zinc-900">
<Header />
{/* The rest of your chat UI */}
</div>
</motion.div>
{/* Right Chat Window */}
<motion.div
className={`flex-grow flex-col ${
selectedThreadId ? "flex" : "hidden md:flex"
}`}
initial={{ x: 50, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ duration: 0.3 }}
>
{selectedThread ? (
<ChatWindow
thread={selectedThread}
user={user}
onBack={handleBackToList}
/>
) : (
<div className="flex flex-col items-center justify-center h-full bg-gray-50 dark:bg-zinc-900 text-gray-500">
<svg
className="w-24 h-24 text-emerald-400 mb-4"
fill="none"
stroke="currentColor"
strokeWidth={1.5}
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h8a2 2 0 012 2v2"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15.5 8.5c.33.33.5.83.5 1.33s-.17 1-.5 1.33c-.33.33-.83.5-1.33.5s-1-.17-1.33-.5c-.33-.33-.5-.83-.5-1.33s.17-1 .5-1.33c.33-.33.83-.5 1.33-.5s1 .17 1.33.5z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M20 12a8 8 0 11-8 8"
/>
</svg>
<h2 className="text-2xl font-semibold text-gray-700 dark:text-gray-300">
👋 Welcome to SupaChat+
</h2>
<p className="mt-2 text-gray-500 dark:text-gray-400">
Select a chat or start a new conversation.
</p>
</div>
)}
</motion.div>
{/* New Chat Modal */}
<NewChatModal
isOpen={isNewChatModalOpen}
onClose={() => setIsNewChatModalOpen(false)}
onThreadSelected={handleNewChatCreated}
/>
</div>
);
};
export default App;