query-table.tsx
Overview
query-table.tsx is a React functional component file that implements a dynamic, interactive data table named QueryTable. This table is designed to display a list of query records (BeginQuery objects) with capabilities such as sorting, filtering, pagination, and column visibility toggling. It also includes row-level actions to edit or delete individual records.
The component leverages the powerful and flexible @tanstack/react-table library for table state management and rendering and integrates UI components such as buttons, tooltips, and table elements from a custom UI library (@/components/ui/*). Localization is supported via the react-i18next library.
Detailed Explanation
Interfaces
IProps
The QueryTable component expects props conforming to the IProps interface:
Property | Type | Description |
|---|---|---|
| Array of query records to display in the table. Defaults to an empty array if not provided. | |
| Callback function invoked when a user clicks the delete action on a row. Receives the row index. | |
| Callback function invoked when a user clicks the edit action on a row. Receives the row index and the full record. |
QueryTable Component
function QueryTable({ data = [], deleteRecord, showModal }: IProps)
Purpose
Renders a fully-featured table that displays query records with support for:
Sorting by columns.
Filtering data (state and UI hooks are set up, though no filter UI elements are shown in this snippet).
Pagination.
Column visibility toggling (state managed internally).
Actions per row: Edit and Delete.
Parameters
data: Array ofBeginQueryrecords to show.deleteRecord: Function to delete a record.showModal: Function to open a modal for editing a record.
Returns
A React element representing the interactive table UI.
Usage Example
<QueryTable
data={queries}
deleteRecord={(idx) => handleDelete(idx)}
showModal={(idx, record) => openEditModal(idx, record)}
/>
Internal State & Hooks
sorting(SortingState): Tracks column sorting state.columnFilters(ColumnFiltersState): Tracks active column filters.columnVisibility(VisibilityState): Tracks visibility of columns.useTranslation: Provides thetfunction for localized strings.
Columns Definition
The table columns are defined in a ColumnDef<BeginQuery>[] array:
Column Key | Header Text (localized) | Cell Rendering Details | Special Notes |
|---|---|---|---|
|
| Displays the | Uses tooltip UI components. |
|
| Displays the | Same as |
|
| Displays the | Shows localized type string. |
|
| Shows "Yes" if true, "No" if false. | Boolean display. |
|
| Displays edit (pencil icon) and delete (trash icon) buttons. Clicking triggers handlers passed in props. | Actions column is fixed visible ( |
Table Setup with useReactTable
The table instance is created with:
data: The array of query records.columns: Columns definition described above.State handlers for sorting, filtering, and visibility.
Row models for core, sorting, filtering, and pagination are enabled.
State updates are wired to React's state setters.
Rendering
The table is wrapped in a
<div>with full width and rounded border styling.Table headers are rendered dynamically based on header groups from React Table.
Table body renders visible rows if any; otherwise, it shows an empty state component (
TableEmpty).Cells render using
flexRenderto support React Table's flexible rendering model.Rows mark selected state with
data-state="selected"attribute (though selection logic is not detailed here).
Implementation Details / Algorithms
React Table Integration: The component uses hooks from
@tanstack/react-tableto handle complex table behaviors like filtering, sorting, pagination, and dynamic column visibility. This abstracts away a lot of manual state management and DOM manipulation.Tooltips: For the
keyandnamecolumns, truncation is applied with the help of CSS, and full content is shown inside tooltips for usability.Localization: All user-facing text (headers, column content like types, "Yes"/"No" for optional) is localized using the
tfunction fromreact-i18next, ensuring multi-language support.Row Actions: Edit and delete buttons are styled with transparent backgrounds but show hover effects. Icons from
lucide-reactprovide visual affordances.State Synchronization: Sorting, filtering, and visibility states are managed internally using React state hooks and passed to React Table for synchronized UI rendering.
Interaction with Other Parts of the System
Data Model: Depends on the
BeginQueryinterface/type for the data shape. This interface is imported from a relative path (../../interface), indicating the data structure is defined elsewhere.UI Components: Relies heavily on reusable UI components for table (
Table,TableBody,TableCell, etc.), buttons, tooltips, and empty state placeholders from the local component library (@/components).Localization: Tied to
react-i18nextfor translation, suggesting the app has internationalization support.Parent Components: The callbacks
deleteRecordandshowModalimply that state changes like deletion and editing are managed outside this component, keepingQueryTablefocused on presentation and interaction.
Visual Diagram
componentDiagram
component QueryTable {
+props: IProps
+state: sorting, columnFilters, columnVisibility
+columns: ColumnDef<BeginQuery>[]
+methods: render()
}
component BeginQuery
component useReactTable
component TableUI {
Table
TableHeader
TableBody
TableRow
TableCell
Button
Tooltip
}
QueryTable --> BeginQuery : data[]
QueryTable --> useReactTable : manages sorting, filtering, pagination
QueryTable --> TableUI : renders table structure & UI
QueryTable --> "Parent Component" : deleteRecord(), showModal()
Summary
query-table.tsx defines a reusable, localized, and highly interactive data table React component named QueryTable. It leverages @tanstack/react-table for advanced table features, integrates custom UI components for a polished look and feel, and provides edit/delete functionality through callbacks. The component is designed to be a presentational and interactive layer that delegates data mutations and modal controls to parent components, supporting separation of concerns and modular design.