Skip to content

Commit

Permalink
simplify subjects page
Browse files Browse the repository at this point in the history
  • Loading branch information
xvvvyz committed Jul 8, 2024
1 parent 643b350 commit aad7f62
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 138 deletions.
3 changes: 0 additions & 3 deletions app/(pages)/(with-nav)/subjects/(list)/@archived/loading.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions app/(pages)/(with-nav)/subjects/(list)/@archived/page.tsx

This file was deleted.

8 changes: 2 additions & 6 deletions app/(pages)/(with-nav)/subjects/(list)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import PlusIcon from '@heroicons/react/24/outline/PlusIcon';
import { ReactNode } from 'react';

interface LayoutProps {
archived: ReactNode;
children: ReactNode;
}

const Layout = async ({ archived, children }: LayoutProps) => (
const Layout = async ({ children }: LayoutProps) => (
<>
<div className="my-16 flex h-8 items-center justify-between gap-8 px-4">
<h1 className="text-2xl">Subjects</h1>
Expand All @@ -16,10 +15,7 @@ const Layout = async ({ archived, children }: LayoutProps) => (
New subject
</Button>
</div>
<div className="space-y-4">
{children}
{archived}
</div>
<div className="space-y-4">{children}</div>
</>
);

Expand Down
62 changes: 61 additions & 1 deletion app/(pages)/(with-nav)/subjects/(list)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
import CollapsibleArchive from '@/_components/collapsible-archive';
import Empty from '@/_components/empty';
import SubjectList from '@/_components/subject-list';
import getCurrentUser from '@/_queries/get-current-user';
import listSubjects, { ListSubjectsData } from '@/_queries/list-subjects';
import InformationCircleIcon from '@heroicons/react/24/outline/InformationCircleIcon';

export const metadata = { title: 'Subjects' };

const Page = () => <SubjectList />;
const Page = async () => {
const [{ data: subjects }, user] = await Promise.all([
listSubjects(),
getCurrentUser(),
]);

if (!subjects || !user) return null;

const {
archivedClientSubjects,
archivedTeamSubjects,
clientSubjects,
teamSubjects,
} = subjects.reduce(
(acc, subject) => {
if (subject.team_id === user.id) {
if (subject.archived) acc.archivedTeamSubjects.push(subject);
else acc.teamSubjects.push(subject);
} else {
if (subject.archived) acc.archivedClientSubjects.push(subject);
else acc.clientSubjects.push(subject);
}

return acc;
},
{
archivedClientSubjects: [] as NonNullable<ListSubjectsData>,
archivedTeamSubjects: [] as NonNullable<ListSubjectsData>,
clientSubjects: [] as NonNullable<ListSubjectsData>,
teamSubjects: [] as NonNullable<ListSubjectsData>,
},
);

return (
<>
{!clientSubjects.length && !teamSubjects.length && (
<Empty className="mx-4">
<InformationCircleIcon className="w-7" />
Subjects can be dogs, cats, humans or
<br />
anything else you want to track.
</Empty>
)}
<SubjectList
clientSubjects={clientSubjects}
teamSubjects={teamSubjects}
/>
<CollapsibleArchive>
<SubjectList
clientSubjects={archivedClientSubjects}
teamSubjects={archivedTeamSubjects}
/>
</CollapsibleArchive>
</>
);
};

export default Page;
2 changes: 1 addition & 1 deletion app/_components/page-modal-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const PageModalHeader = ({
{subtitle}
</div>
<BackIconButton
className="relative -top-1 right-1 shrink-0"
className="relative -top-0.5 right-1 shrink-0"
icon={<XMarkIcon className="w-7" />}
onClick={onClose}
/>
Expand Down
2 changes: 1 addition & 1 deletion app/_components/page-modal-loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const PageModalLoading = () => (
<div className="align-start flex justify-between px-4 py-8 sm:px-8">
<div className="h-6 w-32 animate-pulse rounded-sm bg-alpha-3" />
<BackIconButton
className="relative -top-1 right-1 shrink-0"
className="relative -top-0.5 right-1 shrink-0"
icon={<XMarkIcon className="w-7" />}
/>
</div>
Expand Down
158 changes: 62 additions & 96 deletions app/_components/subject-list.tsx
Original file line number Diff line number Diff line change
@@ -1,109 +1,75 @@
import Avatar from '@/_components/avatar';
import Button from '@/_components/button';
import Empty from '@/_components/empty';
import SubjectMenu from '@/_components/subject-menu';
import getCurrentUser from '@/_queries/get-current-user';
import listSubjects, { ListSubjectsData } from '@/_queries/list-subjects';
import { ListSubjectsData } from '@/_queries/list-subjects';
import ArrowRightIcon from '@heroicons/react/24/outline/ArrowRightIcon';
import EllipsisVerticalIcon from '@heroicons/react/24/outline/EllipsisVerticalIcon';
import InformationCircleIcon from '@heroicons/react/24/outline/InformationCircleIcon';

interface SubjectListProps {
archived?: boolean;
clientSubjects: NonNullable<ListSubjectsData>;
teamSubjects: NonNullable<ListSubjectsData>;
}

const SubjectList = async ({ archived = false }: SubjectListProps) => {
const [{ data: subjects }, user] = await Promise.all([
listSubjects({ archived }),
getCurrentUser(),
]);

if (!subjects) return null;

const {
clientSubjects,
teamSubjects,
}: {
clientSubjects: NonNullable<ListSubjectsData>;
teamSubjects: NonNullable<ListSubjectsData>;
} = subjects.reduce(
(acc, subject) => {
if (!!user && subject.team_id === user.id) acc.teamSubjects.push(subject);
else acc.clientSubjects.push(subject);
return acc;
},
{
clientSubjects: [] as NonNullable<ListSubjectsData>,
teamSubjects: [] as NonNullable<ListSubjectsData>,
},
);

return (
<>
{!subjects.length && !archived && (
<Empty className="mx-4">
<InformationCircleIcon className="w-7" />
Subjects can be dogs, cats, humans or
<br />
anything else you want to track.
</Empty>
)}
{!!teamSubjects.length && (
<ul className="mx-4 rounded border border-alpha-1 bg-bg-2 py-1">
{teamSubjects.map((subject) => (
<li
className="flex items-stretch hover:bg-alpha-1 active:bg-alpha-1"
key={subject.id}
const SubjectList = async ({
clientSubjects,
teamSubjects,
}: SubjectListProps) => (
<>
{!!teamSubjects.length && (
<ul className="mx-4 rounded border border-alpha-1 bg-bg-2 py-1">
{teamSubjects.map((subject) => (
<li
className="flex items-stretch hover:bg-alpha-1 active:bg-alpha-1"
key={subject.id}
>
<Button
className="m-0 w-full gap-4 px-4 py-3 pr-0 leading-snug"
href={`/subjects/${subject.id}/events`}
variant="link"
>
<Button
className="m-0 w-full gap-4 px-4 py-3 pr-0 leading-snug"
href={`/subjects/${subject.id}/events`}
variant="link"
>
<Avatar
className="-my-0.5"
file={subject.image_uri}
key={subject.id}
id={subject.id}
size="sm"
/>
{subject.name}
</Button>
<SubjectMenu contentClassName="-mt-12 mr-1.5" subject={subject}>
<div className="group flex items-center justify-center px-2 text-fg-3 hover:text-fg-2 active:text-fg-2">
<div className="rounded-full p-2 group-hover:bg-alpha-1 group-active:bg-alpha-1">
<EllipsisVerticalIcon className="w-5" />
</div>
<Avatar
className="-my-0.5"
file={subject.image_uri}
key={subject.id}
id={subject.id}
size="sm"
/>
{subject.name}
</Button>
<SubjectMenu contentClassName="-mt-12 mr-1.5" subject={subject}>
<div className="group flex items-center justify-center px-2 text-fg-3 hover:text-fg-2 active:text-fg-2">
<div className="rounded-full p-2 group-hover:bg-alpha-1 group-active:bg-alpha-1">
<EllipsisVerticalIcon className="w-5" />
</div>
</SubjectMenu>
</li>
))}
</ul>
)}
{!!clientSubjects.length && (
<ul className="mx-4 rounded border border-alpha-1 bg-bg-2 py-1">
{clientSubjects.map((subject) => (
<li key={subject.id}>
<Button
className="m-0 w-full gap-6 px-4 py-3 leading-snug hover:bg-alpha-1 active:bg-alpha-1"
href={`/subjects/${subject.id}/events`}
variant="link"
>
<Avatar
className="-my-0.5 -mr-2"
file={subject.image_uri}
id={subject.id}
size="sm"
/>
{subject.name}
<ArrowRightIcon className="ml-auto w-5 shrink-0" />
</Button>
</li>
))}
</ul>
)}
</>
);
};
</div>
</SubjectMenu>
</li>
))}
</ul>
)}
{!!clientSubjects.length && (
<ul className="mx-4 rounded border border-alpha-1 bg-bg-2 py-1">
{clientSubjects.map((subject) => (
<li key={subject.id}>
<Button
className="m-0 w-full gap-6 px-4 py-3 leading-snug hover:bg-alpha-1 active:bg-alpha-1"
href={`/subjects/${subject.id}/events`}
variant="link"
>
<Avatar
className="-my-0.5 -mr-2"
file={subject.image_uri}
id={subject.id}
size="sm"
/>
{subject.name}
<ArrowRightIcon className="ml-auto w-5 shrink-0" />
</Button>
</li>
))}
</ul>
)}
</>
);

export default SubjectList;
10 changes: 0 additions & 10 deletions app/_queries/count-archived-subjects.ts

This file was deleted.

6 changes: 2 additions & 4 deletions app/_queries/list-subjects.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import createServerSupabaseClient from '@/_utilities/create-server-supabase-client';

const listSubjects = async (
{ archived }: { archived: boolean } = { archived: false },
) =>
const listSubjects = async () =>
createServerSupabaseClient()
.from('subjects')
.select('archived, id, image_uri, name, public, share_code, team_id')
.not('team_id', 'is', null)
.eq('deleted', false)
.eq('archived', archived)
.not('archived', 'is', null)
.order('name');

export type ListSubjectsData = Awaited<ReturnType<typeof listSubjects>>['data'];
Expand Down

0 comments on commit aad7f62

Please sign in to comment.