signal-desktop/ts/components/conversation/TimelineFloatingHeader.tsx

90 lines
2.1 KiB
TypeScript
Raw Normal View History

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';
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';
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);
const [showSpinner, setShowSpinner] = useState(isLoading);
2022-01-28 18:31:20 +00:00
useEffect(() => {
setHasRendered(true);
}, []);
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
className={classNames(
2022-01-28 18:31:20 +00:00
'TimelineFloatingHeader',
`TimelineFloatingHeader--${
visible && hasRendered ? 'visible' : 'hidden'
2022-01-26 23:05:26 +00:00
}`
)}
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} />
{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
}