mirror of
https://github.com/langgenius/dify.git
synced 2026-02-16 16:00:57 -05:00
94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
import type { SQLiteQueryResult } from '../../hooks/sqlite/types'
|
|
import { useEffect, useReducer } from 'react'
|
|
import { PREVIEW_ROW_LIMIT } from './constants'
|
|
|
|
type TableState = {
|
|
data: SQLiteQueryResult | null
|
|
isLoading: boolean
|
|
error: Error | null
|
|
}
|
|
|
|
type TableAction
|
|
= | { type: 'reset' }
|
|
| { type: 'loading' }
|
|
| { type: 'success', data: SQLiteQueryResult | null }
|
|
| { type: 'error', error: Error }
|
|
|
|
type UseSQLiteTableArgs = {
|
|
selectedTable: string
|
|
queryTable: (tableName: string, limit?: number) => Promise<SQLiteQueryResult | null>
|
|
}
|
|
|
|
const initialTableState: TableState = {
|
|
data: null,
|
|
isLoading: false,
|
|
error: null,
|
|
}
|
|
|
|
export const useSQLiteTable = ({
|
|
selectedTable,
|
|
queryTable,
|
|
}: UseSQLiteTableArgs): TableState => {
|
|
const [tableState, dispatch] = useReducer((current: TableState, action: TableAction): TableState => {
|
|
switch (action.type) {
|
|
case 'reset':
|
|
return {
|
|
data: null,
|
|
isLoading: false,
|
|
error: null,
|
|
}
|
|
case 'loading':
|
|
return {
|
|
data: null,
|
|
isLoading: true,
|
|
error: null,
|
|
}
|
|
case 'success':
|
|
return {
|
|
data: action.data,
|
|
isLoading: false,
|
|
error: null,
|
|
}
|
|
case 'error':
|
|
return {
|
|
data: null,
|
|
isLoading: false,
|
|
error: action.error,
|
|
}
|
|
default:
|
|
return current
|
|
}
|
|
}, initialTableState)
|
|
|
|
useEffect(() => {
|
|
if (!selectedTable) {
|
|
dispatch({ type: 'reset' })
|
|
return
|
|
}
|
|
|
|
let cancelled = false
|
|
|
|
const loadTable = async () => {
|
|
dispatch({ type: 'loading' })
|
|
|
|
try {
|
|
const data = await queryTable(selectedTable, PREVIEW_ROW_LIMIT)
|
|
if (!cancelled)
|
|
dispatch({ type: 'success', data })
|
|
}
|
|
catch (err) {
|
|
if (!cancelled)
|
|
dispatch({ type: 'error', error: err instanceof Error ? err : new Error(String(err)) })
|
|
}
|
|
}
|
|
|
|
loadTable()
|
|
|
|
return () => {
|
|
cancelled = true
|
|
}
|
|
}, [queryTable, selectedTable])
|
|
|
|
return tableState
|
|
}
|