aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/pages/timeline/Timeline.tsx
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-20 20:26:42 +0800
committerGitHub <noreply@github.com>2023-09-20 20:26:42 +0800
commitf836d77e73f3ea0af45c5f71dae7268143d6d86f (patch)
tree573cfafd972106d69bef0d41ff5f270ec3c43ec2 /FrontEnd/src/pages/timeline/Timeline.tsx
parent4a069bf1268f393d5467166356f691eb89963152 (diff)
parent901fe3d7c032d284da5c9bce24c4aaee9054c7ac (diff)
downloadtimeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.tar.gz
timeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.tar.bz2
timeline-f836d77e73f3ea0af45c5f71dae7268143d6d86f.zip
Merge pull request #1395 from crupest/dev
Refector 2023 v0.1
Diffstat (limited to 'FrontEnd/src/pages/timeline/Timeline.tsx')
-rw-r--r--FrontEnd/src/pages/timeline/Timeline.tsx180
1 files changed, 180 insertions, 0 deletions
diff --git a/FrontEnd/src/pages/timeline/Timeline.tsx b/FrontEnd/src/pages/timeline/Timeline.tsx
new file mode 100644
index 00000000..32cbf8c8
--- /dev/null
+++ b/FrontEnd/src/pages/timeline/Timeline.tsx
@@ -0,0 +1,180 @@
+import { useState, useEffect } from "react";
+import classNames from "classnames";
+import { HubConnectionState } from "@microsoft/signalr";
+
+import {
+ HttpForbiddenError,
+ HttpNetworkError,
+ HttpNotFoundError,
+} from "~src/http/common";
+import {
+ getHttpTimelineClient,
+ HttpTimelineInfo,
+ HttpTimelinePostInfo,
+} from "~src/http/timeline";
+
+import { getTimelinePostUpdate$ } from "~src/services/timeline";
+
+import { useScrollToBottom } from "~src/components/hooks";
+
+import TimelinePostList from "./TimelinePostList";
+import TimelineInfoCard from "./TimelineInfoCard";
+import TimelinePostEdit from "./edit/TimelinePostCreateView";
+
+import "./Timeline.css";
+
+export interface TimelineProps {
+ className?: string;
+ timelineOwner: string;
+ timelineName: string;
+}
+
+export function Timeline(props: TimelineProps) {
+ const { timelineOwner, timelineName, className } = props;
+
+ const [timeline, setTimeline] = useState<HttpTimelineInfo | null>(null);
+ const [posts, setPosts] = useState<HttpTimelinePostInfo[] | null>(null);
+ const [signalrState, setSignalrState] = useState<HubConnectionState>(
+ HubConnectionState.Connecting,
+ );
+ const [error, setError] = useState<
+ "offline" | "forbid" | "notfound" | "error" | null
+ >(null);
+
+ const [currentPage, setCurrentPage] = useState(1);
+ const [totalPage, setTotalPage] = useState(0);
+
+ const [timelineReloadKey, setTimelineReloadKey] = useState(0);
+ const [postsReloadKey, setPostsReloadKey] = useState(0);
+
+ const updateTimeline = (): void => setTimelineReloadKey((o) => o + 1);
+ const updatePosts = (): void => setPostsReloadKey((o) => o + 1);
+
+ useEffect(() => {
+ setTimeline(null);
+ setPosts(null);
+ setError(null);
+ setSignalrState(HubConnectionState.Connecting);
+ }, [timelineOwner, timelineName]);
+
+ useEffect(() => {
+ getHttpTimelineClient()
+ .getTimeline(timelineOwner, timelineName)
+ .then(
+ (t) => {
+ setTimeline(t);
+ },
+ (error) => {
+ if (error instanceof HttpNetworkError) {
+ setError("offline");
+ } else if (error instanceof HttpForbiddenError) {
+ setError("forbid");
+ } else if (error instanceof HttpNotFoundError) {
+ setError("notfound");
+ } else {
+ console.error(error);
+ setError("error");
+ }
+ },
+ );
+ }, [timelineOwner, timelineName, timelineReloadKey]);
+
+ useEffect(() => {
+ getHttpTimelineClient()
+ .listPost(timelineOwner, timelineName, 1)
+ .then(
+ (page) => {
+ setPosts(
+ page.items.filter((p): p is HttpTimelinePostInfo => !p.deleted),
+ );
+ setTotalPage(page.totalPageCount);
+ },
+ (error) => {
+ if (error instanceof HttpNetworkError) {
+ setError("offline");
+ } else if (error instanceof HttpForbiddenError) {
+ setError("forbid");
+ } else if (error instanceof HttpNotFoundError) {
+ setError("notfound");
+ } else {
+ console.error(error);
+ setError("error");
+ }
+ },
+ );
+ }, [timelineOwner, timelineName, postsReloadKey]);
+
+ useEffect(() => {
+ const timelinePostUpdate$ = getTimelinePostUpdate$(
+ timelineOwner,
+ timelineName,
+ );
+ const subscription = timelinePostUpdate$.subscribe(({ update, state }) => {
+ if (update) {
+ setPostsReloadKey((o) => o + 1);
+ }
+ setSignalrState(state);
+ });
+ return () => {
+ subscription.unsubscribe();
+ };
+ }, [timelineOwner, timelineName]);
+
+ useScrollToBottom(() => {
+ console.log(`Load page ${currentPage + 1}.`);
+ setCurrentPage(currentPage + 1);
+ void getHttpTimelineClient()
+ .listPost(timelineOwner, timelineName, currentPage + 1)
+ .then(
+ (page) => {
+ const ps = page.items.filter(
+ (p): p is HttpTimelinePostInfo => !p.deleted,
+ );
+ setPosts((old) => [...(old ?? []), ...ps]);
+ },
+ (error) => {
+ if (error instanceof HttpNetworkError) {
+ setError("offline");
+ } else if (error instanceof HttpForbiddenError) {
+ setError("forbid");
+ } else if (error instanceof HttpNotFoundError) {
+ setError("notfound");
+ } else {
+ console.error(error);
+ setError("error");
+ }
+ },
+ );
+ }, currentPage < totalPage);
+
+ if (error === "offline") {
+ return <div className={className}>Offline.</div>;
+ } else if (error === "notfound") {
+ return <div className={className}>Not exist.</div>;
+ } else if (error === "forbid") {
+ return <div className={className}>Forbid.</div>;
+ } else if (error === "error") {
+ return <div className={className}>Error.</div>;
+ }
+ return (
+ <div className="timeline-container">
+ {timeline && (
+ <TimelineInfoCard
+ timeline={timeline}
+ connectionStatus={signalrState}
+ onReload={updateTimeline}
+ />
+ )}
+ {posts && (
+ <div className={classNames("timeline", className)}>
+ {timeline?.postable && (
+ <TimelinePostEdit timeline={timeline} onPosted={updatePosts} />
+ )}
+ <TimelinePostList posts={posts} onReload={updatePosts} />
+ </div>
+ )}
+ </div>
+ );
+}
+
+export default Timeline;