2023-01-03 11:55:46 -08:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 15:34:04 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-04-12 16:17:56 -07:00
|
|
|
import React, { useEffect, useReducer } from 'react';
|
2018-07-09 14:29:13 -07:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2019-01-14 13:49:58 -08:00
|
|
|
import { getIncrement, getTimerBucket } from '../../util/timer';
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2021-01-14 12:07:05 -06:00
|
|
|
export type Props = {
|
2022-04-07 09:58:15 -07:00
|
|
|
direction?: 'incoming' | 'outgoing';
|
|
|
|
expirationLength: number;
|
|
|
|
expirationTimestamp?: number;
|
2023-10-30 09:24:28 -07:00
|
|
|
isOutlineOnlyBubble?: boolean;
|
2019-06-26 12:33:13 -07:00
|
|
|
withImageNoCaption?: boolean;
|
|
|
|
withSticker?: boolean;
|
|
|
|
withTapToViewExpired?: boolean;
|
2021-01-14 12:07:05 -06:00
|
|
|
};
|
2018-07-09 14:29:13 -07:00
|
|
|
|
2023-04-12 16:17:56 -07:00
|
|
|
export function ExpireTimer({
|
|
|
|
direction,
|
|
|
|
expirationLength,
|
|
|
|
expirationTimestamp,
|
2023-10-30 09:24:28 -07:00
|
|
|
isOutlineOnlyBubble,
|
2023-04-12 16:17:56 -07:00
|
|
|
withImageNoCaption,
|
|
|
|
withSticker,
|
|
|
|
withTapToViewExpired,
|
|
|
|
}: Props): JSX.Element {
|
|
|
|
const [, forceUpdate] = useReducer(() => ({}), {});
|
|
|
|
|
|
|
|
useEffect(() => {
|
2018-07-09 14:29:13 -07:00
|
|
|
const increment = getIncrement(expirationLength);
|
|
|
|
const updateFrequency = Math.max(increment, 500);
|
2023-04-12 16:17:56 -07:00
|
|
|
const interval = setInterval(forceUpdate, updateFrequency);
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval);
|
2018-07-09 14:29:13 -07:00
|
|
|
};
|
2023-04-12 16:17:56 -07:00
|
|
|
}, [expirationLength]);
|
|
|
|
|
|
|
|
const bucket = getTimerBucket(expirationTimestamp, expirationLength);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-expire-timer',
|
|
|
|
`module-expire-timer--${bucket}`,
|
|
|
|
direction ? `module-expire-timer--${direction}` : null,
|
2023-10-30 09:24:28 -07:00
|
|
|
isOutlineOnlyBubble ? 'module-expire-timer--outline-only-bubble' : null,
|
2023-04-12 16:17:56 -07:00
|
|
|
withTapToViewExpired
|
|
|
|
? `module-expire-timer--${direction}-with-tap-to-view-expired`
|
|
|
|
: null,
|
|
|
|
direction && withImageNoCaption
|
|
|
|
? 'module-expire-timer--with-image-no-caption'
|
|
|
|
: null,
|
|
|
|
withSticker ? 'module-expire-timer--with-sticker' : null
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
2018-07-09 14:29:13 -07:00
|
|
|
}
|