mirror of
https://github.com/supabase/supabase.git
synced 2026-07-06 21:44:22 +08:00
* very wip * add expandable rows * fix table layout, collapsible row, spacing issues * use new query with filters everywhere * rm unused queries * rm unused fn * improve loading state * fix text overflowing in role * rm padding so that table doesn't always need scroll * fix icon in search input * add latency to table row heading to clarify what col youre sorting with * rm unused imports * run prettier * align sql with row content * add syntax highlighting and sort icons * rm copy btn * move tailwind dep to correct package, rm unused syntax highlighting, rm unused component
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { DEFAULT_QUERY_PARAMS } from 'components/interfaces/Reports/Reports.constants'
|
|
import {
|
|
BaseReportParams,
|
|
MetaQueryResponse,
|
|
ReportQuery,
|
|
} from 'components/interfaces/Reports/Reports.types'
|
|
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
|
|
export interface DbQueryHook<T = any> {
|
|
isLoading: boolean
|
|
error: string
|
|
data: T[]
|
|
params: BaseReportParams
|
|
logData?: never
|
|
runQuery: () => void
|
|
setParams?: never
|
|
changeQuery?: never
|
|
resolvedSql: string
|
|
}
|
|
|
|
const useDbQuery = (
|
|
sql: ReportQuery['sql'] | string,
|
|
params: BaseReportParams = DEFAULT_QUERY_PARAMS,
|
|
where?: string,
|
|
orderBy?: string
|
|
): DbQueryHook => {
|
|
const { project } = useProjectContext()
|
|
|
|
const resolvedSql = typeof sql === 'function' ? sql([]) : sql
|
|
|
|
const {
|
|
data,
|
|
error: rqError,
|
|
isLoading,
|
|
isRefetching,
|
|
refetch,
|
|
} = useQuery(
|
|
['projects', project?.ref, 'db', { ...params, sql: resolvedSql }, where, orderBy],
|
|
({ signal }) => {
|
|
return executeSql(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
sql: resolvedSql,
|
|
},
|
|
signal
|
|
).then((res) => res.result) as Promise<MetaQueryResponse>
|
|
},
|
|
{
|
|
enabled: Boolean(resolvedSql),
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
}
|
|
)
|
|
|
|
const error = rqError || (typeof data === 'object' ? data?.error : '')
|
|
return {
|
|
error,
|
|
data,
|
|
isLoading: isLoading || isRefetching,
|
|
params,
|
|
runQuery: refetch,
|
|
resolvedSql,
|
|
}
|
|
}
|
|
|
|
export default useDbQuery
|