Files
sim/apps/sim/hooks/queries/api-keys.ts
Waleed ace87791d8 feat(analytics): add PostHog product analytics (#3910)
* feat(analytics): add PostHog product analytics

* fix(posthog): fix workspace group via URL params, type errors, and clean up comments

* fix(posthog): address PR review - fix pre-tx event, auth_method, paused executions, enterprise cancellation, settings double-fire

* chore(posthog): remove unused identifyServerPerson

* fix(posthog): isolate processQueuedResumes errors, simplify settings posthog deps

* fix(posthog): correctly classify SSO auth_method, fix phantom empty-string workspace groups

* fix(posthog): remove usePostHog from memo'd TemplateCard, fix copilot chat phantom workspace group

* fix(posthog): eliminate all remaining phantom empty-string workspace groups

* fix(posthog): fix cancel route phantom group, remove redundant workspaceId shadow in catch block

* fix(posthog): use ids.length for block_removed guard to handle container blocks with descendants

* chore(posthog): remove unused removedBlockTypes variable

* fix(posthog): remove phantom $set person properties from subscription events

* fix(posthog): add passedKnowledgeBaseName to knowledge_base_opened effect deps

* fix(posthog): capture currentWorkflowId synchronously before async import to avoid stale closure

* fix(posthog): add typed captureEvent wrapper for React components, deduplicate copilot_panel_opened

* feat(posthog): add task_created and task_message_sent events, remove copilot_panel_opened

* feat(posthog): track task_renamed, task_deleted, task_marked_read, task_marked_unread

* feat(analytics): expand posthog event coverage with source tracking and lifecycle events

* fix(analytics): flush posthog events on SIGTERM before ECS task termination

* fix(analytics): fix posthog in useCallback deps and fire block events for bulk operations
2026-04-03 01:00:35 -07:00

212 lines
5.6 KiB
TypeScript

import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { workspaceKeys } from '@/hooks/queries/workspace'
/**
* Query key factories for API keys-related queries
*/
export const apiKeysKeys = {
all: ['apiKeys'] as const,
workspaces: () => [...apiKeysKeys.all, 'workspace'] as const,
workspace: (workspaceId: string) => [...apiKeysKeys.workspaces(), workspaceId] as const,
personal: () => [...apiKeysKeys.all, 'personal'] as const,
combineds: () => [...apiKeysKeys.all, 'combined'] as const,
combined: (workspaceId: string) => [...apiKeysKeys.combineds(), workspaceId] as const,
}
/**
* API Key type definition
*/
export interface ApiKey {
id: string
name: string
key: string
displayKey?: string
lastUsed?: string
createdAt: string
expiresAt?: string
createdBy?: string
}
/**
* Combined API keys response
*/
interface ApiKeysResponse {
workspaceKeys: ApiKey[]
personalKeys: ApiKey[]
conflicts: string[]
}
/**
* Fetch both workspace and personal API keys
*/
async function fetchApiKeys(workspaceId: string, signal?: AbortSignal): Promise<ApiKeysResponse> {
const [workspaceResponse, personalResponse] = await Promise.all([
fetch(`/api/workspaces/${workspaceId}/api-keys`, { signal }),
fetch('/api/users/me/api-keys', { signal }),
])
if (!workspaceResponse.ok) {
throw new Error(`Failed to fetch workspace API keys: ${workspaceResponse.status}`)
}
if (!personalResponse.ok) {
throw new Error(`Failed to fetch personal API keys: ${personalResponse.status}`)
}
const workspaceData = await workspaceResponse.json()
const personalData = await personalResponse.json()
const workspaceKeys: ApiKey[] = workspaceData.keys || []
const personalKeys: ApiKey[] = personalData.keys || []
const workspaceKeyNames = new Set(workspaceKeys.map((k) => k.name))
const conflicts = personalKeys
.filter((key) => workspaceKeyNames.has(key.name))
.map((key) => key.name)
return {
workspaceKeys,
personalKeys,
conflicts,
}
}
/**
* Hook to fetch API keys (both workspace and personal)
*/
export function useApiKeys(workspaceId: string) {
return useQuery({
queryKey: apiKeysKeys.combined(workspaceId),
queryFn: ({ signal }) => fetchApiKeys(workspaceId, signal),
enabled: !!workspaceId,
staleTime: 60 * 1000,
placeholderData: keepPreviousData,
})
}
/**
* Create API key mutation params
*/
interface CreateApiKeyParams {
workspaceId: string
name: string
keyType: 'personal' | 'workspace'
source?: 'settings' | 'deploy_modal'
}
/**
* Hook to create a new API key
*/
export function useCreateApiKey() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ workspaceId, name, keyType, source }: CreateApiKeyParams) => {
const url =
keyType === 'workspace'
? `/api/workspaces/${workspaceId}/api-keys`
: '/api/users/me/api-keys'
const body: Record<string, unknown> = { name: name.trim() }
if (keyType === 'workspace' && source) body.source = source
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Failed to create API key' }))
throw new Error(error.error || 'Failed to create API key')
}
return response.json()
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({
queryKey: apiKeysKeys.combined(variables.workspaceId),
})
},
})
}
/**
* Delete API key mutation params
*/
interface DeleteApiKeyParams {
workspaceId: string
keyId: string
keyType: 'personal' | 'workspace'
}
/**
* Hook to delete an API key
*/
export function useDeleteApiKey() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({ workspaceId, keyId, keyType }: DeleteApiKeyParams) => {
const url =
keyType === 'workspace'
? `/api/workspaces/${workspaceId}/api-keys/${keyId}`
: `/api/users/me/api-keys/${keyId}`
const response = await fetch(url, {
method: 'DELETE',
})
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Failed to delete API key' }))
throw new Error(error.error || 'Failed to delete API key')
}
return response.json()
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({
queryKey: apiKeysKeys.combined(variables.workspaceId),
})
},
})
}
/**
* Update workspace API key settings mutation params
*/
interface UpdateWorkspaceApiKeySettingsParams {
workspaceId: string
allowPersonalApiKeys: boolean
}
/**
* Hook to update workspace API key settings
*/
export function useUpdateWorkspaceApiKeySettings() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({
workspaceId,
allowPersonalApiKeys,
}: UpdateWorkspaceApiKeySettingsParams) => {
const response = await fetch(`/api/workspaces/${workspaceId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ allowPersonalApiKeys }),
})
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Failed to update settings' }))
throw new Error(error.error || 'Failed to update workspace settings')
}
return response.json()
},
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({
queryKey: workspaceKeys.settings(variables.workspaceId),
})
},
})
}