Skip to content

Commit

Permalink
add basic subject archive functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
xvvvyz committed Jul 1, 2024
1 parent df60e44 commit 9d5c6c2
Show file tree
Hide file tree
Showing 37 changed files with 298 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const Page = async ({ params: { eventTypeId, subjectId } }: PageProps) => {
<PageModalHeader title={eventType.name as string} />
<EventCard
eventType={eventType}
isArchived={subject.archived}
isTeamMember={!!user && subject.team_id === user.id}
subjectId={subjectId}
user={user}
Expand Down
3 changes: 3 additions & 0 deletions app/(pages)/(private)/subjects/(group)/@archived/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Loading = () => null;

export default Loading;
15 changes: 15 additions & 0 deletions app/(pages)/(private)/subjects/(group)/@archived/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import SubjectList from '@/_components/subject-list';
import CollapsibleArchive from '@/_queries/collapsible-archive';
import countArchivedSubjects from '@/_queries/count-archived-subjects';

const Page = async () => {
const { count } = await countArchivedSubjects();
if (!count) return null;

return (
<CollapsibleArchive label="subjects">
<SubjectList archived />
</CollapsibleArchive>
);
};
export default Page;
8 changes: 6 additions & 2 deletions app/(pages)/(private)/subjects/(group)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import Button from '@/_components/button';
import { ReactNode } from 'react';

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

const Layout = ({ children }: LayoutProps) => (
const Layout = async ({ archived, children }: LayoutProps) => (
<>
<div className="my-16 flex h-8 items-center justify-between gap-8 px-4">
<h1 className="text-2xl">Subjects</h1>
<Button href="/subjects/create" scroll={false} size="sm">
Create subject
</Button>
</div>
{children}
<div className="space-y-4">
{children}
{archived}
</div>
</>
);

Expand Down
107 changes: 2 additions & 105 deletions app/(pages)/(private)/subjects/(group)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,110 +1,7 @@
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 ArrowRightIcon from '@heroicons/react/24/outline/ArrowRightIcon';
import EllipsisVerticalIcon from '@heroicons/react/24/outline/EllipsisVerticalIcon';
import InformationCircleIcon from '@heroicons/react/24/outline/InformationCircleIcon';
import SubjectList from '@/_components/subject-list';

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

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

if (!subjects?.length) {
return (
<Empty className="mx-4">
<InformationCircleIcon className="w-7" />
Subjects can be dogs, cats, humans or
<br />
anything else you want to track.
</Empty>
);
}

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 (
<div className="space-y-4">
{!!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"
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"
>
<Avatar
className="-my-0.5"
file={subject.image_uri}
key={subject.id}
id={subject.id}
size="sm"
/>
{subject.name}
</Button>
<SubjectMenu
className="group flex h-full items-center justify-center px-2 text-fg-3 hover:text-fg-2"
itemsClassName="mr-2 mt-2"
subject={subject}
>
<div className="rounded-full p-2 group-hover: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"
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>
);
};
const Page = () => <SubjectList />;

export default Page;
11 changes: 7 additions & 4 deletions app/_components/event-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface EventCardProps {
| NonNullable<GetEventTypeWithInputsAndOptionsData>
| NonNullable<GetSessionWithDetailsData>['modules'][0];
hideContent?: boolean;
isArchived?: boolean;
isPublic?: boolean;
isTeamMember?: boolean;
mission?: NonNullable<GetMissionWithSessionsData>;
Expand All @@ -32,14 +33,14 @@ const EventCard = ({
event,
eventType,
hideContent,
isArchived,
isPublic,
isTeamMember,
mission,
subjectId,
user,
}: EventCardProps) => {
const comments = forceArray(event?.comments);
const showComments = event && (!isPublic || !!comments.length);
const showDescription = !hideContent && !!eventType.content;
const showModule = mission && typeof eventType.order === 'number';

Expand Down Expand Up @@ -73,25 +74,27 @@ const EventCard = ({
)}
</div>
)}
{(!isPublic || event) && (
{(event || (!isPublic && !isArchived)) && (
<EventForm
disabled={disabled}
event={event}
eventType={eventType}
isArchived={isArchived}
isMission={!!mission}
isPublic={isPublic}
subjectId={subjectId}
/>
)}
{showComments && (
{event && (!!comments.length || (!isPublic && !isArchived)) && (
<div className="flex flex-col gap-8 border-t border-alpha-1 px-4 py-8 sm:px-8">
<EventComments
comments={comments as EventCommentsProps['comments']}
isArchived={isArchived}
isPublic={isPublic}
isTeamMember={isTeamMember}
userId={user?.id}
/>
{!isPublic && <EventCommentForm eventId={event.id} />}
{!isPublic && !isArchived && <EventCommentForm eventId={event.id} />}
</div>
)}
</>
Expand Down
30 changes: 17 additions & 13 deletions app/_components/event-comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface EventCommentProps {
createdAt: string;
id: string;
profile: Database['public']['Tables']['profiles']['Row'];
isArchived?: boolean;
isPublic?: boolean;
isTeamMember?: boolean;
userId?: string;
Expand All @@ -26,6 +27,7 @@ const EventComment = ({
createdAt,
id,
profile,
isArchived,
isPublic,
isTeamMember,
userId,
Expand Down Expand Up @@ -54,19 +56,21 @@ const EventComment = ({
formatter="date-time"
/>
</div>
{!isPublic && (userId === profile.id || isTeamMember) && (
<Menu className="-mr-2 -mt-2.5">
<Menu.Button className="rounded-full p-2 hover:bg-alpha-1">
<EllipsisVerticalIcon className="w-5" />
</Menu.Button>
<Menu.Items>
<Menu.Item onClick={() => toggleDeleteAlert(true)}>
<TrashIcon className="w-5 text-fg-4" />
Delete comment
</Menu.Item>
</Menu.Items>
</Menu>
)}
{!isPublic &&
!isArchived &&
(userId === profile.id || isTeamMember) && (
<Menu className="-mr-2 -mt-2.5">
<Menu.Button className="rounded-full p-2 hover:bg-alpha-1">
<EllipsisVerticalIcon className="w-5" />
</Menu.Button>
<Menu.Items>
<Menu.Item onClick={() => toggleDeleteAlert(true)}>
<TrashIcon className="w-5 text-fg-4" />
Delete comment
</Menu.Item>
</Menu.Items>
</Menu>
)}
</div>
<DirtyHtml className="mt-1">{content}</DirtyHtml>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/_components/event-comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface EventCommentsProps {
id: string;
profile: Database['public']['Tables']['profiles']['Row'];
}>;
isArchived?: boolean;
isPublic?: boolean;
isTeamMember?: boolean;
userId?: string;
Expand All @@ -20,6 +21,7 @@ interface EventCommentsProps {
const EventComments = ({
className,
comments,
isArchived,
isPublic,
isTeamMember,
userId,
Expand All @@ -33,6 +35,7 @@ const EventComments = ({
content={content}
createdAt={created_at}
id={id}
isArchived={isArchived}
isPublic={isPublic}
isTeamMember={isTeamMember}
key={id}
Expand Down
4 changes: 3 additions & 1 deletion app/_components/event-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface EventFormProps {
| NonNullable<NonNullable<GetEventData>['type']>
| NonNullable<GetEventTypeWithInputsAndOptionsData>
| NonNullable<GetSessionWithDetailsData>['modules'][0];
isArchived?: boolean;
isMission?: boolean;
isPublic?: boolean;
subjectId: string;
Expand All @@ -55,6 +56,7 @@ const EventForm = ({
disabled,
event,
eventType,
isArchived,
isMission,
isPublic,
subjectId,
Expand Down Expand Up @@ -305,7 +307,7 @@ const EventForm = ({
{form.formState.errors.root.message}
</div>
)}
{!isPublic && (
{!isPublic && !isArchived && (
<div className="flex gap-4 border-t border-alpha-1 px-4 py-8 sm:px-8">
{!event && !isMission && (
<BackButton className="w-full" colorScheme="transparent">
Expand Down
1 change: 1 addition & 0 deletions app/_components/event-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const EventPage = async ({ eventId, isPublic, subjectId }: EventPageProps) => {
<EventCard
event={event}
eventType={event.type}
isArchived={subject.archived}
isPublic={isPublic}
isTeamMember={!!user && subject.team_id === user.id}
subjectId={subjectId}
Expand Down
2 changes: 1 addition & 1 deletion app/_components/event-type-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const EventTypeForm = ({
onCreateOption={(value) =>
setCreateInputModal({
label: value,
subjects_for: [{ subject_id: subjectId }],
subjects: [{ id: subjectId }],
})
}
options={availableInputs as IOption[]}
Expand Down
2 changes: 1 addition & 1 deletion app/_components/input-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const InputForm = ({
options: input?.options ?? [],
settings: input?.settings as InputSettingsJson,
subjects: forceArray(subjects).filter(({ id }) =>
input?.subjects_for?.some(({ subject_id }) => subject_id === id),
input?.subjects?.some((sf) => sf.id === id),
),
type: INPUT_TYPE_OPTIONS.find(({ id }) => id === input?.type),
},
Expand Down
9 changes: 6 additions & 3 deletions app/_components/insights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { twMerge } from 'tailwind-merge';
interface InsightsProps {
events: NonNullable<ListEventsData>;
insights: NonNullable<ListInsightsData>;
isArchived?: boolean;
isPublic?: boolean;
isTeamMember: boolean;
searchString: string;
Expand All @@ -23,6 +24,7 @@ interface InsightsProps {
const Insights = ({
events,
insights,
isArchived,
isPublic,
isTeamMember,
searchString,
Expand All @@ -34,6 +36,7 @@ const Insights = ({

return insights.map((insight) => {
const config = insight.config as InsightConfigJson;
const isReadOnly = !isTeamMember || isArchived;

return (
<article
Expand All @@ -45,18 +48,18 @@ const Insights = ({
<Button
className={twMerge(
'm-0 flex w-full gap-4 px-4 py-3 leading-snug',
isTeamMember && 'pr-0',
!isReadOnly && 'pr-0',
)}
href={`/${shareOrSubjects}/${subjectId}/insights/${insight.id}${searchString}`}
scroll={false}
variant="link"
>
{insight.name}
{!isTeamMember && (
{isReadOnly && (
<ArrowUpRightIcon className="ml-auto w-5 shrink-0" />
)}
</Button>
{isTeamMember && (
{!isReadOnly && (
<InsightCardMenu insightId={insight.id} subjectId={subjectId} />
)}
</div>
Expand Down
Loading

0 comments on commit 9d5c6c2

Please sign in to comment.