43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { useInject } from '@/lib/di/hooks/useInject';
|
|
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
|
import { POLICY_SERVICE_TOKEN } from '@/lib/di/tokens';
|
|
import { ApiError } from '@/lib/gateways/api/base/ApiError';
|
|
import { PolicySnapshotDto } from '@/lib/gateways/api/policy/PolicyApiClient';
|
|
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
|
|
|
export function useCapability(
|
|
capabilityKey: string,
|
|
options?: Omit<UseQueryOptions<PolicySnapshotDto, ApiError>, 'queryKey' | 'queryFn'>
|
|
) {
|
|
const policyService = useInject(POLICY_SERVICE_TOKEN);
|
|
|
|
const queryResult = useQuery({
|
|
queryKey: ['policySnapshot', capabilityKey],
|
|
queryFn: async () => {
|
|
const result = await policyService.getSnapshot();
|
|
if (result.isErr()) {
|
|
const error = result.getError();
|
|
throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new Date().toISOString() });
|
|
}
|
|
return result.unwrap();
|
|
},
|
|
staleTime: 60_000,
|
|
gcTime: 5 * 60_000,
|
|
...options,
|
|
});
|
|
|
|
const enhancedResult = enhanceQueryResult(queryResult);
|
|
|
|
// Add helper to get capability state
|
|
const capabilityState = enhancedResult.data
|
|
? policyService.getCapabilityState(enhancedResult.data, capabilityKey)
|
|
: null;
|
|
|
|
return {
|
|
...enhancedResult,
|
|
capabilityState,
|
|
isCapabilityEnabled: capabilityState === 'enabled',
|
|
isCapabilityComingSoon: capabilityState === 'coming_soon',
|
|
};
|
|
}
|