()
| 24 | // One free bone a local day. Per device for now, like his other settings: |
| 25 | // keeping count on the server needs an API change. |
| 26 | export const usePatchyBone = (): UsePatchyBone => { |
| 27 | const { user } = useAuthContext(); |
| 28 | const { logEvent } = useLogContext(); |
| 29 | const userId = user?.id ?? ''; |
| 30 | const [, setVersion] = useState(0); |
| 31 | const hasFreeBone = |
| 32 | !!userId && |
| 33 | storageWrapper.getItem(freeBoneKey(userId)) !== getPatchyDayKey(new Date()); |
| 34 | const bonesGiven = userId ? readBonesGiven(userId) : 0; |
| 35 | |
| 36 | const giveFreeBone = useCallback(() => { |
| 37 | if (!userId) { |
| 38 | return; |
| 39 | } |
| 40 | const today = getPatchyDayKey(new Date()); |
| 41 | if (storageWrapper.getItem(freeBoneKey(userId)) === today) { |
| 42 | return; |
| 43 | } |
| 44 | storageWrapper.setItem(freeBoneKey(userId), today); |
| 45 | storageWrapper.setItem( |
| 46 | bonesGivenKey(userId), |
| 47 | String(readBonesGiven(userId) + 1), |
| 48 | ); |
| 49 | setVersion((version) => version + 1); |
| 50 | emitPatchyEvent({ type: 'bone' }); |
| 51 | logEvent({ |
| 52 | event_name: LogEvent.Click, |
| 53 | target_type: TargetType.Patchy, |
| 54 | target_id: 'free_bone', |
| 55 | extra: JSON.stringify({ origin: 'panel' }), |
| 56 | }); |
| 57 | }, [userId, logEvent]); |
| 58 | |
| 59 | return { hasFreeBone, bonesGiven, giveFreeBone }; |
| 60 | }; |
no test coverage detected