Signed-off-by: OpenClaw Bot <ai-agent@topdoglabs.com>
This commit is contained in:
parent
9ce8deb678
commit
64bcbb1b97
@ -10,6 +10,7 @@ SUPABASE_SERVICE_ROLE_KEY=your-service-role-secret-key-here
|
||||
# Docker example: http://gantt-board:3000/api
|
||||
GANTT_API_BASE_URL=http://localhost:3000/api
|
||||
GANTT_API_BEARER_TOKEN=replace_with_same_value_as_gantt_machine_token
|
||||
GANTT_API_REVALIDATE_SECONDS=15
|
||||
|
||||
# Optional link targets for UI
|
||||
NEXT_PUBLIC_GANTT_BOARD_URL=http://localhost:3000
|
||||
|
||||
@ -138,6 +138,7 @@ NEXT_PUBLIC_SUPABASE_URL=
|
||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=
|
||||
SUPABASE_SERVICE_ROLE_KEY=
|
||||
GANTT_API_BASE_URL= # ex: http://gantt-board:3000/api (Docker network)
|
||||
GANTT_API_REVALIDATE_SECONDS=15 # Optional cache window for gantt API reads
|
||||
|
||||
# Optional
|
||||
NEXT_PUBLIC_GOOGLE_CLIENT_ID= # For Calendar integration
|
||||
|
||||
@ -1,4 +1,13 @@
|
||||
const DEFAULT_GANTT_API_BASE_URL = "http://localhost:3000/api";
|
||||
const DEFAULT_REVALIDATE_SECONDS = 15;
|
||||
|
||||
type CacheEntry = {
|
||||
expiresAt: number;
|
||||
value: unknown;
|
||||
promise?: Promise<unknown>;
|
||||
};
|
||||
|
||||
const responseCache = new Map<string, CacheEntry>();
|
||||
|
||||
function normalizeBaseUrl(url: string): string {
|
||||
return url.replace(/\/+$/, "");
|
||||
@ -12,6 +21,11 @@ export function getGanttApiBaseUrl(): string {
|
||||
return normalizeBaseUrl(configured);
|
||||
}
|
||||
|
||||
function getRevalidateSeconds(): number {
|
||||
const raw = Number(process.env.GANTT_API_REVALIDATE_SECONDS || String(DEFAULT_REVALIDATE_SECONDS));
|
||||
return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_REVALIDATE_SECONDS;
|
||||
}
|
||||
|
||||
function buildAuthHeaders(): HeadersInit {
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
@ -28,7 +42,20 @@ function buildAuthHeaders(): HeadersInit {
|
||||
|
||||
export async function fetchGanttApi<T>(endpoint: string): Promise<T> {
|
||||
const baseUrl = getGanttApiBaseUrl();
|
||||
const response = await fetch(`${baseUrl}${endpoint}`, {
|
||||
const revalidateSeconds = getRevalidateSeconds();
|
||||
const cacheKey = `${baseUrl}${endpoint}`;
|
||||
const now = Date.now();
|
||||
const cached = responseCache.get(cacheKey);
|
||||
|
||||
if (cached && cached.expiresAt > now) {
|
||||
if (cached.promise) {
|
||||
return (await cached.promise) as T;
|
||||
}
|
||||
return cached.value as T;
|
||||
}
|
||||
|
||||
const requestPromise = (async () => {
|
||||
const response = await fetch(cacheKey, {
|
||||
method: "GET",
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
@ -47,4 +74,23 @@ export async function fetchGanttApi<T>(endpoint: string): Promise<T> {
|
||||
}
|
||||
|
||||
return payload as T;
|
||||
})();
|
||||
|
||||
responseCache.set(cacheKey, {
|
||||
expiresAt: now + revalidateSeconds * 1000,
|
||||
value: null,
|
||||
promise: requestPromise as Promise<unknown>,
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await requestPromise;
|
||||
responseCache.set(cacheKey, {
|
||||
expiresAt: Date.now() + revalidateSeconds * 1000,
|
||||
value: result as unknown,
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
responseCache.delete(cacheKey);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
30
lib/data/gantt-snapshot.ts
Normal file
30
lib/data/gantt-snapshot.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { cache } from "react";
|
||||
import { fetchGanttApi } from "@/lib/data/gantt-api";
|
||||
import type { Task } from "@/lib/data/tasks";
|
||||
import type { Project, Sprint } from "@/lib/data/projects";
|
||||
|
||||
interface SnapshotResponse {
|
||||
tasks?: Task[];
|
||||
projects?: Project[];
|
||||
sprints?: Sprint[];
|
||||
}
|
||||
|
||||
export interface GanttSnapshot {
|
||||
tasks: Task[];
|
||||
projects: Project[];
|
||||
sprints: Sprint[];
|
||||
}
|
||||
|
||||
export const fetchGanttSnapshot = cache(async (): Promise<GanttSnapshot> => {
|
||||
try {
|
||||
const response = await fetchGanttApi<SnapshotResponse>("/tasks?scope=all");
|
||||
return {
|
||||
tasks: Array.isArray(response.tasks) ? response.tasks : [],
|
||||
projects: Array.isArray(response.projects) ? response.projects : [],
|
||||
sprints: Array.isArray(response.sprints) ? response.sprints : [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching gantt snapshot from API:", error);
|
||||
return { tasks: [], projects: [], sprints: [] };
|
||||
}
|
||||
});
|
||||
@ -1,5 +1,5 @@
|
||||
import { fetchGanttApi } from "@/lib/data/gantt-api";
|
||||
import { Task, fetchAllTasks } from "./tasks";
|
||||
import { fetchGanttSnapshot } from "@/lib/data/gantt-snapshot";
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
@ -30,74 +30,9 @@ export interface ProjectStats {
|
||||
recentTasks: Task[];
|
||||
}
|
||||
|
||||
interface GanttProjectsResponse {
|
||||
projects?: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string | null;
|
||||
color?: string | null;
|
||||
created_at?: string;
|
||||
createdAt?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface GanttSprintsResponse {
|
||||
sprints?: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
status: Sprint["status"];
|
||||
start_date?: string;
|
||||
end_date?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
project_id?: string;
|
||||
projectId?: string;
|
||||
goal?: string | null;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface GanttCurrentSprintResponse {
|
||||
sprint?: {
|
||||
id: string;
|
||||
name: string;
|
||||
status: Sprint["status"];
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
projectId: string;
|
||||
goal?: string | null;
|
||||
} | null;
|
||||
}
|
||||
|
||||
function mapProject(project: NonNullable<GanttProjectsResponse["projects"]>[number]): Project {
|
||||
return {
|
||||
id: project.id,
|
||||
name: project.name,
|
||||
description: project.description ?? undefined,
|
||||
color: project.color || "#3b82f6",
|
||||
createdAt: project.createdAt || project.created_at || new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
function mapSprint(sprint: NonNullable<GanttSprintsResponse["sprints"]>[number]): Sprint {
|
||||
return {
|
||||
id: sprint.id,
|
||||
name: sprint.name,
|
||||
status: sprint.status,
|
||||
startDate: sprint.startDate || sprint.start_date || new Date().toISOString(),
|
||||
endDate: sprint.endDate || sprint.end_date || new Date().toISOString(),
|
||||
projectId: sprint.projectId || sprint.project_id || "",
|
||||
goal: sprint.goal ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchAllProjects(): Promise<Project[]> {
|
||||
try {
|
||||
const response = await fetchGanttApi<GanttProjectsResponse>("/projects");
|
||||
return Array.isArray(response.projects) ? response.projects.map(mapProject) : [];
|
||||
} catch (error) {
|
||||
console.error("Error fetching projects from gantt-board API:", error);
|
||||
return [];
|
||||
}
|
||||
const snapshot = await fetchGanttSnapshot();
|
||||
return snapshot.projects;
|
||||
}
|
||||
|
||||
export async function countProjects(): Promise<number> {
|
||||
@ -106,13 +41,8 @@ export async function countProjects(): Promise<number> {
|
||||
}
|
||||
|
||||
export async function fetchAllSprints(): Promise<Sprint[]> {
|
||||
try {
|
||||
const response = await fetchGanttApi<GanttSprintsResponse>("/sprints");
|
||||
return Array.isArray(response.sprints) ? response.sprints.map(mapSprint) : [];
|
||||
} catch (error) {
|
||||
console.error("Error fetching sprints from gantt-board API:", error);
|
||||
return [];
|
||||
}
|
||||
const snapshot = await fetchGanttSnapshot();
|
||||
return snapshot.sprints;
|
||||
}
|
||||
|
||||
export async function fetchProjectSprints(projectId: string): Promise<Sprint[]> {
|
||||
@ -121,22 +51,9 @@ export async function fetchProjectSprints(projectId: string): Promise<Sprint[]>
|
||||
}
|
||||
|
||||
export async function fetchActiveSprint(): Promise<Sprint | null> {
|
||||
try {
|
||||
const response = await fetchGanttApi<GanttCurrentSprintResponse>("/sprints/current");
|
||||
if (!response.sprint) return null;
|
||||
return {
|
||||
id: response.sprint.id,
|
||||
name: response.sprint.name,
|
||||
status: response.sprint.status,
|
||||
startDate: response.sprint.startDate,
|
||||
endDate: response.sprint.endDate,
|
||||
projectId: response.sprint.projectId,
|
||||
goal: response.sprint.goal ?? undefined,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching current sprint from gantt-board API:", error);
|
||||
return null;
|
||||
}
|
||||
const sprints = await fetchAllSprints();
|
||||
const active = sprints.find((sprint) => sprint.status === "active");
|
||||
return active || null;
|
||||
}
|
||||
|
||||
export async function countSprintsByStatus(): Promise<{ planning: number; active: number; completed: number; total: number }> {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { fetchGanttApi } from "@/lib/data/gantt-api";
|
||||
import { fetchGanttSnapshot } from "@/lib/data/gantt-snapshot";
|
||||
|
||||
export interface Task {
|
||||
id: string;
|
||||
@ -27,10 +27,6 @@ export interface Task {
|
||||
attachments: unknown[];
|
||||
}
|
||||
|
||||
interface GanttTasksResponse {
|
||||
tasks?: Task[];
|
||||
}
|
||||
|
||||
export interface TaskStatusCounts {
|
||||
open: number;
|
||||
inProgress: number;
|
||||
@ -60,13 +56,8 @@ function toDateOnly(value: string): string {
|
||||
}
|
||||
|
||||
async function fetchTasksFromApi(): Promise<Task[]> {
|
||||
try {
|
||||
const response = await fetchGanttApi<GanttTasksResponse>("/tasks?scope=all&include=detail");
|
||||
return Array.isArray(response.tasks) ? response.tasks : [];
|
||||
} catch (error) {
|
||||
console.error("Error fetching tasks from gantt-board API:", error);
|
||||
return [];
|
||||
}
|
||||
const snapshot = await fetchGanttSnapshot();
|
||||
return snapshot.tasks;
|
||||
}
|
||||
|
||||
export async function fetchAllTasks(): Promise<Task[]> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user