From 291edd44c83cea682c981e2d5cbc392d39e3b4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E7=B6=A0BG?= Date: Thu, 11 Jul 2024 20:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor=EF=BC=9A=E5=B0=86?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=A8=A1=E5=9D=97=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[serverId]/channels/[channelId]/page.tsx | 81 ++-------------- .../conversations/[memberId]/page.tsx | 88 ++--------------- .../(routes)/servers/[serverId]/page.tsx | 49 ++-------- src/app/(setup)/page.tsx | 36 +++---- src/modules/Channels/components/Channels.tsx | 84 +++++++++++++++- .../components/Conversations.tsx | 97 ++++++++++++++++++- src/modules/Servers/components/Servers.tsx | 47 ++++++++- src/modules/SetUp/components/SetUp.tsx | 26 ++++- 8 files changed, 281 insertions(+), 227 deletions(-) diff --git a/src/app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx b/src/app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx index 9a52fdd..0c5cbea 100644 --- a/src/app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx +++ b/src/app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx @@ -1,13 +1,6 @@ -import { redirectToSignIn } from '@clerk/nextjs/server'; -import { ChannelType } from '@prisma/client'; -import { redirect } from 'next/navigation'; +import { NextPage } from 'next'; -import { ChatHeader } from '@/common/components/chat/chat-header'; -import { ChatInput } from '@/common/components/chat/chat-input'; -import { ChatMessages } from '@/common/components/chat/chat-messages'; -import { MediaRoom } from '@/common/components/elements/media-room'; -import { currentProfile } from '@/common/libs/current-profile'; -import { db } from '@/common/libs/db'; +import Channels from '@/modules/Channels'; interface ChannelIdPageProps { params: { @@ -16,72 +9,12 @@ interface ChannelIdPageProps { }; } -const ChannelIdPage = async ({ params }: ChannelIdPageProps) => { - const profile = await currentProfile(); - - if (!profile) { - return redirectToSignIn(); - } - - const channel = await db.channel.findUnique({ - where: { - id: params.channelId, - }, - }); - - const member = await db.member.findFirst({ - where: { - serverId: params.serverId, - profileId: profile.id, - }, - }); - - if (!channel || !member) { - redirect('/'); - } - +const Page: NextPage = ({ params }) => { return ( -
- - {channel.type === ChannelType.TEXT && ( - <> - - - - )} - {channel.type === ChannelType.AUDIO && ( - - )} - {channel.type === ChannelType.VIDEO && ( - - )} -
+ <> + + ); }; -export default ChannelIdPage; +export default Page; diff --git a/src/app/(main)/(routes)/servers/[serverId]/conversations/[memberId]/page.tsx b/src/app/(main)/(routes)/servers/[serverId]/conversations/[memberId]/page.tsx index 1a5b8c1..4f03a7a 100644 --- a/src/app/(main)/(routes)/servers/[serverId]/conversations/[memberId]/page.tsx +++ b/src/app/(main)/(routes)/servers/[serverId]/conversations/[memberId]/page.tsx @@ -1,13 +1,6 @@ -import { redirectToSignIn } from '@clerk/nextjs/server'; -import { redirect } from 'next/navigation'; +import { NextPage } from 'next'; -import { ChatHeader } from '@/common/components/chat/chat-header'; -import { ChatInput } from '@/common/components/chat/chat-input'; -import { ChatMessages } from '@/common/components/chat/chat-messages'; -import { MediaRoom } from '@/common/components/elements/media-room'; -import { getOrCreateConversation } from '@/common/libs/conversation'; -import { currentProfile } from '@/common/libs/current-profile'; -import { db } from '@/common/libs/db'; +import Conversations from '@/modules/Conversations'; interface MemberIdPageProps { params: { @@ -19,79 +12,16 @@ interface MemberIdPageProps { }; } -const MemberIdPage = async ({ params, searchParams }: MemberIdPageProps) => { - const profile = await currentProfile(); - - if (!profile) { - return redirectToSignIn(); - } - - const currentMember = await db.member.findFirst({ - where: { - serverId: params.serverId, - profileId: profile.id, - }, - include: { - profile: true, - }, - }); - - if (!currentMember) { - return redirect('/'); - } - - const conversation = await getOrCreateConversation( - currentMember.id, - params.memberId - ); - - if (!conversation) { - return redirect(`/servers/${params.serverId}`); - } - - const { memberOne, memberTwo } = conversation; - - const otherMember = - memberOne.profileId === profile.id ? memberTwo : memberOne; - +const Page: NextPage = ({ params, searchParams }) => { return ( -
- + - {searchParams.video && ( - - )} - {!searchParams.video && ( - <> - - - - )} -
+ ); }; -export default MemberIdPage; +export default Page; diff --git a/src/app/(main)/(routes)/servers/[serverId]/page.tsx b/src/app/(main)/(routes)/servers/[serverId]/page.tsx index cc2d91b..925c7eb 100644 --- a/src/app/(main)/(routes)/servers/[serverId]/page.tsx +++ b/src/app/(main)/(routes)/servers/[serverId]/page.tsx @@ -1,8 +1,6 @@ -import { redirectToSignIn } from '@clerk/nextjs/server'; -import { redirect } from 'next/navigation'; +import { NextPage } from 'next'; -import { currentProfile } from '@/common/libs/current-profile'; -import { db } from '@/common/libs/db'; +import Servers from '@/modules/Servers'; interface ServerIdPageProps { params: { @@ -10,41 +8,12 @@ interface ServerIdPageProps { }; } -const ServerIdPage = async ({ params }: ServerIdPageProps) => { - const profile = await currentProfile(); - - if (!profile) { - return redirectToSignIn(); - } - - const server = await db.server.findUnique({ - where: { - id: params.serverId, - members: { - some: { - profileId: profile.id, - }, - }, - }, - include: { - channels: { - where: { - name: '一般频道', - }, - orderBy: { - createdAt: 'asc', - }, - }, - }, - }); - - const initialChannel = server?.channels[0]; - - if (initialChannel?.name !== '一般频道') { - return null; - } - - return redirect(`/servers/${params.serverId}/channels/${initialChannel?.id}`); +const Page: NextPage = ({ params }) => { + return ( + <> + + + ); }; -export default ServerIdPage; +export default Page; diff --git a/src/app/(setup)/page.tsx b/src/app/(setup)/page.tsx index 0e764d7..e73e0f4 100644 --- a/src/app/(setup)/page.tsx +++ b/src/app/(setup)/page.tsx @@ -1,27 +1,19 @@ -import { redirect } from 'next/navigation'; +import { NextPage } from 'next'; -import { InitialModal } from '@/common/components/modals/initial-modal'; -import { db } from '@/common/libs/db'; -import { initialProfile } from '@/common/libs/initial-profile'; +import SetUp from '@/modules/SetUp'; -const SetupPage = async () => { - const profile = await initialProfile(); +interface InviteCodePageProps { + params: { + inviteCode: string; + }; +} - const server = await db.server.findFirst({ - where: { - members: { - some: { - profileId: profile.id, - }, - }, - }, - }); - - if (server) { - return redirect(`/servers/${server.id}`); - } - - return ; +const Page: NextPage = ({ params }) => { + return ( + <> + + + ); }; -export default SetupPage; +export default Page; diff --git a/src/modules/Channels/components/Channels.tsx b/src/modules/Channels/components/Channels.tsx index b155d1f..b0e19e8 100644 --- a/src/modules/Channels/components/Channels.tsx +++ b/src/modules/Channels/components/Channels.tsx @@ -1,7 +1,85 @@ -import { SignIn as ClerkSignIn } from '@clerk/nextjs'; +import { redirectToSignIn } from '@clerk/nextjs/server'; +import { ChannelType } from '@prisma/client'; +import { redirect } from 'next/navigation'; -const Channels: React.FC = () => { - return ; +import { ChatHeader } from '@/common/components/chat/chat-header'; +import { ChatInput } from '@/common/components/chat/chat-input'; +import { ChatMessages } from '@/common/components/chat/chat-messages'; +import { MediaRoom } from '@/common/components/elements/media-room'; +import { currentProfile } from '@/common/libs/current-profile'; +import { db } from '@/common/libs/db'; + +interface ChannelProps { + serverId: string; + channelId: string; +} + +const Channels: React.FC = async ({ serverId, channelId }) => { + const profile = await currentProfile(); + + if (!profile) { + return redirectToSignIn(); + } + + const channel = await db.channel.findUnique({ + where: { + id: channelId, + }, + }); + + const member = await db.member.findFirst({ + where: { + serverId: serverId, + profileId: profile.id, + }, + }); + + if (!channel || !member) { + redirect('/'); + } + + return ( +
+ + {channel.type === ChannelType.TEXT && ( + <> + + + + )} + {channel.type === ChannelType.AUDIO && ( + + )} + {channel.type === ChannelType.VIDEO && ( + + )} +
+ ); }; export default Channels; diff --git a/src/modules/Conversations/components/Conversations.tsx b/src/modules/Conversations/components/Conversations.tsx index c1afcc6..71096c1 100644 --- a/src/modules/Conversations/components/Conversations.tsx +++ b/src/modules/Conversations/components/Conversations.tsx @@ -1,7 +1,98 @@ -import { SignIn as ClerkSignIn } from '@clerk/nextjs'; +import { redirectToSignIn } from '@clerk/nextjs/server'; +import { redirect } from 'next/navigation'; -const Conversations: React.FC = () => { - return ; +import { ChatHeader } from '@/common/components/chat/chat-header'; +import { ChatInput } from '@/common/components/chat/chat-input'; +import { ChatMessages } from '@/common/components/chat/chat-messages'; +import { MediaRoom } from '@/common/components/elements/media-room'; +import { getOrCreateConversation } from '@/common/libs/conversation'; +import { currentProfile } from '@/common/libs/current-profile'; +import { db } from '@/common/libs/db'; + +interface ConversationsProps { + memberId: string; + serverId: string; + + video?: boolean; +} + +const Conversations: React.FC = async ({ + memberId, + serverId, + video, +}) => { + const profile = await currentProfile(); + + if (!profile) { + return redirectToSignIn(); + } + + const currentMember = await db.member.findFirst({ + where: { + serverId: serverId, + profileId: profile.id, + }, + include: { + profile: true, + }, + }); + + if (!currentMember) { + return redirect('/'); + } + + const conversation = await getOrCreateConversation( + currentMember.id, + memberId + ); + + if (!conversation) { + return redirect(`/servers/${serverId}`); + } + + const { memberOne, memberTwo } = conversation; + + const otherMember = + memberOne.profileId === profile.id ? memberTwo : memberOne; + + return ( +
+ + {video && ( + + )} + {!video && ( + <> + + + + )} +
+ ); }; export default Conversations; diff --git a/src/modules/Servers/components/Servers.tsx b/src/modules/Servers/components/Servers.tsx index 067d9c1..c8f37cd 100644 --- a/src/modules/Servers/components/Servers.tsx +++ b/src/modules/Servers/components/Servers.tsx @@ -1,7 +1,48 @@ -import { SignIn as ClerkSignIn } from '@clerk/nextjs'; +import { redirectToSignIn } from '@clerk/nextjs/server'; +import { redirect } from 'next/navigation'; -const Servers: React.FC = () => { - return ; +import { currentProfile } from '@/common/libs/current-profile'; +import { db } from '@/common/libs/db'; + +interface ServerProps { + serverId: string; +} + +const Servers: React.FC = async ({ serverId }) => { + const profile = await currentProfile(); + + if (!profile) { + return redirectToSignIn(); + } + + const server = await db.server.findUnique({ + where: { + id: serverId, + members: { + some: { + profileId: profile.id, + }, + }, + }, + include: { + channels: { + where: { + name: '一般频道', + }, + orderBy: { + createdAt: 'asc', + }, + }, + }, + }); + + const initialChannel = server?.channels[0]; + + if (initialChannel?.name !== '一般频道') { + return null; + } + + return redirect(`/servers/${serverId}/channels/${initialChannel?.id}`); }; export default Servers; diff --git a/src/modules/SetUp/components/SetUp.tsx b/src/modules/SetUp/components/SetUp.tsx index 651228b..f3e9ff1 100644 --- a/src/modules/SetUp/components/SetUp.tsx +++ b/src/modules/SetUp/components/SetUp.tsx @@ -1,7 +1,27 @@ -import { SignIn as ClerkSignIn } from '@clerk/nextjs'; +import { redirect } from 'next/navigation'; -const SetUp: React.FC = () => { - return ; +import { InitialModal } from '@/common/components/modals/initial-modal'; +import { db } from '@/common/libs/db'; +import { initialProfile } from '@/common/libs/initial-profile'; + +const SetUp: React.FC = async () => { + const profile = await initialProfile(); + + const server = await db.server.findFirst({ + where: { + members: { + some: { + profileId: profile.id, + }, + }, + }, + }); + + if (server) { + return redirect(`/servers/${server.id}`); + } + + return ; }; export default SetUp;