1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
import React from "react";
import { UiLogicError } from "@/common";
import { useUser } from "@/services/user";
import {
TimelinePostInfo,
timelineService,
usePosts,
useTimeline,
} from "@/services/timeline";
import { mergeDataStatus } from "@/services/DataHub2";
import { TimelineMemberDialog } from "./TimelineMember";
import TimelinePropertyChangeDialog from "./TimelinePropertyChangeDialog";
import {
TimelinePageTemplateUIOperations,
TimelinePageTemplateUIProps,
} from "./TimelinePageTemplateUI";
export interface TimelinePageTemplateProps<TManageItem> {
name: string;
onManage: (item: TManageItem) => void;
UiComponent: React.ComponentType<
Omit<TimelinePageTemplateUIProps<TManageItem>, "CardComponent">
>;
notFoundI18nKey: string;
}
export default function TimelinePageTemplate<TManageItem>(
props: TimelinePageTemplateProps<TManageItem>
): React.ReactElement | null {
const { name } = props;
const service = timelineService;
const user = useUser();
const [dialog, setDialog] = React.useState<null | "property" | "member">(
null
);
const [scrollBottomKey, setScrollBottomKey] = React.useState<number>(0);
React.useEffect(() => {
if (scrollBottomKey > 0) {
window.scrollTo(0, document.body.scrollHeight);
}
}, [scrollBottomKey]);
const timelineAndStatus = useTimeline(name);
const postsAndState = usePosts(name);
const [
scrollToBottomNextSyncKey,
setScrollToBottomNextSyncKey,
] = React.useState<number>(0);
const scrollToBottomNextSync = (): void => {
setScrollToBottomNextSyncKey((old) => old + 1);
};
React.useEffect(() => {
let subscribe = true;
void timelineService.syncPosts(name).then(() => {
if (subscribe) {
setScrollBottomKey((old) => old + 1);
}
});
return () => {
subscribe = false;
};
}, [name, scrollToBottomNextSyncKey]);
const uiTimelineProp = ((): TimelinePageTemplateUIProps<TManageItem>["timeline"] => {
const { status, data: timeline } = timelineAndStatus;
if (timeline == null) {
if (status === "offline") {
return "offline";
} else {
return undefined;
}
} else if (timeline === "notexist") {
return "notexist";
} else {
const operations: TimelinePageTemplateUIOperations<TManageItem> = {
onPost: service.hasPostPermission(user, timeline)
? (req) =>
service.createPost(name, req).then(() => scrollToBottomNextSync())
: undefined,
onManage: service.hasManagePermission(user, timeline)
? (item) => {
if (item === "property") {
setDialog(item);
} else {
props.onManage(item);
}
}
: undefined,
onMember: () => setDialog("member"),
};
const posts = ((): TimelinePostInfo[] | "forbid" | undefined => {
const { data: postsInfo } = postsAndState;
if (postsInfo === "forbid") {
return "forbid";
} else if (postsInfo == null || postsInfo === "notexist") {
return undefined;
} else {
return postsInfo.posts;
}
})();
return { ...timeline, operations, posts };
}
})();
const timeline = timelineAndStatus?.data;
let dialogElement: React.ReactElement | undefined;
const closeDialog = (): void => setDialog(null);
if (dialog === "property") {
if (timeline == null || timeline === "notexist") {
throw new UiLogicError(
"Timeline is null but attempt to open change property dialog."
);
}
dialogElement = (
<TimelinePropertyChangeDialog
open
close={closeDialog}
timeline={timeline}
onProcess={(req) => service.changeTimelineProperty(name, req)}
/>
);
} else if (dialog === "member") {
if (timeline == null || timeline === "notexist") {
throw new UiLogicError(
"Timeline is null but attempt to open change property dialog."
);
}
dialogElement = (
<TimelineMemberDialog
open
onClose={closeDialog}
timeline={timeline}
editable={service.hasManagePermission(user, timeline)}
/>
);
}
const { UiComponent } = props;
return (
<>
<UiComponent
timeline={uiTimelineProp}
syncStatus={mergeDataStatus([
timelineAndStatus.status,
postsAndState.status,
])}
notExistMessageI18nKey={props.notFoundI18nKey}
/>
{dialogElement}
</>
);
}
|