fix timer expiring sometimes missing a sec
parent
1fd15ac977
commit
bb87eb0f52
@ -1,80 +1,59 @@
|
|||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
import { getIncrement, getTimerBucket } from '../../util/timer';
|
import { getIncrement, getTimerBucket } from '../../util/timer';
|
||||||
|
import { useInterval } from '../../hooks/useInterval';
|
||||||
|
|
||||||
interface Props {
|
type Props = {
|
||||||
withImageNoCaption: boolean;
|
withImageNoCaption: boolean;
|
||||||
expirationLength: number;
|
expirationLength: number;
|
||||||
expirationTimestamp: number;
|
expirationTimestamp: number;
|
||||||
direction: 'incoming' | 'outgoing';
|
direction: 'incoming' | 'outgoing';
|
||||||
}
|
};
|
||||||
|
|
||||||
export class ExpireTimer extends React.Component<Props> {
|
export const ExpireTimer = (props: Props) => {
|
||||||
private interval: any;
|
const [lastUpdated, setLastUpdated] = useState(Date.now());
|
||||||
|
const update = () => {
|
||||||
constructor(props: Props) {
|
setLastUpdated(Date.now());
|
||||||
super(props);
|
};
|
||||||
|
const {
|
||||||
this.interval = null;
|
direction,
|
||||||
}
|
expirationLength,
|
||||||
|
expirationTimestamp,
|
||||||
public componentDidMount() {
|
withImageNoCaption,
|
||||||
const { expirationLength } = this.props;
|
} = props;
|
||||||
const increment = getIncrement(expirationLength);
|
|
||||||
const updateFrequency = Math.max(increment, 500);
|
const increment = getIncrement(expirationLength);
|
||||||
|
const updateFrequency = Math.max(increment, 500);
|
||||||
const update = () => {
|
|
||||||
this.setState({
|
useInterval(update, updateFrequency);
|
||||||
lastUpdated: Date.now(),
|
|
||||||
});
|
const bucket = getTimerBucket(expirationTimestamp, expirationLength);
|
||||||
};
|
let timeLeft = Math.round((expirationTimestamp - Date.now()) / 1000);
|
||||||
this.interval = setInterval(update, updateFrequency);
|
timeLeft = timeLeft >= 0 ? timeLeft : 0;
|
||||||
}
|
if (timeLeft <= 60) {
|
||||||
|
|
||||||
public componentWillUnmount() {
|
|
||||||
if (this.interval) {
|
|
||||||
clearInterval(this.interval);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public render() {
|
|
||||||
const {
|
|
||||||
direction,
|
|
||||||
expirationLength,
|
|
||||||
expirationTimestamp,
|
|
||||||
withImageNoCaption,
|
|
||||||
} = this.props;
|
|
||||||
|
|
||||||
const bucket = getTimerBucket(expirationTimestamp, expirationLength);
|
|
||||||
let timeLeft = Math.round((expirationTimestamp - Date.now()) / 1000);
|
|
||||||
timeLeft = timeLeft >= 0 ? timeLeft : 0;
|
|
||||||
if (timeLeft <= 60) {
|
|
||||||
return (
|
|
||||||
<span
|
|
||||||
className={classNames(
|
|
||||||
'module-expire-timer-margin',
|
|
||||||
'module-message__metadata__date',
|
|
||||||
`module-message__metadata__date--${direction}`
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{timeLeft}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<span
|
||||||
className={classNames(
|
className={classNames(
|
||||||
'module-expire-timer',
|
|
||||||
'module-expire-timer-margin',
|
'module-expire-timer-margin',
|
||||||
`module-expire-timer--${bucket}`,
|
'module-message__metadata__date',
|
||||||
`module-expire-timer--${direction}`,
|
`module-message__metadata__date--${direction}`
|
||||||
withImageNoCaption
|
|
||||||
? 'module-expire-timer--with-image-no-caption'
|
|
||||||
: null
|
|
||||||
)}
|
)}
|
||||||
/>
|
>
|
||||||
|
{timeLeft}
|
||||||
|
</span>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={classNames(
|
||||||
|
'module-expire-timer',
|
||||||
|
'module-expire-timer-margin',
|
||||||
|
`module-expire-timer--${bucket}`,
|
||||||
|
`module-expire-timer--${direction}`,
|
||||||
|
withImageNoCaption ? 'module-expire-timer--with-image-no-caption' : null
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue