Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix table editor infinite loading when sorting or filtering on view

Joshen Lim6ae440c13669
diff --git a/studio/components/grid/SupabaseGrid.tsx b/studio/components/grid/SupabaseGrid.tsx
index cc0f3a40cf4..75b3abf65b9 100644
--- a/studio/components/grid/SupabaseGrid.tsx
+++ b/studio/components/grid/SupabaseGrid.tsx
@@ -71,6 +71,10 @@ const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props
const sorts = formatSortURLParams(sort as string[])
const filters = formatFilterURLParams(filter as string[])
+ // [Joshen] This is not a perfect fix, but the useEffect to initialize the data in the editor
+ // was not getting retriggered for views, works fine for tables.
+ const table = typeof props.table === 'string' ? { name: props.table } : props.table
+
useImperativeHandle(ref, () => ({
rowAdded(row: Dictionary<any>) {
dispatch({
@@ -91,7 +95,7 @@ const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props
}, [])
useEffect(() => {
- if (state.refreshPageFlag == REFRESH_PAGE_IMMEDIATELY) {
+ if (state.refreshPageFlag === REFRESH_PAGE_IMMEDIATELY) {
fetchPage(state, dispatch, sorts, filters)
} else if (state.refreshPageFlag !== 0) {
refreshPageDebounced(state, dispatch, sorts, filters)
@@ -145,15 +149,15 @@ const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props
useEffect(() => {
if (!state.metaService) return
- if (
- !state.table ||
- (typeof props.table === 'string' &&
- state.table!.name !== props.table &&
- state.table!.schema !== props.schema) ||
- (typeof props.table !== 'string' &&
- JSON.stringify(props.table) !== JSON.stringify(state.table))
- ) {
- const { savedState } = initTable(props, state, dispatch, sort as string[], filter as string[])
+ const initializeData = async () => {
+ const { savedState } = await initTable(
+ props,
+ state,
+ dispatch,
+ sort as string[],
+ filter as string[]
+ )
+
if (savedState.sorts || savedState.filters) {
setParams((prevParams) => {
return {
@@ -164,7 +168,18 @@ const SupabaseGridLayout = forwardRef<SupabaseGridRef, SupabaseGridProps>((props
})
}
}
- }, [state.metaService, state.table, props.table, props.schema])
+
+ const refreshView =
+ typeof props.table === 'string' &&
+ state?.table?.name !== props.table &&
+ state?.table?.schema !== props.schema
+ const refreshTable =
+ typeof props.table !== 'string' && JSON.stringify(props.table) !== JSON.stringify(state.table)
+
+ if (!state.table || refreshView || refreshTable) {
+ initializeData()
+ }
+ }, [state.metaService, state.table, table, props.schema])
return (
<div className="sb-grid">
diff --git a/studio/components/grid/SupabaseGrid.utils.ts b/studio/components/grid/SupabaseGrid.utils.ts
index 60731c00291..66e6e6f0eea 100644
--- a/studio/components/grid/SupabaseGrid.utils.ts
+++ b/studio/components/grid/SupabaseGrid.utils.ts
@@ -67,13 +67,13 @@ export function formatFilterURLParams(filter?: string[]): Filter[] {
) as Filter[]
}
-export function initTable(
+export async function initTable(
props: SupabaseGridProps,
state: InitialStateType,
dispatch: (value: any) => void,
sort?: string[], // Come directly from URL param
filter?: string[] // Come directly from URL param
-): { savedState: { sorts?: string[]; filters?: string[] } } {
+): Promise<{ savedState: { sorts?: string[]; filters?: string[] } }> {
function onInitTable(table: SupaTable, props: SupabaseGridProps) {
const savedState = props.storageRef
? onLoadStorage(props.storageRef, table.name, table.schema)
@@ -118,19 +118,15 @@ export function initTable(
}
if (typeof props.table === 'string') {
- const fetchMethod = props.editable
- ? fetchEditableInfo(state.metaService!, props.table, props.schema)
- : fetchReadOnlyInfo(state.metaService!, props.table, props.schema)
+ const viewData = props.editable
+ ? await fetchEditableInfo(state.metaService!, props.table, props.schema)
+ : await fetchReadOnlyInfo(state.metaService!, props.table, props.schema)
- fetchMethod.then((res) => {
- if (res) {
- return onInitTable(res, props)
- } else {
- if (props.onError) {
- props.onError({ message: 'fetch table info failed' })
- }
- }
- })
+ if (viewData) {
+ return onInitTable(viewData, props)
+ } else {
+ if (props.onError) props.onError({ message: 'Failed to fetch data from view' })
+ }
} else {
return onInitTable(props.table, props)
}
diff --git a/studio/components/grid/utils/row.ts b/studio/components/grid/utils/row.ts
index d5173c1ef36..aabf2c85a78 100644
--- a/studio/components/grid/utils/row.ts
+++ b/studio/components/grid/utils/row.ts
@@ -26,7 +26,13 @@ export async function fetchPage(
sorts: Sort[],
filters: Filter[]
) {
- if (!state.rowService) return
+ if (!state.rowService) {
+ return dispatch({
+ type: 'SET_ROWS',
+ payload: { rows: state?.rows ?? [] },
+ })
+ }
+
dispatch({ type: 'SET_IS_LOADING', payload: { isLoading: true } })
const { data, error } = await state.rowService.fetchPage(