MCPcopy Create free account
hub / github.com/dailydotdev/apps / usePatchyBone

Function usePatchyBone

packages/shared/src/features/patchy/patchyBone.ts:26–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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.
26export 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};

Callers 1

PatchyPanelFunction · 0.90

Calls 8

useAuthContextFunction · 0.90
useLogContextFunction · 0.90
getPatchyDayKeyFunction · 0.90
emitPatchyEventFunction · 0.90
freeBoneKeyFunction · 0.85
readBonesGivenFunction · 0.85
bonesGivenKeyFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected