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
|
# Docker example: http://gantt-board:3000/api
|
||||||
GANTT_API_BASE_URL=http://localhost:3000/api
|
GANTT_API_BASE_URL=http://localhost:3000/api
|
||||||
GANTT_API_BEARER_TOKEN=replace_with_same_value_as_gantt_machine_token
|
GANTT_API_BEARER_TOKEN=replace_with_same_value_as_gantt_machine_token
|
||||||
|
GANTT_API_REVALIDATE_SECONDS=15
|
||||||
|
|
||||||
# Optional link targets for UI
|
# Optional link targets for UI
|
||||||
NEXT_PUBLIC_GANTT_BOARD_URL=http://localhost:3000
|
NEXT_PUBLIC_GANTT_BOARD_URL=http://localhost:3000
|
||||||
|
|||||||
@ -138,6 +138,7 @@ NEXT_PUBLIC_SUPABASE_URL=
|
|||||||
NEXT_PUBLIC_SUPABASE_ANON_KEY=
|
NEXT_PUBLIC_SUPABASE_ANON_KEY=
|
||||||
SUPABASE_SERVICE_ROLE_KEY=
|
SUPABASE_SERVICE_ROLE_KEY=
|
||||||
GANTT_API_BASE_URL= # ex: http://gantt-board:3000/api (Docker network)
|
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
|
# Optional
|
||||||
NEXT_PUBLIC_GOOGLE_CLIENT_ID= # For Calendar integration
|
NEXT_PUBLIC_GOOGLE_CLIENT_ID= # For Calendar integration
|
||||||
|
|||||||
@ -1,4 +1,13 @@
|
|||||||
const DEFAULT_GANTT_API_BASE_URL = "http://localhost:3000/api";
|
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 {
|
function normalizeBaseUrl(url: string): string {
|
||||||
return url.replace(/\/+$/, "");
|
return url.replace(/\/+$/, "");
|
||||||
@ -12,6 +21,11 @@ export function getGanttApiBaseUrl(): string {
|
|||||||
return normalizeBaseUrl(configured);
|
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 {
|
function buildAuthHeaders(): HeadersInit {
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
|
|
||||||
@ -28,23 +42,55 @@ function buildAuthHeaders(): HeadersInit {
|
|||||||
|
|
||||||
export async function fetchGanttApi<T>(endpoint: string): Promise<T> {
|
export async function fetchGanttApi<T>(endpoint: string): Promise<T> {
|
||||||
const baseUrl = getGanttApiBaseUrl();
|
const baseUrl = getGanttApiBaseUrl();
|
||||||
const response = await fetch(`${baseUrl}${endpoint}`, {
|
const revalidateSeconds = getRevalidateSeconds();
|
||||||
method: "GET",
|
const cacheKey = `${baseUrl}${endpoint}`;
|
||||||
cache: "no-store",
|
const now = Date.now();
|
||||||
headers: {
|
const cached = responseCache.get(cacheKey);
|
||||||
Accept: "application/json",
|
|
||||||
...buildAuthHeaders(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const payload = (await response.json().catch(() => null)) as
|
if (cached && cached.expiresAt > now) {
|
||||||
| { error?: string; message?: string }
|
if (cached.promise) {
|
||||||
| null;
|
return (await cached.promise) as T;
|
||||||
|
}
|
||||||
if (!response.ok) {
|
return cached.value as T;
|
||||||
const details = payload?.error || payload?.message || response.statusText;
|
|
||||||
throw new Error(`gantt-board API request failed (${response.status} ${endpoint}): ${details}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return payload as T;
|
const requestPromise = (async () => {
|
||||||
|
const response = await fetch(cacheKey, {
|
||||||
|
method: "GET",
|
||||||
|
cache: "no-store",
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
...buildAuthHeaders(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = (await response.json().catch(() => null)) as
|
||||||
|
| { error?: string; message?: string }
|
||||||
|
| null;
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const details = payload?.error || payload?.message || response.statusText;
|
||||||
|
throw new Error(`gantt-board API request failed (${response.status} ${endpoint}): ${details}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 { Task, fetchAllTasks } from "./tasks";
|
||||||
|
import { fetchGanttSnapshot } from "@/lib/data/gantt-snapshot";
|
||||||
|
|
||||||
export interface Project {
|
export interface Project {
|
||||||
id: string;
|
id: string;
|
||||||
@ -30,74 +30,9 @@ export interface ProjectStats {
|
|||||||
recentTasks: Task[];
|
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[]> {
|
export async function fetchAllProjects(): Promise<Project[]> {
|
||||||
try {
|
const snapshot = await fetchGanttSnapshot();
|
||||||
const response = await fetchGanttApi<GanttProjectsResponse>("/projects");
|
return snapshot.projects;
|
||||||
return Array.isArray(response.projects) ? response.projects.map(mapProject) : [];
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching projects from gantt-board API:", error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function countProjects(): Promise<number> {
|
export async function countProjects(): Promise<number> {
|
||||||
@ -106,13 +41,8 @@ export async function countProjects(): Promise<number> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchAllSprints(): Promise<Sprint[]> {
|
export async function fetchAllSprints(): Promise<Sprint[]> {
|
||||||
try {
|
const snapshot = await fetchGanttSnapshot();
|
||||||
const response = await fetchGanttApi<GanttSprintsResponse>("/sprints");
|
return snapshot.sprints;
|
||||||
return Array.isArray(response.sprints) ? response.sprints.map(mapSprint) : [];
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching sprints from gantt-board API:", error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchProjectSprints(projectId: string): Promise<Sprint[]> {
|
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> {
|
export async function fetchActiveSprint(): Promise<Sprint | null> {
|
||||||
try {
|
const sprints = await fetchAllSprints();
|
||||||
const response = await fetchGanttApi<GanttCurrentSprintResponse>("/sprints/current");
|
const active = sprints.find((sprint) => sprint.status === "active");
|
||||||
if (!response.sprint) return null;
|
return active || 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function countSprintsByStatus(): Promise<{ planning: number; active: number; completed: number; total: number }> {
|
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 {
|
export interface Task {
|
||||||
id: string;
|
id: string;
|
||||||
@ -27,10 +27,6 @@ export interface Task {
|
|||||||
attachments: unknown[];
|
attachments: unknown[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GanttTasksResponse {
|
|
||||||
tasks?: Task[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TaskStatusCounts {
|
export interface TaskStatusCounts {
|
||||||
open: number;
|
open: number;
|
||||||
inProgress: number;
|
inProgress: number;
|
||||||
@ -60,13 +56,8 @@ function toDateOnly(value: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function fetchTasksFromApi(): Promise<Task[]> {
|
async function fetchTasksFromApi(): Promise<Task[]> {
|
||||||
try {
|
const snapshot = await fetchGanttSnapshot();
|
||||||
const response = await fetchGanttApi<GanttTasksResponse>("/tasks?scope=all&include=detail");
|
return snapshot.tasks;
|
||||||
return Array.isArray(response.tasks) ? response.tasks : [];
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching tasks from gantt-board API:", error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchAllTasks(): Promise<Task[]> {
|
export async function fetchAllTasks(): Promise<Task[]> {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user