This commit is contained in:
@@ -73,11 +73,11 @@ interface HabitResponse {
|
|||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [userToken, setUserToken] = useState<string | null>(null);
|
|
||||||
const [showNewHabitDialog, setShowNewHabitDialog] = useState(false);
|
const [showNewHabitDialog, setShowNewHabitDialog] = useState(false);
|
||||||
const [newHabitName, setNewHabitName] = useState('');
|
const [newHabitName, setNewHabitName] = useState('');
|
||||||
const [newHabitType, setNewHabitType] = useState<'positive' | 'neutral' | 'negative'>('neutral');
|
const [newHabitType, setNewHabitType] = useState<'positive' | 'neutral' | 'negative'>('neutral');
|
||||||
const [copiedToken, setCopiedToken] = useState(false);
|
const [copiedToken, setCopiedToken] = useState(false);
|
||||||
|
const [currentTime, setCurrentTime] = useState(() => Date.now());
|
||||||
|
|
||||||
// Check authentication
|
// Check authentication
|
||||||
const { data: authData, isLoading: authLoading } = useQuery<AuthData>({
|
const { data: authData, isLoading: authLoading } = useQuery<AuthData>({
|
||||||
@@ -92,11 +92,15 @@ export default function Dashboard() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update current time periodically to avoid impure Date.now() calls during render
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authData?.token) {
|
const interval = setInterval(() => {
|
||||||
setUserToken(authData.token);
|
setCurrentTime(Date.now());
|
||||||
}
|
}, 60000); // Update every minute
|
||||||
}, [authData]);
|
return () => {
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Fetch habits
|
// Fetch habits
|
||||||
const { data: habitsData, isLoading: habitsLoading } = useQuery<HabitsResponse>({
|
const { data: habitsData, isLoading: habitsLoading } = useQuery<HabitsResponse>({
|
||||||
@@ -154,8 +158,8 @@ export default function Dashboard() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const copyToken = () => {
|
const copyToken = () => {
|
||||||
if (userToken) {
|
if (authData?.token) {
|
||||||
void navigator.clipboard.writeText(userToken);
|
void navigator.clipboard.writeText(authData.token);
|
||||||
setCopiedToken(true);
|
setCopiedToken(true);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setCopiedToken(false);
|
setCopiedToken(false);
|
||||||
@@ -199,7 +203,7 @@ export default function Dashboard() {
|
|||||||
const getAverageFrequency = (habit: Habit) => {
|
const getAverageFrequency = (habit: Habit) => {
|
||||||
const daysSinceCreation = Math.max(
|
const daysSinceCreation = Math.max(
|
||||||
1,
|
1,
|
||||||
Math.floor((Date.now() - new Date(habit.createdAt).getTime()) / (1000 * 60 * 60 * 24)),
|
Math.floor((currentTime - new Date(habit.createdAt).getTime()) / (1000 * 60 * 60 * 24)),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (daysSinceCreation <= 7) {
|
if (daysSinceCreation <= 7) {
|
||||||
@@ -320,13 +324,13 @@ export default function Dashboard() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Token Alert */}
|
{/* Token Alert */}
|
||||||
{userToken && (
|
{authData?.token && (
|
||||||
<Alert className="border-zinc-800 bg-zinc-950">
|
<Alert className="border-zinc-800 bg-zinc-950">
|
||||||
<AlertDescription className="flex items-center justify-between">
|
<AlertDescription className="flex items-center justify-between">
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<p className="text-sm text-zinc-400">Your access token:</p>
|
<p className="text-sm text-zinc-400">Your access token:</p>
|
||||||
<code className="rounded bg-zinc-900 px-2 py-1 font-mono text-sm text-white">
|
<code className="rounded bg-zinc-900 px-2 py-1 font-mono text-sm text-white">
|
||||||
{userToken}
|
{authData.token}
|
||||||
</code>
|
</code>
|
||||||
</div>
|
</div>
|
||||||
<TooltipProvider>
|
<TooltipProvider>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
import { ReactNode, useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
import type { ReactNode } from 'react';
|
||||||
|
|
||||||
export function Providers({ children }: { children: ReactNode }) {
|
export function Providers({ children }: { children: ReactNode }) {
|
||||||
const [queryClient] = useState(
|
const [queryClient] = useState(
|
||||||
|
|||||||
Reference in New Issue
Block a user