Skip to content

Commit 552e8dd

Browse files
committed
feat: useExpiredDataCheck hook
1 parent 76fd314 commit 552e8dd

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/components/Layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { Toaster } from "@/components/ui/sonner";
44
import { MODE } from "@/env";
55
import { useAnalytics } from "@/hooks/useAnalytics";
66
import { useAuth } from "@/hooks/useAuth";
7+
import { useExpiredDataCheck } from "@/hooks/useExpiredDataCheck";
78
import React from "react";
89
import { Outlet } from "react-router";
910

1011
const Layout: React.FC = () => {
1112
useAnalytics();
1213
useAuth();
14+
useExpiredDataCheck();
1315
return (
1416
<div className="h-dvh min-h-dvh overflow-hidden tracking-tight">
1517
<Background />

src/hooks/useExpiredDataCheck.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { cleanDataAtom, lastEntranceAtom } from "@/atoms/user";
2+
import { DATA_EXPIRY } from "@/env";
3+
import { useAtom, useSetAtom } from "jotai";
4+
import { useEffect, useRef } from "react";
5+
6+
export const useExpiredDataCheck = (): void => {
7+
const [lastEntranceTime, setLastEntranceTime] = useAtom(lastEntranceAtom);
8+
const clearDataAtom = useSetAtom(cleanDataAtom);
9+
const checked = useRef(false);
10+
useEffect(() => {
11+
if (
12+
!checked.current &&
13+
(lastEntranceTime === null ||
14+
lastEntranceTime < new Date(DATA_EXPIRY).getTime())
15+
) {
16+
clearDataAtom();
17+
checked.current = true;
18+
} else if (!checked.current) {
19+
setLastEntranceTime(Date.now());
20+
checked.current = true;
21+
}
22+
// This hook runs only once on mount
23+
// eslint-disable-next-line react-hooks/exhaustive-deps
24+
}, []);
25+
};

src/hooks/useUser.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
cleanDataAtom,
32
isFirstProfileViewAtom,
43
lastEntranceAtom,
54
userGenderAtom,
@@ -15,12 +14,11 @@ import { usePurchasedProfiles, useSelfProfile } from "@/hooks/queries/profiles";
1514
import { useViewerSelf } from "@/hooks/queries/viewers";
1615
import { Gender } from "@/types/profile";
1716
import { useQueryClient } from "@tanstack/react-query";
18-
import { useAtom, useAtomValue, useSetAtom } from "jotai";
17+
import { useAtom, useAtomValue } from "jotai";
1918
import { useCallback, useEffect, useState } from "react";
2019
import { UserData } from "@/types/user";
2120
import { useAuth } from "@/hooks/useAuth";
2221
import { useUserInfo } from "@/hooks/queries/users";
23-
import { DATA_EXPIRY } from "@/env";
2422

2523
export const useUser = (): UserData & {
2624
setIsFirstProfileView: (value: boolean) => void;
@@ -43,15 +41,13 @@ export const useUser = (): UserData & {
4341
const [isFirstProfileView, setIsFirstProfileView] = useAtom(
4442
isFirstProfileViewAtom,
4543
);
44+
const lastEntranceTime = useAtomValue(lastEntranceAtom);
4645
const [viewer, setViewer] = useAtom(viewerAtom);
4746
const [purchasedProfiles, setPurchasedProfiles] = useAtom(
4847
purchasedProfilesAtom,
4948
);
50-
const [lastEntranceTime, setLastEntranceTime] = useAtom(lastEntranceAtom);
5149
const savedProfiles = useAtomValue(savedProfilesAtom);
5250
const recentlyViewedProfileIds = useAtomValue(recentlyViewedProfilesAtom);
53-
const clearDataAtom = useSetAtom(cleanDataAtom);
54-
5551
const queryClient = useQueryClient();
5652

5753
const { data: profileRes, isPending: isProfilePending } = useSelfProfile({
@@ -72,19 +68,6 @@ export const useUser = (): UserData & {
7268
setRefreshInitiated(true);
7369
}, [queryClient]);
7470

75-
useEffect(() => {
76-
if (
77-
lastEntranceTime === null ||
78-
lastEntranceTime < new Date(DATA_EXPIRY).getTime()
79-
) {
80-
clearDataAtom();
81-
} else {
82-
setLastEntranceTime(Date.now());
83-
}
84-
// This hook runs only once on mount
85-
// eslint-disable-next-line react-hooks/exhaustive-deps
86-
}, []);
87-
8871
useEffect(() => {
8972
if (isAuthenticated) {
9073
refreshUser();

0 commit comments

Comments
 (0)