|
|
@ -1,50 +1,32 @@
|
|
|
|
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> {
|
|
|
|
|
|
|
|
private interval: any;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.interval = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
|
|
|
|
const { expirationLength } = this.props;
|
|
|
|
|
|
|
|
const increment = getIncrement(expirationLength);
|
|
|
|
|
|
|
|
const updateFrequency = Math.max(increment, 500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const ExpireTimer = (props: Props) => {
|
|
|
|
|
|
|
|
const [lastUpdated, setLastUpdated] = useState(Date.now());
|
|
|
|
const update = () => {
|
|
|
|
const update = () => {
|
|
|
|
this.setState({
|
|
|
|
setLastUpdated(Date.now());
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
this.interval = setInterval(update, updateFrequency);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
|
|
|
|
if (this.interval) {
|
|
|
|
|
|
|
|
clearInterval(this.interval);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
|
|
|
|
const {
|
|
|
|
const {
|
|
|
|
direction,
|
|
|
|
direction,
|
|
|
|
expirationLength,
|
|
|
|
expirationLength,
|
|
|
|
expirationTimestamp,
|
|
|
|
expirationTimestamp,
|
|
|
|
withImageNoCaption,
|
|
|
|
withImageNoCaption,
|
|
|
|
} = this.props;
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const increment = getIncrement(expirationLength);
|
|
|
|
|
|
|
|
const updateFrequency = Math.max(increment, 500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useInterval(update, updateFrequency);
|
|
|
|
|
|
|
|
|
|
|
|
const bucket = getTimerBucket(expirationTimestamp, expirationLength);
|
|
|
|
const bucket = getTimerBucket(expirationTimestamp, expirationLength);
|
|
|
|
let timeLeft = Math.round((expirationTimestamp - Date.now()) / 1000);
|
|
|
|
let timeLeft = Math.round((expirationTimestamp - Date.now()) / 1000);
|
|
|
@ -70,11 +52,8 @@ export class ExpireTimer extends React.Component<Props> {
|
|
|
|
'module-expire-timer-margin',
|
|
|
|
'module-expire-timer-margin',
|
|
|
|
`module-expire-timer--${bucket}`,
|
|
|
|
`module-expire-timer--${bucket}`,
|
|
|
|
`module-expire-timer--${direction}`,
|
|
|
|
`module-expire-timer--${direction}`,
|
|
|
|
withImageNoCaption
|
|
|
|
withImageNoCaption ? 'module-expire-timer--with-image-no-caption' : null
|
|
|
|
? 'module-expire-timer--with-image-no-caption'
|
|
|
|
|
|
|
|
: null
|
|
|
|
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|