feat: rebuild fiscal clone architecture and harden coolify deployment
This commit is contained in:
22
frontend/hooks/use-auth-guard.ts
Normal file
22
frontend/hooks/use-auth-guard.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useSession } from '@/lib/better-auth';
|
||||
|
||||
export function useAuthGuard() {
|
||||
const { data: session, isPending } = useSession();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isPending && !session) {
|
||||
router.replace('/auth/signin');
|
||||
}
|
||||
}, [isPending, session, router]);
|
||||
|
||||
return {
|
||||
session,
|
||||
isPending,
|
||||
isAuthenticated: Boolean(session?.user)
|
||||
};
|
||||
}
|
||||
59
frontend/hooks/use-task-poller.ts
Normal file
59
frontend/hooks/use-task-poller.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { getTask } from '@/lib/api';
|
||||
import type { Task } from '@/lib/types';
|
||||
|
||||
type UseTaskPollerInput = {
|
||||
taskId: string | null;
|
||||
intervalMs?: number;
|
||||
onTerminalState?: (task: Task) => void;
|
||||
};
|
||||
|
||||
export function useTaskPoller({ taskId, intervalMs = 2200, onTerminalState }: UseTaskPollerInput) {
|
||||
const [task, setTask] = useState<Task | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!taskId) {
|
||||
setTask(null);
|
||||
return;
|
||||
}
|
||||
|
||||
let stopped = false;
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const { task: latest } = await getTask(taskId);
|
||||
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
|
||||
setTask(latest);
|
||||
|
||||
if (latest.status === 'completed' || latest.status === 'failed') {
|
||||
onTerminalState?.(latest);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
timer = setTimeout(poll, intervalMs);
|
||||
};
|
||||
|
||||
void poll();
|
||||
|
||||
return () => {
|
||||
stopped = true;
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
}, [taskId, intervalMs, onTerminalState]);
|
||||
|
||||
return task;
|
||||
}
|
||||
Reference in New Issue
Block a user