Skip to content

Commit

Permalink
Update Airdrop sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
saltict committed Aug 3, 2024
1 parent a16ec18 commit 480cf7e
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions packages/extension-koni-ui/src/Popup/Home/Airdrop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,41 @@ const AirdropComponent: React.FC<Props> = ({ className }) => {
const [airdropCampaign, setAirdropCampaign] = useState<AirdropCampaign[]>(apiSDK.airdropCampaignList);

const orderAirdropCampaign = useMemo(() => {
return [...airdropCampaign].sort((a, b) => {
if (a.start === null && b.start === null) {
return 0;
const futureList: AirdropCampaign[] = [];
const nowList: AirdropCampaign[] = [];
const pastList: AirdropCampaign[] = [];

airdropCampaign.forEach((campaign) => {
const now = Date.now();
const start = new Date(campaign.start).getTime();
const end = new Date(campaign.end).getTime();

if (now < start) {
futureList.push(campaign);
} else if (now > end) {
pastList.push(campaign);
} else {
nowList.push(campaign);
}
});

if (a.start === null) {
return -1;
function sortByStart (a: AirdropCampaign, b: AirdropCampaign) {
if (!a.start || !b.start) {
return 0;
}

if (b.start === null) {
return 1;
return new Date(a.start).getTime() - new Date(b.start).getTime();
}

function sortByStartDesc (a: AirdropCampaign, b: AirdropCampaign) {
if (!a.start || !b.start) {
return 0;
}

const aTime = new Date(a.end).getTime();
const bTime = new Date(b.end).getTime();
return new Date(b.start).getTime() - new Date(a.start).getTime();
}

return bTime - aTime;
});
return [...nowList.sort(sortByStartDesc), ...futureList.sort(sortByStart), ...pastList];
}, [airdropCampaign]);

const onExplore = useCallback((campaignId: number) => {
Expand Down

0 comments on commit 480cf7e

Please sign in to comment.