-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
187 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useAirdropRequest } from '../utils/useHandleRequest'; | ||
import { ReferralsResponse, useShellOfSecretsStore } from '@app/store/useShellOfSecretsStore'; | ||
import { CrewMember } from '@app/types/ws'; | ||
|
||
const MAX_REFERRALS = 100; | ||
|
||
export const useGetSosReferrals = () => { | ||
const [intervalSeconds, setIntervalSeconds] = useState(0); | ||
const handleRequest = useAirdropRequest(); | ||
const setReferrals = useShellOfSecretsStore((state) => state.setReferrals); | ||
const referrals = useShellOfSecretsStore((state) => state.referrals); | ||
|
||
const fetchUserReferrals = useCallback(async () => { | ||
const data = await handleRequest<ReferralsResponse>({ | ||
path: '/sos/referrals/', | ||
method: 'GET', | ||
}); | ||
if (!data?.toleranceMs) return; | ||
setReferrals(data); | ||
}, [handleRequest, setReferrals]); | ||
|
||
const fetchCrewMemberDetails = useCallback( | ||
async (userId: string) => { | ||
const existingReferral = referrals?.activeReferrals?.find((x) => x.id === userId); | ||
let updatedReferrals = referrals?.activeReferrals || []; | ||
let totalActiveReferrals = referrals?.totalActiveReferrals || 0; | ||
|
||
let shouldUpdate = false; | ||
if (!referrals) return; | ||
|
||
if (!existingReferral) { | ||
shouldUpdate = true; | ||
totalActiveReferrals += 1; | ||
|
||
const data = await handleRequest<CrewMember>({ | ||
path: `/sos/crew-member-data/${userId}/`, | ||
method: 'GET', | ||
}); | ||
|
||
if (data && data.id) { | ||
updatedReferrals.push(data); | ||
|
||
// Remove referrals if there are too many saved crew members | ||
if (updatedReferrals.length > MAX_REFERRALS) { | ||
const indexToRemove = updatedReferrals.findIndex((member) => member.active === false); | ||
if (indexToRemove > -1) { | ||
updatedReferrals.splice(indexToRemove, 1); | ||
} else { | ||
updatedReferrals.splice(15, 1); | ||
} | ||
} | ||
} | ||
} else if (existingReferral.active) { | ||
shouldUpdate = true; | ||
updatedReferrals = updatedReferrals.map((x) => { | ||
if (x.id === userId) { | ||
return { ...x, active: true }; | ||
} | ||
return x; | ||
}); | ||
} | ||
|
||
if (shouldUpdate) { | ||
setReferrals({ | ||
...referrals, | ||
totalActiveReferrals, | ||
activeReferrals: updatedReferrals, | ||
}); | ||
} | ||
}, | ||
[handleRequest, referrals, setReferrals] | ||
); | ||
|
||
useEffect(() => { | ||
if (intervalSeconds) { | ||
const intervalId = setInterval(fetchUserReferrals, intervalSeconds * 1000); | ||
return () => clearInterval(intervalId); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [intervalSeconds]); | ||
|
||
return { | ||
fetchUserReferrals, | ||
setRefetchIntervalSeconds: setIntervalSeconds, | ||
fetchCrewMemberDetails, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useAirdropStore } from '@app/store/useAirdropStore'; | ||
import { useShellOfSecretsStore } from '@app/store/useShellOfSecretsStore'; | ||
import { WebsocketEventNames, WebsocketUserEvent } from '@app/types/ws'; | ||
import { useGetSosReferrals } from '../stateHelpers/useGetSosReferrals'; | ||
|
||
export const useHandleWsUserIdEvent = () => { | ||
const setTotalBonusTimeMs = useShellOfSecretsStore((state) => state.setTotalBonusTimeMs); | ||
const referrals = useShellOfSecretsStore((state) => state.referrals); | ||
const setReferrals = useShellOfSecretsStore((state) => state.setReferrals); | ||
const setUserGems = useAirdropStore((state) => state.setUserGems); | ||
const { fetchCrewMemberDetails } = useGetSosReferrals(); | ||
|
||
return (event: string) => { | ||
const eventParsed = JSON.parse(event) as WebsocketUserEvent; | ||
switch (eventParsed.name) { | ||
case WebsocketEventNames.COMPLETED_QUEST: | ||
if (eventParsed.data.userPoints?.gems) { | ||
setUserGems(eventParsed.data.userPoints?.gems); | ||
} | ||
break; | ||
case WebsocketEventNames.MINING_STATUS_CREW_UPDATE: { | ||
fetchCrewMemberDetails(eventParsed.data.crewMember.id); | ||
setTotalBonusTimeMs(eventParsed.data.totalTimeBonusMs); | ||
break; | ||
} | ||
|
||
case WebsocketEventNames.MINING_STATUS_CREW_DISCONNECTED: | ||
if (referrals?.activeReferrals) { | ||
const totalActiveReferrals = (referrals?.totalActiveReferrals || 1) - 1; | ||
const referralsUpdated = referrals?.activeReferrals.map((x) => { | ||
if (x.id === eventParsed.data.crewMemberId) { | ||
return { ...x, active: false }; | ||
} | ||
return x; | ||
}); | ||
|
||
setReferrals({ | ||
...referrals, | ||
totalActiveReferrals, | ||
activeReferrals: referralsUpdated, | ||
}); | ||
} | ||
break; | ||
case WebsocketEventNames.MINING_STATUS_USER_UPDATE: | ||
setTotalBonusTimeMs(eventParsed.data.totalTimeBonusMs); | ||
break; | ||
default: | ||
// eslint-disable-next-line no-console | ||
console.log('Unknown event', eventParsed); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters