All files / apps/web/src/hooks useOrgConfig.js

0% Statements 0/17
0% Branches 0/12
0% Functions 0/6
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                                                   
import { useQuery } from '@tanstack/react-query'
import api from '../services/api'
import { useOrganizationStore } from '../store/organizationStore'
 
function getOrgIdFromPath() {
  const parts = window.location.pathname.split('/').filter(Boolean)
  return parts[0] && /^[0-9a-f]{24}$/.test(parts[0]) ? parts[0] : null
}
 
export function useOrgConfig() {
  const storeOrgId = useOrganizationStore(s => s.currentOrganization?.id)
  const organizationId = storeOrgId || getOrgIdFromPath()
 
  const { data: config, isLoading } = useQuery({
    queryKey: ['system-config', organizationId],
    queryFn: async () => {
      const response = await api.get('/config/resolve')
      return response.data.data
    },
    enabled: !!organizationId
  })
 
  const isKeyConfigured = (key) => {
    if (!config) return false
    if (Array.isArray(config)) {
      const item = config.find(c => c.key === key)
      return !!(item?.value && item.value.length > 0)
    }
    return false
  }
 
  return { config, isLoading, isKeyConfigured, organizationId }
}