mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-03-11 04:49:49 +01:00
fix(#2): state checking
This commit is contained in:
@@ -10,6 +10,7 @@ import LoadingSpinner from './LoadingSpinner';
|
|||||||
import PomodoroTimer from './PomodoroTimer';
|
import PomodoroTimer from './PomodoroTimer';
|
||||||
import RefreshBanner from './RefreshBanner';
|
import RefreshBanner from './RefreshBanner';
|
||||||
import UserSelectModal from './UserSelectModal';
|
import UserSelectModal from './UserSelectModal';
|
||||||
|
import { DATA_FRESHNESS_INTERVAL } from '@/lib/constants';
|
||||||
|
|
||||||
function ClientWrapperContent({ children }: { children: ReactNode }) {
|
function ClientWrapperContent({ children }: { children: ReactNode }) {
|
||||||
const [pomo] = useAtom(pomodoroAtom)
|
const [pomo] = useAtom(pomodoroAtom)
|
||||||
@@ -52,9 +53,7 @@ function ClientWrapperContent({ children }: { children: ReactNode }) {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Interval for polling data freshness
|
// Interval for polling data freshness
|
||||||
if (clientToken && !showRefreshBanner && status === 'authenticated') {
|
if (clientToken && !showRefreshBanner && status === 'authenticated') {
|
||||||
const intervalId = setInterval(() => {
|
const intervalId = setInterval(performFreshnessCheck, DATA_FRESHNESS_INTERVAL);
|
||||||
performFreshnessCheck();
|
|
||||||
}, 30000); // Check every 30 seconds
|
|
||||||
|
|
||||||
return () => clearInterval(intervalId);
|
return () => clearInterval(intervalId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ export default function UserSelectModal({ onClose }: { onClose: () => void }) {
|
|||||||
const [isCreating, setIsCreating] = useState(false);
|
const [isCreating, setIsCreating] = useState(false);
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
const [usersData, setUsersData] = useAtom(usersAtom);
|
const [usersData] = useAtom(usersAtom);
|
||||||
const users = usersData.users;
|
const users = usersData.users;
|
||||||
const [currentUser] = useAtom(currentUserAtom);
|
const [currentUser] = useAtom(currentUserAtom);
|
||||||
|
|
||||||
|
|||||||
10
lib/atoms.ts
10
lib/atoms.ts
@@ -22,6 +22,7 @@ import {
|
|||||||
getDefaultWishlistData,
|
getDefaultWishlistData,
|
||||||
Habit,
|
Habit,
|
||||||
PomodoroAtom,
|
PomodoroAtom,
|
||||||
|
User,
|
||||||
UserId
|
UserId
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
@@ -77,6 +78,10 @@ export const currentUserAtom = atom((get) => {
|
|||||||
return users.users.find(user => user.id === currentUserId);
|
return users.users.find(user => user.id === currentUserId);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function removeHasPassword(users: Omit<User, 'password'>[]): Omit<User, 'password'>[] {
|
||||||
|
return users.map(user => { delete user.hasPassword; return user });
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronous atom that calculates a freshness token (hash) based on the current client-side data.
|
* Asynchronous atom that calculates a freshness token (hash) based on the current client-side data.
|
||||||
* This token can be compared with a server-generated token to detect data discrepancies.
|
* This token can be compared with a server-generated token to detect data discrepancies.
|
||||||
@@ -86,9 +91,10 @@ export const clientFreshnessTokenAtom = atom(async (get) => {
|
|||||||
const habits = get(habitsAtom);
|
const habits = get(habitsAtom);
|
||||||
const coins = get(coinsAtom);
|
const coins = get(coinsAtom);
|
||||||
const wishlist = get(wishlistAtom);
|
const wishlist = get(wishlistAtom);
|
||||||
const users = get(usersAtom);
|
const users = structuredClone(get(usersAtom));
|
||||||
|
const usersWithoutHasPassword = removeHasPassword(users.users);
|
||||||
|
|
||||||
const dataString = prepareDataForHashing(settings, habits, coins, wishlist, users);
|
const dataString = prepareDataForHashing(settings, habits, coins, wishlist, { users: usersWithoutHasPassword });
|
||||||
const hash = await generateCryptoHash(dataString);
|
const hash = await generateCryptoHash(dataString);
|
||||||
return hash;
|
return hash;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -34,3 +34,5 @@ export const QUICK_DATES = [
|
|||||||
export const MAX_COIN_LIMIT = 9999
|
export const MAX_COIN_LIMIT = 9999
|
||||||
|
|
||||||
export const DESKTOP_DISPLAY_ITEM_COUNT = 4
|
export const DESKTOP_DISPLAY_ITEM_COUNT = 4
|
||||||
|
|
||||||
|
export const DATA_FRESHNESS_INTERVAL = 30000;
|
||||||
Reference in New Issue
Block a user