-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved error handling for root route
- Loading branch information
1 parent
60fcc02
commit b2b5d34
Showing
5 changed files
with
83 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
import { useEffect, useCallback } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
import { useStoreModal } from '@/hooks/use-store-modal'; | ||
|
||
// Компонент настройки магазина | ||
const SetupPage = () => { | ||
const onOpen = useStoreModal((state) => state.onOpen); | ||
const isOpen = useStoreModal((state) => state.isOpen); | ||
const router = useRouter(); | ||
|
||
// Используем селекторы из хука с мемоизацией | ||
const { onOpen, isOpen } = useStoreModal( | ||
useCallback( | ||
(state) => ({ | ||
onOpen: state.onOpen, | ||
isOpen: state.isOpen, | ||
}), | ||
[], | ||
), | ||
); | ||
|
||
useEffect(() => { | ||
if (!isOpen) { | ||
onOpen(); | ||
try { | ||
if (!isOpen) { | ||
onOpen(); | ||
} | ||
} catch (error) { | ||
console.error('[SETUP_PAGE]', error); | ||
} | ||
}, [isOpen, onOpen]); | ||
|
||
return null; | ||
// Возвращаем пустой фрагмент вместо null для лучшей производительности | ||
return <></>; | ||
}; | ||
|
||
export default SetupPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,47 @@ | ||
import { auth } from '@clerk/nextjs/server'; | ||
import { redirect } from 'next/navigation'; | ||
import { cache } from 'react'; | ||
import { z } from 'zod'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { errorResponses } from '@/lib/error-responses'; | ||
|
||
// Кэширование запроса на получение магазина | ||
const getStore = cache(async (userId: string) => { | ||
return prismadb.store.findFirst({ | ||
where: { userId }, | ||
select: { | ||
id: true, | ||
name: true, | ||
createdAt: true, | ||
}, | ||
}); | ||
}); | ||
|
||
// Схема валидации для userId | ||
const userIdSchema = z.string().min(1, 'ID пользователя обязателен'); | ||
|
||
export default async function SetupLayout({ children }: { children: React.ReactNode }) { | ||
const { userId }: { userId: string | null } = await auth(); | ||
try { | ||
const { userId } = await auth(); | ||
|
||
if (!userId) { | ||
redirect('/sign-in'); | ||
} | ||
// Валидация userId | ||
const validationResult = userIdSchema.safeParse(userId); | ||
|
||
const store = await prismadb.store.findFirst({ | ||
where: { | ||
userId, | ||
}, | ||
}); | ||
if (!validationResult.success) { | ||
redirect('/sign-in'); | ||
} | ||
|
||
if (store) { | ||
redirect(`/${store.id}`); | ||
} | ||
// Используем кэшированную функцию для получения магазина | ||
const store = await getStore(validationResult.data); | ||
|
||
return <>{children}</>; | ||
if (store) { | ||
redirect(`/${store.id}`); | ||
} | ||
|
||
return <div className="min-h-screen">{children}</div>; | ||
} catch (error) { | ||
console.error('[SETUP_LAYOUT]', error); | ||
redirect('/error'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters