Files
supabase/apps/studio/data/auth/users-count-query.ts
Joshen LimandAlaister Young 536dc37d58 Chore/user management v2 panel part 2 (#29515)
* Set up banning and unbanning user, although untested due to API CORs issue

* Update search filter UI for users management V2

* Update API types

* Minoir

* Small fix

* Update UI

* Add support for resizing and re-ordering columns

* Add google profile image url to csp

* Revert test button

* Implement toggling of columns

* Fix loading

* Fully implement banning/unbanning user

* Fix

* Update apps/studio/components/interfaces/Auth/Users/UserOverview.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

* Fallback non CSP supported avatar urls to user icon

* Fix some bugs

* Remove prism-react-renderer from studio, add to ui patterns

* Migrate users query from API to studio

* Address some feedback

---------

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-10-01 14:32:46 +08:00

70 lines
2.1 KiB
TypeScript

import { UseQueryOptions } from '@tanstack/react-query'
import { ExecuteSqlData, ExecuteSqlError, useExecuteSqlQuery } from 'data/sql/execute-sql-query'
import { Filter } from './users-infinite-query'
import { authKeys } from './keys'
type UsersCountVariables = {
projectRef?: string
connectionString?: string
keywords?: string
filter?: Filter
providers?: string[]
}
const getUsersCountSQl = ({
verified,
keywords,
providers,
}: {
verified?: Filter
keywords?: string
providers?: string[]
}) => {
const hasValidKeywords = keywords && keywords !== ''
const conditions: string[] = []
const baseQueryCount = `select count(*) from auth.users`
if (hasValidKeywords) {
conditions.push(
`id::text ilike '%${keywords}%' or email ilike '%${keywords}%' or phone ilike '%${keywords}%`
)
}
if (verified === 'verified') {
conditions.push(`email_confirmed_at IS NOT NULL or phone_confirmed_at IS NOT NULL`)
} else if (verified === 'anonymous') {
conditions.push(`is_anonymous is true`)
} else if (verified === 'unverified') {
conditions.push(`email_confirmed_at IS NULL AND phone_confirmed_at IS NULL`)
}
if (providers && providers.length > 0) {
conditions.push(
`(raw_app_meta_data->>'providers')::jsonb ?| array[${providers.map((p) => `'${p}'`).join(', ')}]`
)
}
const combinedConditions = conditions.map((x) => `(${x})`).join(' and ')
return `${baseQueryCount}${conditions.length > 0 ? ` where ${combinedConditions}` : ''};`
}
export type UsersCountData = { result: [{ count: number }] }
export type UsersCountError = ExecuteSqlError
export const useUsersCountQuery = <TData extends UsersCountData = UsersCountData>(
{ projectRef, connectionString, keywords, filter, providers }: UsersCountVariables,
options: UseQueryOptions<ExecuteSqlData, UsersCountError, TData> = {}
) =>
useExecuteSqlQuery(
{
projectRef,
connectionString,
sql: getUsersCountSQl({ keywords, verified: filter, providers }),
queryKey: authKeys.usersCount(projectRef, { keywords, filter, providers }),
},
options
)