Skip to content

Commit 48f56d3

Browse files
fixed client side duplicates
1 parent af4cf0c commit 48f56d3

File tree

8 files changed

+48
-34
lines changed

8 files changed

+48
-34
lines changed

app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Message, toAISDKMessages, toMessageImage } from '@/lib/messages'
1515
import { LLMModelConfig } from '@/lib/models'
1616
import modelsList from '@/lib/models.json'
1717
import { FragmentSchema, fragmentSchema as schema } from '@/lib/schema'
18-
import { createBrowserClient } from '@/lib/supabase-client'
18+
import { createSupabaseBrowserClient } from '@/lib/supabase-browser'
1919
import templates, { TemplateId, Templates } from '@/lib/templates'
2020
import { ExecutionResult } from '@/lib/types'
2121
import { cn } from '@/lib/utils'
@@ -28,7 +28,7 @@ import { useEnhancedChat } from '@/hooks/use-enhanced-chat'
2828
import { HeroPillSecond } from '@/components/announcement'
2929

3030
export default function Home() {
31-
const supabase = createBrowserClient()
31+
const supabase = createSupabaseBrowserClient()
3232
const [chatInput, setChatInput] = useLocalStorage('chat', '')
3333
const [files, setFiles] = useState<File[]>([])
3434
const [selectedTemplate, setSelectedTemplate] = useState<'auto' | TemplateId>('auto')

app/settings/account/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
1111
import { AlertTriangle, Camera, Mail, Shield, Loader2 } from 'lucide-react'
1212
import { useState, useEffect } from 'react'
1313
import { useAuth } from '@/lib/auth'
14-
import { createBrowserClient } from '@/lib/supabase-client'
14+
import { createSupabaseBrowserClient } from '@/lib/supabase-browser'
1515
import {
1616
getUserPreferences,
1717
getUserSecuritySettings,
@@ -24,7 +24,7 @@ import {
2424
export default function AccountSettings() {
2525
const { session } = useAuth(() => {}, () => {})
2626
const { toast } = useToast()
27-
const supabase = createBrowserClient()
27+
const supabase = createSupabaseBrowserClient()
2828

2929
// Form state
3030
const [email, setEmail] = useState('')

components/sidebar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Link from 'next/link';
33
import { X, MessageCircle, Search, Gift, Settings, HelpCircle, CreditCard, User, LogOut, MoreHorizontal, Menu, Plus, Trash2, CornerUpLeft } from 'lucide-react';
4-
import type { User as SupabaseUser } from '@supabase/supabase-js';
4+
import type { User as SupabaseUser, Session, AuthChangeEvent } from '@supabase/supabase-js';
55
import {
66
DropdownMenu,
77
DropdownMenuContent,
@@ -14,7 +14,7 @@ import { Button } from '@/components/ui/button';
1414
import { Input } from '@/components/ui/input';
1515
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
1616
import { Separator } from '@/components/ui/separator';
17-
import { createBrowserClient } from '@/lib/supabase-client';
17+
import { createSupabaseBrowserClient } from '@/lib/supabase-browser';
1818
import { getProjects, Project, deleteProject } from '@/lib/database';
1919
import { formatDistanceToNow } from 'date-fns';
2020

@@ -67,7 +67,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
6767
};
6868

6969
const handleDeleteChat = async (chatId: string) => {
70-
const supabase = createBrowserClient();
70+
const supabase = createSupabaseBrowserClient();
7171
await deleteProject(supabase, chatId);
7272
setChatHistory(chatHistory.filter(chat => chat.id !== chatId));
7373
};
@@ -112,7 +112,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
112112
};
113113

114114
React.useEffect(() => {
115-
const supabase = createBrowserClient();
115+
const supabase = createSupabaseBrowserClient();
116116

117117
// Skip authentication setup if Supabase is not available (development mode)
118118
if (!supabase) {
@@ -138,7 +138,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
138138
};
139139
getUser();
140140

141-
const { data: authListener } = supabase.auth.onAuthStateChange((_event, session) => {
141+
const { data: authListener } = supabase.auth.onAuthStateChange((_event: AuthChangeEvent, session: Session | null) => {
142142
setUser(session?.user ?? null);
143143
if (session?.user) {
144144
fetchChatHistory();

lib/auth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { createBrowserClient } from './supabase-client'
1+
import { createSupabaseBrowserClient } from './supabase-browser'
22
import { ViewType } from '@/components/auth'
33
import { randomString } from './utils'
4-
import { Session } from '@supabase/supabase-js'
4+
import { Session, AuthChangeEvent } from '@supabase/supabase-js'
55
import { usePostHog } from 'posthog-js/react'
66
import { useState, useEffect } from 'react'
77

8-
const supabase = createBrowserClient();
8+
const supabase = createSupabaseBrowserClient();
99

1010
type UserTeam = {
1111
email: string
@@ -43,7 +43,7 @@ export function useAuth(
4343
return setSession({ user: { email: 'demo@codinit.dev' } } as Session)
4444
}
4545

46-
supabase.auth.getSession().then(({ data: { session } }) => {
46+
supabase.auth.getSession().then(({ data: { session } }: { data: { session: Session | null } }) => {
4747
setSession(session)
4848
if (session) {
4949
getUserTeam(session).then(setUserTeam)
@@ -63,7 +63,7 @@ export function useAuth(
6363

6464
const {
6565
data: { subscription },
66-
} = supabase.auth.onAuthStateChange((_event, session) => {
66+
} = supabase.auth.onAuthStateChange((_event: AuthChangeEvent, session: Session | null) => {
6767
setSession(session)
6868

6969
if (_event === 'PASSWORD_RECOVERY') {

lib/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { SupabaseClient } from '@supabase/supabase-js'
2-
import { createBrowserClient } from './supabase-client'
2+
import { createSupabaseBrowserClient } from './supabase-browser'
33
import { Message } from './messages'
44

55
// The supabase client will be passed as an argument to functions.
66
// A browser client is created here for convenience on the client-side.
7-
const browserSupabase = createBrowserClient()
7+
const browserSupabase = createSupabaseBrowserClient()
88

99
export interface Project {
1010
id: string

lib/supabase-browser.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { createBrowserClient } from '@supabase/ssr'
2+
3+
let supabaseClient: ReturnType<typeof createBrowserClient> | null = null
4+
5+
export function createSupabaseBrowserClient() {
6+
// Only create client on the client side
7+
if (typeof window === 'undefined') {
8+
return null
9+
}
10+
11+
// Return existing client if it exists
12+
if (supabaseClient) {
13+
return supabaseClient
14+
}
15+
16+
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
17+
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
18+
19+
if (!supabaseUrl || !supabaseAnonKey) {
20+
// Return null in development when Supabase is not configured
21+
if (process.env.NODE_ENV === 'development') {
22+
return null
23+
}
24+
throw new Error('Supabase URL and Anon Key are required')
25+
}
26+
27+
// Create and cache the client using SSR-compatible client
28+
supabaseClient = createBrowserClient(supabaseUrl, supabaseAnonKey)
29+
return supabaseClient
30+
}

lib/supabase-client.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/user-settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { createBrowserClient } from './supabase-client'
1+
import { createSupabaseBrowserClient } from './supabase-browser'
22

33
// Lazy getter for supabase instance
44
function getSupabase() {
5-
return createBrowserClient()
5+
return createSupabaseBrowserClient()
66
}
77

88
export interface UserProfile {

0 commit comments

Comments
 (0)