Files
breakpilot-compliance/admin-compliance/lib/sdk/context-projects.ts
Sharang Parnerkar 786bb409e4 refactor(admin): split lib/sdk/context.tsx (1280 LOC) into focused modules
Extract the monolithic SDK context provider into seven focused modules:
- context-types.ts (203 LOC): SDKContextValue interface, initialState, ExtendedSDKAction
- context-reducer.ts (353 LOC): sdkReducer with all action handlers
- context-provider.tsx (495 LOC): SDKProvider component + SDKContext
- context-hooks.ts (17 LOC): useSDK hook
- context-validators.ts (94 LOC): local checkpoint validation logic
- context-projects.ts (67 LOC): project management API helpers
- context-sync-helpers.ts (145 LOC): sync infrastructure init/cleanup/callbacks
- context.tsx (23 LOC): barrel re-export preserving existing import paths

All files under the 500-line hard cap. Build verified with `npx next build`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 13:55:42 +02:00

68 lines
1.8 KiB
TypeScript

import React from 'react'
import { SDKApiClient, getSDKApiClient } from './api-client'
import { CustomerType, ProjectInfo } from './types'
// =============================================================================
// PROJECT MANAGEMENT HELPERS
// =============================================================================
/**
* Ensures an API client is available. If the ref is null and backend sync is
* enabled, lazily initialises one. Returns the client or throws.
*/
export function ensureApiClient(
apiClientRef: React.MutableRefObject<SDKApiClient | null>,
enableBackendSync: boolean,
tenantId: string,
projectId?: string
): SDKApiClient {
if (!apiClientRef.current && enableBackendSync) {
apiClientRef.current = getSDKApiClient(tenantId, projectId)
}
if (!apiClientRef.current) {
throw new Error('Backend sync not enabled')
}
return apiClientRef.current
}
export async function createProjectApi(
apiClient: SDKApiClient,
name: string,
customerType: CustomerType,
copyFromProjectId?: string
): Promise<ProjectInfo> {
return apiClient.createProject({
name,
customer_type: customerType,
copy_from_project_id: copyFromProjectId,
})
}
export async function listProjectsApi(
apiClient: SDKApiClient
): Promise<ProjectInfo[]> {
const result = await apiClient.listProjects()
return result.projects
}
export async function archiveProjectApi(
apiClient: SDKApiClient,
archiveId: string
): Promise<void> {
await apiClient.archiveProject(archiveId)
}
export async function restoreProjectApi(
apiClient: SDKApiClient,
restoreId: string
): Promise<ProjectInfo> {
return apiClient.restoreProject(restoreId)
}
export async function permanentlyDeleteProjectApi(
apiClient: SDKApiClient,
deleteId: string
): Promise<void> {
await apiClient.permanentlyDeleteProject(deleteId)
}