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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import React from 'react';
import { Spinner } from 'reactstrap';
import { useTranslation } from 'react-i18next';
import { fromEvent } from 'rxjs';
import Svg from 'react-inlinesvg';
import arrowsAngleContractIcon from 'bootstrap-icons/icons/arrows-angle-contract.svg';
import arrowsAngleExpandIcon from 'bootstrap-icons/icons/arrows-angle-expand.svg';
import { getAlertHost } from '../common/alert-service';
import Timeline, {
TimelinePostInfoEx,
TimelineDeleteCallback,
} from './Timeline';
import AppBar from '../common/AppBar';
import TimelinePostEdit, { TimelinePostSendCallback } from './TimelinePostEdit';
import { useEventEmiiter } from '../common';
export interface TimelineCardComponentProps<TTimeline, TManageItems> {
timeline: TTimeline;
onManage?: (item: TManageItems | 'property') => void;
onMember: () => void;
className?: string;
onHeight?: (height: number) => void;
}
export interface TimelinePageTemplateUIProps<
TTimeline extends { uniqueId: string },
TManageItems
> {
avatarKey?: string | number;
timeline?: TTimeline;
posts?: TimelinePostInfoEx[] | 'forbid';
CardComponent: React.ComponentType<
TimelineCardComponentProps<TTimeline, TManageItems>
>;
onMember: () => void;
onManage?: (item: TManageItems | 'property') => void;
onPost?: TimelinePostSendCallback;
onDelete: TimelineDeleteCallback;
error?: string;
}
export default function TimelinePageTemplateUI<
TTimeline extends { uniqueId: string },
TEditItems
>(
props: TimelinePageTemplateUIProps<TTimeline, TEditItems>
): React.ReactElement | null {
const { timeline } = props;
const { t } = useTranslation();
const bottomSpaceRef = React.useRef<HTMLDivElement | null>(null);
const onPostEditHeightChange = React.useCallback((height: number): void => {
const { current: bottomSpaceDiv } = bottomSpaceRef;
if (bottomSpaceDiv != null) {
bottomSpaceDiv.style.height = `${height}px`;
}
if (height === 0) {
const alertHost = getAlertHost();
if (alertHost != null) {
alertHost.style.removeProperty('margin-bottom');
}
} else {
const alertHost = getAlertHost();
if (alertHost != null) {
alertHost.style.marginBottom = `${height}px`;
}
}
}, []);
const timelineRef = React.useRef<HTMLDivElement | null>(null);
const [getResizeEvent, triggerResizeEvent] = useEventEmiiter();
React.useEffect(() => {
const { current: timelineElement } = timelineRef;
if (timelineElement != null) {
let loadingScrollToBottom = true;
let pinBottom = false;
const isAtBottom = (): boolean =>
window.innerHeight + window.scrollY + 10 >= document.body.scrollHeight;
const disableLoadingScrollToBottom = (): void => {
loadingScrollToBottom = false;
if (isAtBottom()) pinBottom = true;
};
const checkAndScrollToBottom = (): void => {
if (loadingScrollToBottom || pinBottom) {
window.scrollTo(0, document.body.scrollHeight);
}
};
const subscriptions = [
fromEvent(timelineElement, 'wheel').subscribe(
disableLoadingScrollToBottom
),
fromEvent(timelineElement, 'pointerdown').subscribe(
disableLoadingScrollToBottom
),
fromEvent(timelineElement, 'keydown').subscribe(
disableLoadingScrollToBottom
),
fromEvent(window, 'scroll').subscribe(() => {
if (loadingScrollToBottom) return;
if (isAtBottom()) {
pinBottom = true;
} else {
pinBottom = false;
}
}),
fromEvent(window, 'resize').subscribe(checkAndScrollToBottom),
getResizeEvent().subscribe(checkAndScrollToBottom),
];
return () => {
subscriptions.forEach((s) => s.unsubscribe());
};
}
}, [getResizeEvent, triggerResizeEvent, timeline, props.posts]);
const [cardHeight, setCardHeight] = React.useState<number>(0);
const genCardCollapseLocalStorageKey = (uniqueId: string): string =>
`timeline.${uniqueId}.cardCollapse`;
const cardCollapseLocalStorageKey =
timeline != null ? genCardCollapseLocalStorageKey(timeline.uniqueId) : null;
const [infoCardCollapse, setInfoCardCollapse] = React.useState<boolean>(true);
React.useEffect(() => {
if (cardCollapseLocalStorageKey != null) {
const savedCollapse =
window.localStorage.getItem(cardCollapseLocalStorageKey) === 'true';
setInfoCardCollapse(savedCollapse);
}
}, [cardCollapseLocalStorageKey]);
let body: React.ReactElement;
if (props.error != null) {
body = <p className="text-danger">{t(props.error)}</p>;
} else {
if (timeline != null) {
let timelineBody: React.ReactElement;
if (props.posts != null) {
if (props.posts === 'forbid') {
timelineBody = (
<p className="text-danger">{t('timeline.messageCantSee')}</p>
);
} else {
timelineBody = (
<Timeline
containerRef={timelineRef}
posts={props.posts}
onDelete={props.onDelete}
onResize={triggerResizeEvent}
/>
);
if (props.onPost != null) {
timelineBody = (
<>
{timelineBody}
<div ref={bottomSpaceRef} className="flex-fix-length" />
<TimelinePostEdit
onPost={props.onPost}
onHeightChange={onPostEditHeightChange}
timelineUniqueId={timeline.uniqueId}
/>
</>
);
}
}
} else {
timelineBody = (
<div className="full-viewport-center-child">
<Spinner color="primary" type="grow" />
</div>
);
}
const { CardComponent } = props;
body = (
<>
<div
className="fixed-top mt-appbar info-card-container"
data-collapse={infoCardCollapse ? 'true' : 'false'}
>
<Svg
src={
infoCardCollapse
? arrowsAngleExpandIcon
: arrowsAngleContractIcon
}
onClick={() => {
const newState = !infoCardCollapse;
setInfoCardCollapse(newState);
window.localStorage.setItem(
genCardCollapseLocalStorageKey(timeline.uniqueId),
newState.toString()
);
}}
className="float-right m-1 info-card-collapse-button text-primary icon-button"
/>
<CardComponent
timeline={timeline}
onManage={props.onManage}
onMember={props.onMember}
onHeight={setCardHeight}
className="info-card-content"
/>
</div>
{timelineBody}
</>
);
} else {
body = (
<div className="full-viewport-center-child">
<Spinner color="primary" type="grow" />
</div>
);
}
}
return (
<>
<AppBar />
<div>
<div
style={{ height: 56 + cardHeight }}
className="timeline-page-top-space flex-fix-length"
/>
{body}
</div>
</>
);
}
|