Files
supabase/apps/studio/components/interfaces/Storage/useBucketPolicyCount.ts
Joshen Lim 4901f081e5 Migrate remaining requests to pg-meta API to use query endpoint (#47758)
## Context

Migrates the remaining API requests to the pg-meta endpoint to use the
query endpoint directly with the SQL from the pg-meta package. This
touches the following:
- policies
- publications
- triggers
- views
- materialized views
- types

## To test
Just need to verify that we're still fetching the data correctly on
these pages
- Database policies
- Database publications
- Database triggers
- Database tables (views + materialized views)
- Database types

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved and stabilized loading of database metadata (views, triggers,
RLS policies, publications, materialized views, and enum types),
including more reliable schema-scoped filtering.
* Updated policy loading behavior and related UI queries to consistently
use schema arrays, improving cache correctness and consistency.
* **Tests**
* Updated end-to-end test synchronization to wait for the correct
metadata responses using more specific request identifiers.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-09 17:09:03 +08:00

36 lines
1.3 KiB
TypeScript

import { useCallback, useMemo } from 'react'
import { extractBucketNameFromDefinition } from '@/components/interfaces/Storage/Storage.utils'
import { useDatabasePoliciesQuery } from '@/data/database-policies/database-policies-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
export function useBucketPolicyCount() {
const { data: project, isPending: isProjectPending } = useSelectedProjectQuery()
const { data: policiesData = [], isPending: isPoliciesPending } = useDatabasePoliciesQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
schemas: ['storage'],
})
const policyCountByBucket = useMemo(() => {
const countMap = new Map<string, number>()
for (const policy of policiesData) {
if (policy.table !== 'objects') continue
const bucketName =
extractBucketNameFromDefinition(policy.definition) ??
extractBucketNameFromDefinition(policy.check)
if (bucketName) {
countMap.set(bucketName, (countMap.get(bucketName) ?? 0) + 1)
}
}
return countMap
}, [policiesData])
const getPolicyCount = useCallback(
(bucketName: string) => policyCountByBucket.get(bucketName) ?? 0,
[policyCountByBucket]
)
return { getPolicyCount, isLoading: isProjectPending || isPoliciesPending }
}