-
Notifications
You must be signed in to change notification settings - Fork 22
/
useLoadingGovProposalTimestampInfo.tsx
60 lines (52 loc) · 2.06 KB
/
useLoadingGovProposalTimestampInfo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useTranslation } from 'react-i18next'
import TimeAgo from 'react-timeago'
import { GovProposal, LoadingData, ProposalTimestampInfo } from '@dao-dao/types'
import { ProposalStatus } from '@dao-dao/types/protobuf/codegen/cosmos/gov/v1beta1/gov'
import { formatDate, formatDateTimeTz } from '@dao-dao/utils'
import { useTranslatedTimeDeltaFormatter } from './useTranslatedTimeDeltaFormatter'
export const useLoadingGovProposalTimestampInfo = (
// If undefined, returns loading.
proposal: GovProposal['proposal'] | undefined
): LoadingData<ProposalTimestampInfo> => {
const { t } = useTranslation()
const timeAgoFormatter = useTranslatedTimeDeltaFormatter({ words: false })
if (!proposal) {
return { loading: true }
}
const depositOrVotingEndDate =
(proposal.status === ProposalStatus.PROPOSAL_STATUS_DEPOSIT_PERIOD
? proposal.depositEndTime
: proposal.votingEndTime) || new Date(0)
const dateDisplay: ProposalTimestampInfo['display'] | undefined =
proposal.status === ProposalStatus.PROPOSAL_STATUS_DEPOSIT_PERIOD ||
proposal.status === ProposalStatus.PROPOSAL_STATUS_VOTING_PERIOD
? {
label: t('title.timeLeft'),
tooltip: formatDateTimeTz(depositOrVotingEndDate),
content: (
<TimeAgo
date={depositOrVotingEndDate}
formatter={timeAgoFormatter}
/>
),
}
: proposal.status === ProposalStatus.PROPOSAL_STATUS_PASSED ||
proposal.status === ProposalStatus.PROPOSAL_STATUS_REJECTED
? {
label:
proposal.status === ProposalStatus.PROPOSAL_STATUS_PASSED
? t('proposalStatusTitle.passed')
: t('proposalStatusTitle.rejected'),
tooltip: formatDateTimeTz(proposal.votingEndTime || new Date(0)),
content: formatDate(proposal.votingEndTime || new Date(0)),
}
: undefined
const timestampInfo: ProposalTimestampInfo = {
display: dateDisplay,
expirationDate: depositOrVotingEndDate,
}
return {
loading: false,
data: timestampInfo,
}
}