2022-01-26 23:05:26 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
2022-03-09 22:44:57 +00:00
|
|
|
import type { CSSProperties, ReactElement } from 'react';
|
2022-01-28 18:31:20 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-05-26 19:18:17 +00:00
|
|
|
import { animated, useSpring } from '@react-spring/web';
|
|
|
|
|
2022-01-26 23:05:26 +00:00
|
|
|
import type { LocalizerType } from '../../types/Util';
|
|
|
|
import { TimelineDateHeader } from './TimelineDateHeader';
|
|
|
|
import { Spinner } from '../Spinner';
|
|
|
|
|
2022-05-26 19:18:17 +00:00
|
|
|
export type PropsType = Readonly<{
|
|
|
|
i18n: LocalizerType;
|
|
|
|
isLoading: boolean;
|
|
|
|
style?: CSSProperties;
|
|
|
|
timestamp: number;
|
|
|
|
visible: boolean;
|
|
|
|
}>;
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function TimelineFloatingHeader({
|
2022-01-26 23:05:26 +00:00
|
|
|
i18n,
|
|
|
|
isLoading,
|
2022-03-09 22:44:57 +00:00
|
|
|
style,
|
2022-01-26 23:05:26 +00:00
|
|
|
timestamp,
|
|
|
|
visible,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): ReactElement {
|
2022-01-28 18:31:20 +00:00
|
|
|
const [hasRendered, setHasRendered] = useState(false);
|
2022-05-26 19:18:17 +00:00
|
|
|
const [showSpinner, setShowSpinner] = useState(isLoading);
|
2022-01-28 18:31:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setHasRendered(true);
|
|
|
|
}, []);
|
|
|
|
|
2022-05-26 19:18:17 +00:00
|
|
|
const [spinnerStyles, spinnerSpringRef] = useSpring(
|
|
|
|
() => ({
|
|
|
|
delay: 300,
|
|
|
|
duration: 250,
|
|
|
|
from: { opacity: 1 },
|
|
|
|
to: { opacity: 0 },
|
|
|
|
onRest: {
|
|
|
|
opacity: ({ value }) => {
|
|
|
|
if (value === 0) {
|
|
|
|
setShowSpinner(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
[isLoading]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isLoading) {
|
|
|
|
spinnerSpringRef.stop();
|
|
|
|
spinnerSpringRef.set({ opacity: 1 });
|
|
|
|
setShowSpinner(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLoading && showSpinner) {
|
|
|
|
spinnerSpringRef.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLoading && !showSpinner) {
|
|
|
|
spinnerSpringRef.stop();
|
|
|
|
}
|
|
|
|
}, [isLoading, showSpinner, spinnerSpringRef]);
|
|
|
|
|
2022-01-28 18:31:20 +00:00
|
|
|
return (
|
2022-01-26 23:05:26 +00:00
|
|
|
<div
|
2023-04-24 20:19:16 +00:00
|
|
|
aria-level={5}
|
2022-01-26 23:05:26 +00:00
|
|
|
className={classNames(
|
2022-01-28 18:31:20 +00:00
|
|
|
'TimelineFloatingHeader',
|
|
|
|
`TimelineFloatingHeader--${
|
|
|
|
visible && hasRendered ? 'visible' : 'hidden'
|
2022-01-26 23:05:26 +00:00
|
|
|
}`
|
|
|
|
)}
|
2023-04-24 20:19:16 +00:00
|
|
|
role="heading"
|
2022-03-09 22:44:57 +00:00
|
|
|
style={style}
|
2022-01-26 23:05:26 +00:00
|
|
|
>
|
2022-01-28 18:31:20 +00:00
|
|
|
<TimelineDateHeader floating i18n={i18n} timestamp={timestamp} />
|
2022-05-26 19:18:17 +00:00
|
|
|
{showSpinner && (
|
|
|
|
<animated.div
|
|
|
|
className="TimelineFloatingHeader__spinner-container"
|
|
|
|
style={spinnerStyles}
|
|
|
|
>
|
|
|
|
<Spinner direction="on-background" size="20px" svgSize="small" />
|
|
|
|
</animated.div>
|
|
|
|
)}
|
2022-01-26 23:05:26 +00:00
|
|
|
</div>
|
2022-01-28 18:31:20 +00:00
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|