Connect, collaborate, and grow with a community of TypeScript developers
refactor tabs up a level + stub nested submission details page (#185)
bautistaaa5d26efd13e7d
diff --git a/src/app/challenge/[id]/comments/loading.tsx b/src/app/challenge/[id]/comments/loading.tsxdeleted file mode 100644index 90c4524f..00000000--- a/src/app/challenge/[id]/comments/loading.tsx+++ /dev/null@@ -1,3 +0,0 @@-export default function Loading() {- return 'loading';-}diff --git a/src/app/challenge/[id]/comments/page.tsx b/src/app/challenge/[id]/comments/page.tsxdeleted file mode 100644index 6509a66c..00000000--- a/src/app/challenge/[id]/comments/page.tsx+++ /dev/null@@ -1,21 +0,0 @@-import { notFound } from 'next/navigation';-import { LeftPanel } from '~/components/challenge/left-panel';-import { getChallengeRouteData } from '../getChallengeRouteData';-import { getServerAuthSession } from '../../../../server/auth';--interface Props {- params: {- id: string;- };-}--export default async function CommentsPage({ params: { id } }: Props) {- const session = await getServerAuthSession();- const challenge = await getChallengeRouteData(id, session);-- if (!challenge) {- return notFound();- }-- return <LeftPanel challenge={challenge} selectedTab={'comments'} />;-}diff --git a/src/app/challenge/[id]/layout.tsx b/src/app/challenge/[id]/layout.tsxindex 6a534e10..96d9cce4 100644--- a/src/app/challenge/[id]/layout.tsx+++ b/src/app/challenge/[id]/layout.tsx@@ -2,6 +2,7 @@ import { ChallengeLayout } from '~/components/challenge/challenge-layout';import { Wrapper } from './wrapper';import { getChallengeRouteData } from './getChallengeRouteData';import { getServerAuthSession } from '~/server/auth';+import { LeftWrapper } from './left-wrapper';export default async function LayoutData({children,@@ -14,5 +15,10 @@ export default async function LayoutData({const challenge = await getChallengeRouteData(id, session);if (!challenge) return <div>loading</div>;- return <ChallengeLayout left={children} right={<Wrapper challenge={challenge} />} />;+ return (+ <ChallengeLayout+ left={<LeftWrapper challengeId={challenge.id}>{children}</LeftWrapper>}+ right={<Wrapper challenge={challenge} />}+ />+ );}diff --git a/src/app/challenge/[id]/left-wrapper.tsx b/src/app/challenge/[id]/left-wrapper.tsxnew file mode 100644index 00000000..39df3ffd--- /dev/null+++ b/src/app/challenge/[id]/left-wrapper.tsx@@ -0,0 +1,63 @@+'use client';++import { usePathname, useRouter } from 'next/navigation';+import { useMemo, type ReactNode } from 'react';+import { Tabs, TabsContent, TabsList, TabsTrigger } from '~/components/ui/tabs';++type Tab = 'description' | 'submissions' | 'solutions';+interface Props {+ children: ReactNode;+ challengeId: number;+}++export function LeftWrapper({ challengeId, children }: Props) {+ const pathname = usePathname();+ const router = useRouter();++ const selectedTab: Tab = useMemo(() => {+ const splitPath = pathname.split('/');++ if (splitPath.includes('submissions')) {+ return 'submissions';+ }+ if (splitPath.includes('solutions')) {+ return 'solutions';+ }++ return 'description';+ }, [pathname]);++ return (+ <Tabs+ defaultValue={selectedTab}+ className="flex h-full w-full flex-col overflow-y-auto overflow-x-hidden"+ >+ <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-3 rounded-none rounded-tl-2xl rounded-tr-xl border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">+ <TabsTrigger+ value="description"+ className="rounded-md rounded-tl-xl duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"+ onClick={() => router.push(`/challenge/${challengeId}`)}+ >+ Description+ </TabsTrigger>+ <TabsTrigger+ value="solutions"+ className="rounded-md duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"+ onClick={() => router.push(`/challenge/${challengeId}/solutions`)}+ >+ Solutions+ </TabsTrigger>+ <TabsTrigger+ value="submissions"+ className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"+ onClick={() => router.push(`/challenge/${challengeId}/submissions`)}+ >+ Submissions+ </TabsTrigger>+ </TabsList>+ <TabsContent value={selectedTab} className="mt-0 flex-1">+ {children}+ </TabsContent>+ </Tabs>+ );+}diff --git a/src/app/challenge/[id]/page.tsx b/src/app/challenge/[id]/page.tsxindex b9dde4a0..dfa47ac9 100644--- a/src/app/challenge/[id]/page.tsx+++ b/src/app/challenge/[id]/page.tsx@@ -1,7 +1,7 @@import { notFound } from 'next/navigation';-import { LeftPanel } from '~/components/challenge/left-panel';-import { getChallengeRouteData } from './getChallengeRouteData';+import { Description } from '~/components/challenge/description';import { getServerAuthSession } from '~/server/auth';+import { getChallengeRouteData } from './getChallengeRouteData';interface Props {params: {@@ -17,5 +17,5 @@ export default async function Challenges({ params: { id } }: Props) {return notFound();}- return <LeftPanel challenge={challenge} selectedTab={'description'} />;+ return <Description challenge={challenge} />;}diff --git a/src/app/challenge/[id]/solutions/[solutionId]/page.tsx b/src/app/challenge/[id]/solutions/[solutionId]/page.tsxindex eeba3d58..a67fda85 100644--- a/src/app/challenge/[id]/solutions/[solutionId]/page.tsx+++ b/src/app/challenge/[id]/solutions/[solutionId]/page.tsx@@ -1,8 +1,21 @@+import { notFound } from 'next/navigation';+import { getServerAuthSession } from '../../../../../server/auth';+import { getChallengeRouteData } from '../../getChallengeRouteData';+interface Props {params: {+ id: string;solutionId: string;};}-export default function SolutionPage({ params: { solutionId } }: Props) {- return <div>Solution Details for Solution {solutionId}</div>;++export default async function SolutionPage({ params: { id, solutionId } }: Props) {+ // const session = await getServerAuthSession();+ // const challenge = await getChallengeRouteData(id, session);+ //+ // if (!challenge) {+ // return notFound();+ // }+ //+ return <div>some details go here for {solutionId}</div>;}diff --git a/src/app/challenge/[id]/solutions/page.tsx b/src/app/challenge/[id]/solutions/page.tsxindex 2e3d353c..11a75b11 100644--- a/src/app/challenge/[id]/solutions/page.tsx+++ b/src/app/challenge/[id]/solutions/page.tsx@@ -1,7 +1,7 @@import { notFound } from 'next/navigation';-import { LeftPanel } from '~/components/challenge/left-panel';import { getChallengeRouteData } from '../getChallengeRouteData';import { getServerAuthSession } from '../../../../server/auth';+import { Solutions } from '~/components/challenge/solutions';interface Props {params: {@@ -17,5 +17,5 @@ export default async function SolutionPage({ params: { id } }: Props) {return notFound();}- return <LeftPanel challenge={challenge} selectedTab={'solutions'} />;+ return <Solutions challenge={challenge} />;}diff --git a/src/app/challenge/[id]/submissions/[submissionId]/page.tsx b/src/app/challenge/[id]/submissions/[submissionId]/page.tsxindex eb4c4c6a..a20dee38 100644--- a/src/app/challenge/[id]/submissions/[submissionId]/page.tsx+++ b/src/app/challenge/[id]/submissions/[submissionId]/page.tsx@@ -1,8 +1,7 @@import { notFound } from 'next/navigation';-import { LeftPanel } from '~/components/challenge/left-panel';-import { getChallengeRouteData } from '../../getChallengeRouteData';import { getServerAuthSession } from '../../../../../server/auth';-+import { getChallengeRouteData } from '../../getChallengeRouteData';+import { Submissions } from '~/components/challenge/submissions';interface Props {params: {id: string;@@ -17,5 +16,5 @@ export default async function SubmissionPage({ params: { id } }: Props) {return notFound();}- return <LeftPanel challenge={challenge} selectedTab={'submissions'} />;+ return <Submissions challenge={challenge} />;}diff --git a/src/app/challenge/[id]/submissions/page.tsx b/src/app/challenge/[id]/submissions/page.tsxindex 7b579e1f..15be62ad 100644--- a/src/app/challenge/[id]/submissions/page.tsx+++ b/src/app/challenge/[id]/submissions/page.tsx@@ -1,7 +1,7 @@import { notFound } from 'next/navigation';-import { LeftPanel } from '~/components/challenge/left-panel';-import { getChallengeRouteData } from '../getChallengeRouteData';+import { Submissions } from '~/components/challenge/submissions';import { getServerAuthSession } from '../../../../server/auth';+import { getChallengeRouteData } from '../getChallengeRouteData';interface Props {params: {@@ -16,6 +16,5 @@ export default async function SubmissionPage({ params: { id } }: Props) {if (!challenge) {return notFound();}-- return <LeftPanel challenge={challenge} selectedTab={'submissions'} />;+ return <Submissions challenge={challenge} />;}diff --git a/src/components/challenge/left-panel.tsx b/src/components/challenge/left-panel.tsxdeleted file mode 100644index d7261555..00000000--- a/src/components/challenge/left-panel.tsx+++ /dev/null@@ -1,61 +0,0 @@-'use client';--import Link from 'next/link';-import { type ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';-import { Description } from '~/components/challenge/description';-import { Tabs, TabsContent, TabsList, TabsTrigger } from '~/components/ui/tabs';-import Comments from './comments';-import { Solutions } from './solutions';-import { Submissions } from './submissions';--interface Props {- challenge: ChallengeRouteData;- selectedTab: 'description' | 'submissions' | 'solutions' | 'comments';-}--export function LeftPanel({ challenge, selectedTab }: Props) {- return (- <Tabs- defaultValue={selectedTab}- className="flex h-full w-full flex-col overflow-y-auto overflow-x-hidden"- >- <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-3 rounded-none rounded-tl-2xl rounded-tr-xl border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">- <TabsTrigger- value="description"- className="rounded-md rounded-tl-xl duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- >- <Link href={`/challenge/${challenge.id}`}>Description</Link>- </TabsTrigger>- <TabsTrigger- value="solutions"- className="rounded-md duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- >- <Link href={`/challenge/${challenge.id}/solutions`}>Solutions</Link>- </TabsTrigger>- <TabsTrigger- value="submissions"- className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"- >- <Link href={`/challenge/${challenge.id}/submissions`}>Submissions</Link>- </TabsTrigger>- </TabsList>- <TabsContent value="description" className="mt-0 flex-1">- <div className="relative flex h-full flex-col justify-between">- <div className="px-4 py-3">- <Description challenge={challenge}></Description>- </div>-- <div className="sticky bottom-0 -mx-[1px] overflow-hidden rounded-xl border border-zinc-300 border-b-background bg-background/90 shadow-[0_0_3rem_-0.25rem_#0004] backdrop-blur-sm duration-300 dark:border-zinc-700 dark:border-b-muted dark:bg-muted/90 dark:shadow-[0_0_3rem_-0.25rem_#0008]">- <Comments challenge={challenge} />- </div>- </div>- </TabsContent>- <TabsContent value="solutions" className="mt-0 flex-1">- <Solutions challenge={challenge} />- </TabsContent>- <TabsContent value="submissions" className="mt-0 flex-1">- <Submissions challenge={challenge} />- </TabsContent>- </Tabs>- );-}diff --git a/src/components/challenge/solutions/index.tsx b/src/components/challenge/solutions/index.tsxindex 0cf07205..e5a3cea3 100644--- a/src/components/challenge/solutions/index.tsx+++ b/src/components/challenge/solutions/index.tsx@@ -1,91 +1,86 @@'use client';-import { ArrowLeft, ArrowUp, Lock, MessageCircle, Plus } from 'lucide-react';+import { ArrowUp, MessageCircle, Plus } from 'lucide-react';import { Button } from '~/components/ui/button';-import { SolutionEditor } from './solution-editor';+import { useSession } from 'next-auth/react';import { useState } from 'react';-import { getRelativeTime } from '~/utils/relativeTime';import { type ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';-import { useSession } from 'next-auth/react';+import { getRelativeTime } from '~/utils/relativeTime';+import { SolutionEditor } from './solution-editor';+import Link from 'next/link';interface Props {challenge: ChallengeRouteData;}+type View = 'editor' | 'list' | 'details';export function Solutions({ challenge }: Props) {+ const [view, setView] = useState<View>('list');const hasSolution = challenge.solution?.length > 0;- const [openSolutionEditor, setOpenSolutionEditor] = useState(false);const session = useSession();const sharedSolution = challenge.sharedSolution;- const handleClick = () => {- setOpenSolutionEditor(true);- };-return (<div className="relative h-full">- {openSolutionEditor ? (- session.status === 'authenticated' ? (- <SolutionEditor challenge={challenge} setOpen={setOpenSolutionEditor} />- ) : (- <div className="flex h-full w-full flex-col items-center justify-center space-y-4">- <div className="flex flex-col items-center justify-center space-y-2">- <Lock className="h-8 w-8" />- <span className="max-w-[40ch] text-center text-black/50 dark:text-white/50">- You need to be logged in to post a solution.- </span>- </div>- <Button- variant={'ghost'}- className="gap-2"- onClick={() => {- setOpenSolutionEditor(!openSolutionEditor);- }}- >- <ArrowLeft />- Back- </Button>- </div>- )- ) : (+ {view === 'editor' && (+ <SolutionEditor challenge={challenge} dismiss={() => setView('list')} />+ )}++ {view === 'list' && (<><div className="sticky right-0 top-[41px] flex justify-end border-b border-zinc-300 bg-background/90 p-2 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90"><ButtonclassName="h-8 rounded-lg bg-emerald-600 px-3 py-2 hover:bg-emerald-500 dark:bg-emerald-400 dark:hover:bg-emerald-300"- onClick={() => handleClick()}- disabled={!hasSolution}+ onClick={() => setView('editor')}+ disabled={!hasSolution || !session?.data?.user}><Plus className="mr-2 h-4 w-4" /> Solution</Button></div>{sharedSolution.map((solution) => (- <SolutionRow key={solution.id} solution={solution} />+ <SolutionRow+ key={solution.id}+ solution={solution}+ handleClick={(id: string) => {+ setView('details');+ }}+ />))}</>)}++ {view === 'details' && <div>show some details</div>}</div>);}-function SolutionRow({ solution }: { solution: ChallengeRouteData['sharedSolution'][number] }) {+function SolutionRow({+ handleClick,+ solution,+}: {+ handleClick: (id: string) => void;+ solution: ChallengeRouteData['sharedSolution'][number];+}) {return (- <div className="group flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-zinc-700/50">- <div className="flex items-center justify-between">- <h3 className="font-bold">{solution.title}</h3>- <div className="text-sm text-neutral-500">{getRelativeTime(solution.createdAt)}</div>- </div>- <div className="flex gap-3 text-neutral-600 duration-300 dark:text-neutral-400">- <div className="my-auto mr-auto max-w-fit rounded-full bg-neutral-100 px-2 py-1 text-xs font-bold text-neutral-400 dark:bg-zinc-700 dark:text-neutral-400">- @ {solution.user?.name}+ <Link href={`/challenge/${solution.challengeId}/solutions/${solution.id}`}>+ <div className="group flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-zinc-700/50">+ <div className="flex items-center justify-between">+ <h3 className="font-bold">{solution.title}</h3>+ <div className="text-sm text-neutral-500">{getRelativeTime(solution.createdAt)}</div>+ </div>+ <div className="flex gap-3 text-neutral-600 duration-300 dark:text-neutral-400">+ <div className="my-auto mr-auto max-w-fit rounded-full bg-neutral-100 px-2 py-1 text-xs font-bold text-neutral-400 dark:bg-zinc-700 dark:text-neutral-400">+ @ {solution.user?.name}+ </div>+ <button className="flex cursor-pointer items-center gap-2 rounded-full bg-neutral-100 px-2 py-1 text-sm duration-300 hover:bg-neutral-200 dark:bg-zinc-700 dark:hover:bg-zinc-600">+ <MessageCircle className="h-4 w-4 stroke-1" /> {solution._count.solutionComment}+ </button>+ <button className="flex cursor-pointer items-center gap-2 rounded-full bg-emerald-600/10 px-2 py-1 text-sm text-emerald-600 duration-300 hover:bg-emerald-600/20 dark:bg-emerald-400/20 dark:text-emerald-400 dark:hover:bg-emerald-400/40">+ <ArrowUp className="h-4 w-4 stroke-1" /> {solution._count.vote}+ </button></div>- <button className="flex cursor-pointer items-center gap-2 rounded-full bg-neutral-100 px-2 py-1 text-sm duration-300 hover:bg-neutral-200 dark:bg-zinc-700 dark:hover:bg-zinc-600">- <MessageCircle className="h-4 w-4 stroke-1" /> {solution._count.solutionComment}- </button>- <button className="flex cursor-pointer items-center gap-2 rounded-full bg-emerald-600/10 px-2 py-1 text-sm text-emerald-600 duration-300 hover:bg-emerald-600/20 dark:bg-emerald-400/20 dark:text-emerald-400 dark:hover:bg-emerald-400/40">- <ArrowUp className="h-4 w-4 stroke-1" /> {solution._count.vote}- </button></div>- </div>+ </Link>);}diff --git a/src/components/challenge/solutions/solution-editor.tsx b/src/components/challenge/solutions/solution-editor.tsxindex 79a63a03..f3b57dce 100644--- a/src/components/challenge/solutions/solution-editor.tsx+++ b/src/components/challenge/solutions/solution-editor.tsx@@ -36,10 +36,10 @@ export type FormSchema = z.infer<typeof formSchema>;interface Props {challenge: ChallengeRouteData;- setOpen: (v: boolean) => void;+ dismiss: () => void;}-export function SolutionEditor({ setOpen, challenge }: Props) {+export function SolutionEditor({ dismiss, challenge }: Props) {const session = useSession();const router = useRouter();const form = useForm<FormSchema>({@@ -72,7 +72,7 @@ export function SolutionEditor({ setOpen, challenge }: Props) {title: 'Uh oh! Something went wrong. Please try again.',});} finally {- setOpen(false);+ dismiss();}};@@ -107,7 +107,7 @@ export function SolutionEditor({ setOpen, challenge }: Props) {<ButtonclassName="h-8 rounded-lg bg-white px-3 py-2 text-black hover:bg-zinc-200 focus-visible:bg-zinc-200 focus-visible:ring-offset-0 dark:bg-zinc-800 dark:text-zinc-300 dark:hover:bg-zinc-700 dark:focus-visible:bg-zinc-700"type="button"- onClick={() => setOpen(false)}+ onClick={dismiss}>Cancel</Button>