import React from "react"; import { HttpForbiddenError, HttpNetworkError, HttpNotFoundError, } from "@/http/common"; import { getHttpTimelineClient, HttpTimelinePostInfo } from "@/http/timeline"; import { getTimelinePostUpdate$ } from "@/services/timeline"; import TimelinePagedPostListView from "./TimelinePagedPostListView"; import TimelineTop from "./TimelineTop"; import TimelineLoading from "./TimelineLoading"; export interface TimelineProps { className?: string; style?: React.CSSProperties; timelineName?: string; reloadKey: number; onReload: () => void; } const Timeline: React.FC = (props) => { const { timelineName, className, style, reloadKey, onReload } = props; const [state, setState] = React.useState< "loading" | "loaded" | "offline" | "notexist" | "forbid" | "error" >("loading"); const [posts, setPosts] = React.useState([]); React.useEffect(() => { setState("loading"); setPosts([]); }, [timelineName]); React.useEffect(() => { if (timelineName != null && state === "loaded") { const timelinePostUpdate$ = getTimelinePostUpdate$(timelineName); const subscription = timelinePostUpdate$.subscribe(() => { onReload(); }); return () => { subscription.unsubscribe(); }; } }, [timelineName, state, onReload]); React.useEffect(() => { if (timelineName != null) { let subscribe = true; void getHttpTimelineClient() .listPost(timelineName) .then( (data) => { if (subscribe) { setState("loaded"); setPosts(data); } }, (error) => { if (error instanceof HttpNetworkError) { setState("offline"); } else if (error instanceof HttpForbiddenError) { setState("forbid"); } else if (error instanceof HttpNotFoundError) { setState("notexist"); } else { console.error(error); setState("error"); } } ); return () => { subscribe = false; }; } }, [timelineName, reloadKey]); switch (state) { case "loading": return ; case "offline": return (
Offline.
); case "notexist": return (
Not exist.
); case "forbid": return (
Forbid.
); case "error": return (
Error.
); default: return ( <> ); } }; export default Timeline;