Files
kandev/apps/web/hooks/domains/github/use-task-git-credentials.ts
T
2026-07-31 19:20:49 +01:00

59 lines
1.7 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import {
fetchGitHubWorkspaceSettings,
updateGitHubWorkspaceSettings,
} from "@/lib/api/domains/github-api";
import type { TaskGitCredentialsMode } from "@/lib/types/github";
export type TaskGitCredentialsState = {
mode: TaskGitCredentialsMode;
loading: boolean;
error: boolean;
save: (mode: TaskGitCredentialsMode) => Promise<boolean>;
};
export function useTaskGitCredentials(workspaceId: string): TaskGitCredentialsState {
const [mode, setMode] = useState<TaskGitCredentialsMode>("managed");
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const activeWorkspaceId = useRef(workspaceId);
activeWorkspaceId.current = workspaceId;
useEffect(() => {
let cancelled = false;
setLoading(true);
setError(false);
void fetchGitHubWorkspaceSettings(workspaceId)
.then((settings) => {
if (!cancelled) setMode(settings.task_git_credentials_mode ?? "managed");
})
.catch(() => {
if (!cancelled) setError(true);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [workspaceId]);
const save = useCallback(
async (nextMode: TaskGitCredentialsMode) => {
const updated = await updateGitHubWorkspaceSettings({
workspace_id: workspaceId,
task_git_credentials_mode: nextMode,
});
if (activeWorkspaceId.current !== workspaceId) return false;
setMode(updated.task_git_credentials_mode ?? "managed");
setError(false);
return true;
},
[workspaceId],
);
return { mode, loading, error, save };
}