-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from commonground-project/issue-timeline-modal
Create issue timeline display modal
- Loading branch information
Showing
6 changed files
with
258 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"use client"; | ||
|
||
import { Modal, Timeline } from "@mantine/core"; | ||
import { Dispatch, SetStateAction, useMemo } from "react"; | ||
import { ArrowDownIcon } from "@heroicons/react/16/solid"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useCookies } from "react-cookie"; | ||
import { getIssueTimeline } from "@/lib/requests/timeline/getIssueTimeline"; | ||
import { toast } from "sonner"; | ||
import TimelineSkeleton from "@/components/Conversation/Issues/TimelineSkeleton"; | ||
|
||
type TimeLineModalProps = { | ||
isOpen: boolean; | ||
setIsOpen: Dispatch<SetStateAction<boolean>>; | ||
issueId: string; | ||
issueTitle: string; | ||
}; | ||
|
||
export default function TimeLineModal({ | ||
isOpen, | ||
setIsOpen, | ||
issueId, | ||
issueTitle, | ||
}: TimeLineModalProps) { | ||
const [cookie] = useCookies(["auth_token"]); | ||
|
||
const { isPending, error, data } = useQuery({ | ||
queryKey: ["issueTimeline", issueId], | ||
queryFn: () => | ||
getIssueTimeline({ issueId, user_token: cookie.auth_token }), | ||
}); | ||
|
||
const sortedTimeline = useMemo(() => { | ||
if (!data) return []; | ||
return data.content.sort( | ||
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(), | ||
); | ||
}, [data]); | ||
|
||
if (error) { | ||
toast.error("載入事件時間軸時發生問題,請再試一次"); | ||
return null; | ||
} | ||
|
||
return ( | ||
<Modal | ||
opened={isOpen} | ||
onClose={() => setIsOpen(false)} | ||
title={`《${issueTitle}》的演進`} | ||
size="620px" | ||
classNames={{ | ||
title: "font-bold", | ||
}} | ||
> | ||
{isPending ? ( | ||
<TimelineSkeleton /> | ||
) : sortedTimeline.length === 0 ? ( | ||
<h1 className="text-center font-black"> | ||
目前議題的資料還不足以產生時間軸,稍後再回來看看吧! | ||
</h1> | ||
) : ( | ||
<Timeline | ||
color="black" | ||
lineWidth={2} | ||
bulletSize={8} | ||
active={sortedTimeline.length} | ||
classNames={{ | ||
root: "pl-[32px]", | ||
itemBody: "ml-[16px]", | ||
}} | ||
> | ||
{sortedTimeline.map((node) => ( | ||
<Timeline.Item | ||
key={node.id} | ||
bullet={ | ||
<div className="relative flex items-center"> | ||
<hr className="absolute right-[-16px] h-[1px] w-[16px] border-black"></hr> | ||
</div> | ||
} | ||
title={`${node.date.toLocaleDateString()} ${node.title}`} | ||
> | ||
{node.description} | ||
</Timeline.Item> | ||
))} | ||
<Timeline.Item | ||
bullet={ | ||
<div className="relative"> | ||
<div className="absolute -left-1 -top-1 h-2 w-2 bg-white" /> | ||
<ArrowDownIcon className="absolute -left-2 -top-2 h-4 w-4 text-black" /> | ||
</div> | ||
} | ||
/> | ||
</Timeline> | ||
)} | ||
<div className="h-3" /> | ||
</Modal> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Skeleton } from "@mantine/core"; | ||
|
||
export default function TimelineSkeleton() { | ||
return ( | ||
<div className="ml-[60px]"> | ||
<Skeleton height={20} width={400} /> | ||
<div className="mt-4 flex flex-col gap-2"> | ||
<Skeleton height={16} width={506} /> | ||
<Skeleton height={16} width={506} /> | ||
<Skeleton height={16} width={300} /> | ||
</div> | ||
</div> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { parseJsonWhileHandlingErrors } from "../transformers"; | ||
import { TimelineNode } from "@/types/conversations.types"; | ||
|
||
export type getIssueTimelineResponse = { | ||
content: TimelineNode[]; | ||
}; | ||
|
||
type getIssueTimelineParams = { | ||
issueId: string; | ||
user_token: string; | ||
}; | ||
|
||
export async function getIssueTimeline({ | ||
issueId, | ||
user_token, | ||
}: getIssueTimelineParams): Promise<getIssueTimelineResponse> { | ||
return fetch( | ||
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/issue/${issueId}/timeline`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${user_token}`, | ||
}, | ||
}, | ||
) | ||
.then(parseJsonWhileHandlingErrors) | ||
.then((res: getIssueTimelineResponse) => { | ||
return { | ||
content: res.content.map((node) => ({ | ||
...node, | ||
createdAt: new Date(node.createdAt), | ||
updatedAt: new Date(node.updatedAt), | ||
date: new Date(node.date), | ||
})), | ||
}; | ||
}); | ||
} |
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