Skip to content

Commit 8ebcea9

Browse files
committed
Refactor query execution handling by renaming onRun to runQuery and updating its usage in QueryHistory and DeploymentPage components
1 parent 93c8868 commit 8ebcea9

File tree

3 files changed

+73
-144
lines changed

3 files changed

+73
-144
lines changed

web/components/QueryHistory.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChevronRight, Clock, Play, Search, Trash2 } from 'lucide-preact'
22
import { A, navigate, url } from '../lib/router.tsx'
3-
import { onRun, queriesHistory } from '../lib/shared.tsx'
3+
import { queriesHistory, runQuery } from '../lib/shared.tsx'
44

55
const deleteQuery = (hash: string) => {
66
const updatedHistory = { ...queriesHistory.value }
@@ -70,7 +70,7 @@ export const QueryHistory = () => {
7070
params={{ q: item.query, tab: 'queries' }}
7171
class='btn btn-xs btn-ghost'
7272
title='Run query'
73-
onClick={() => onRun(item.query)}
73+
onClick={() => runQuery(item.query)}
7474
>
7575
<Play class='w-4 h-4' />
7676
</A>

web/lib/shared.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const deployments = api['GET/api/project/deployments'].signal()
3232
// API signal for current project
3333
export const project = api['GET/api/project'].signal()
3434

35-
export const onRun = (query: string) => {
35+
export const runQuery = (query?: string) => {
3636
if (querier.pending) return
3737
const { dep, tab } = url.params
3838
if (dep && tab === 'queries' && query) {

web/pages/DeploymentPage.tsx

Lines changed: 70 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ import { QueryHistory } from '../components/QueryHistory.tsx'
3535
import { JSX } from 'preact'
3636
import {
3737
deployments,
38-
onRun,
3938
querier,
4039
queriesHistory,
40+
runQuery,
4141
sidebarItems,
42-
// tableData,
4342
} from '../lib/shared.tsx'
4443

4544
type AnyRecord = Record<string, unknown>
@@ -125,8 +124,8 @@ const onDownload = () => {
125124
}
126125
}
127126

128-
function LineNumbers({ content }: { content: string }) {
129-
const lineCount = Math.max(1, (content.match(/\n/g)?.length ?? 0) + 1)
127+
const LineNumbers = () => {
128+
const lineCount = Math.max(1, (url.params.q?.match(/\n/g)?.length ?? 0) + 1)
130129

131130
return (
132131
<div class='absolute inset-y-0 left-0 w-11 select-none overflow-hidden border-r border-base-300 z-10'>
@@ -144,19 +143,15 @@ const handleInput = (e: Event) => {
144143
}
145144

146145
const handleKeyDown = (e: KeyboardEvent) => {
147-
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
148-
e.preventDefault()
149-
const q = url.params.q
150-
if (q) {
151-
onRun(q)
152-
}
153-
}
146+
if (!((e.ctrlKey || e.metaKey) && e.key === 'Enter')) return
147+
e.preventDefault()
148+
runQuery(url.params.q || '')
154149
}
155150

156151
const SQLEditor = () => (
157152
<div class='resize-y min-h-[120px] max-h-[80vh] overflow-hidden border border-base-300 rounded-lg bg-base-100'>
158153
<div class='relative h-full bg-base-100 rounded-lg overflow-hidden focus-within:border-primary/50 transition-colors'>
159-
<LineNumbers content={url.params.q || ''} />
154+
<LineNumbers />
160155
<textarea
161156
value={url.params.q || ''}
162157
onInput={handleInput}
@@ -288,11 +283,9 @@ effect(() => {
288283
}
289284
})
290285

291-
const RowNumberCell = (
292-
{ page, index, pageSize }: { page: number; index: number; pageSize: number },
293-
) => (
286+
const RowNumberCell = ({ index }: { index: number }) => (
294287
<td class='sticky left-0 bg-base-100 tabular-nums font-medium text-xs text-base-content/60 w-16 max-w-[4rem]'>
295-
{(page - 1) * pageSize + index + 1}
288+
{(Number(url.params.tpage) || 0) * pageSize + index + 1}
296289
</td>
297290
)
298291

@@ -333,21 +326,11 @@ const EmptyRow = ({ colSpan }: { colSpan: number }) => (
333326
</tr>
334327
)
335328

336-
const DataRow = ({
337-
row,
338-
columns,
339-
index,
340-
page,
341-
pageSize,
342-
}: {
343-
row: AnyRecord
344-
columns: string[]
345-
index: number
346-
page: number
347-
pageSize: number
348-
}) => (
329+
const DataRow = (
330+
{ row, columns, index }: { row: AnyRecord; columns: string[]; index: number },
331+
) => (
349332
<tr class='hover:bg-base-200/50'>
350-
<RowNumberCell page={page} index={index} pageSize={pageSize} />
333+
<RowNumberCell index={index} />
351334
{columns.map((key, i) => (
352335
<td key={i} class='align-top min-w-[8rem] max-w-[20rem]'>
353336
<TableCell value={row[key]} />
@@ -379,51 +362,37 @@ const TableHeader = ({ columns }: { columns: string[] }) => (
379362
</thead>
380363
)
381364

382-
const PaginationControls = ({
383-
tpage,
384-
hasPrev,
385-
hasNext,
386-
}: {
387-
tpage: number
388-
hasPrev: boolean
389-
hasNext: boolean
390-
}) => (
391-
<div class='join'>
392-
<A
393-
params={{ tpage: tpage - 1 }}
394-
class={`join-item btn btn-sm ${hasPrev ? '' : 'btn-disabled'}`}
395-
aria-label='Previous page'
396-
>
397-
398-
</A>
399-
<A
400-
params={{ tpage: tpage + 1 }}
401-
class={`join-item btn btn-sm ${hasNext ? '' : 'btn-disabled'}`}
402-
aria-label='Next page'
403-
>
404-
405-
</A>
406-
</div>
407-
)
365+
const PaginationControls = ({ totalPages }: { totalPages: number }) => {
366+
const tpage = Number(url.params.tpage) || 0
367+
const page = tpage + 1
368+
const hasNext = page < totalPages
369+
const hasPrev = page > 1
370+
return (
371+
<div class='join'>
372+
<A
373+
params={{ tpage: tpage - 1 }}
374+
class={`join-item btn btn-sm ${hasPrev ? '' : 'btn-disabled'}`}
375+
aria-label='Previous page'
376+
>
377+
378+
</A>
379+
<A
380+
params={{ tpage: tpage + 1 }}
381+
class={`join-item btn btn-sm ${hasNext ? '' : 'btn-disabled'}`}
382+
aria-label='Next page'
383+
>
384+
385+
</A>
386+
</div>
387+
)
388+
}
408389

409-
const TableFooter = ({
410-
tab,
411-
rows,
412-
page,
413-
totalPages,
414-
tpage,
415-
hasPrev,
416-
hasNext,
417-
}: {
418-
tab: string
419-
rows: AnyRecord[]
420-
page: number
421-
totalPages: number
422-
tpage: number
423-
hasPrev: boolean
424-
hasNext: boolean
425-
}) => {
426-
if (tab !== 'tables') return null
390+
const TableFooter = ({ rows }: { rows: AnyRecord[] }) => {
391+
const page = (Number(url.params.tpage) || 0) + 1
392+
const totalPages = Math.max(
393+
1,
394+
Math.ceil((tableData.data?.totalRows || 0) / pageSize),
395+
)
427396

428397
return (
429398
<div class='bg-base-100 border-t border-base-300 px-4 sm:px-6 py-3 shrink-0'>
@@ -437,91 +406,52 @@ const TableFooter = ({
437406
</span>
438407
<div class='flex items-center gap-2'>
439408
<span class='hidden sm:inline'>Page {page} of {totalPages}</span>
440-
<PaginationControls
441-
tpage={tpage}
442-
hasPrev={hasPrev}
443-
hasNext={hasNext}
444-
/>
409+
<PaginationControls totalPages={totalPages} />
445410
</div>
446411
</div>
447412
</div>
448413
)
449414
}
450415

451-
const TableContent = ({
452-
columns,
453-
rows,
454-
page,
455-
pageSize,
456-
}: {
457-
columns: string[]
458-
rows: AnyRecord[]
459-
page: number
460-
pageSize: number
461-
}) => (
462-
<table class='table table-zebra w-full'>
463-
<TableHeader columns={columns} />
464-
<tbody>
465-
{rows.length === 0
466-
? <EmptyRow colSpan={Math.max(2, columns.length + 1)} />
467-
: (
468-
rows.map((row, index) => (
469-
<DataRow
470-
key={index}
471-
row={row}
472-
columns={columns}
473-
index={index}
474-
page={page}
475-
pageSize={pageSize}
476-
/>
477-
))
478-
)}
479-
</tbody>
480-
</table>
481-
)
482-
416+
const TableContent = ({ rows }: { rows: AnyRecord[] }) => {
417+
const columns = Object.keys(rows[0] || {})
418+
return (
419+
<table class='table table-zebra w-full'>
420+
<TableHeader columns={columns} />
421+
<tbody>
422+
{rows.length === 0
423+
? <EmptyRow colSpan={Math.max(2, columns.length + 1)} />
424+
: (
425+
rows.map((row, index) => (
426+
<DataRow
427+
key={index}
428+
row={row}
429+
columns={columns}
430+
index={index}
431+
/>
432+
))
433+
)}
434+
</tbody>
435+
</table>
436+
)
437+
}
483438
const DataTable = () => {
484439
const tab = url.params.tab
485-
const tpage = Number(url.params.tpage) || 0
486-
const page = tpage + 1
487440

488-
const data = tab === 'tables'
441+
const rows = tab === 'tables'
489442
? tableData.data?.rows || []
490443
: tab === 'queries'
491444
? querier.data?.rows || []
492445
: []
493446

494-
const columns = Object.keys(data[0] || {})
495-
const rows = data ?? []
496-
const totalPages = Math.max(
497-
1,
498-
Math.ceil((tableData.data?.totalRows || 0) / pageSize),
499-
)
500-
const hasNext = page < totalPages
501-
const hasPrev = page > 1
502-
503447
return (
504448
<div class='flex flex-col h-full min-h-0 grow-1'>
505449
<div class='flex-1 min-h-0 overflow-hidden'>
506450
<div class='w-full overflow-x-auto overflow-y-auto h-full'>
507-
<TableContent
508-
columns={columns}
509-
rows={rows}
510-
page={page}
511-
pageSize={pageSize}
512-
/>
451+
<TableContent rows={rows} />
513452
</div>
514453
</div>
515-
516-
<TableFooter
517-
tab={tab || ''}
518-
rows={rows}
519-
page={page}
520-
totalPages={totalPages}
521-
tpage={tpage}
522-
hasPrev={hasPrev}
523-
hasNext={hasNext}
524-
/>
454+
<TableFooter rows={rows} />
525455
</div>
526456
)
527457
}
@@ -582,7 +512,6 @@ function Header() {
582512
function SchemaPanel() {
583513
const dep = url.params.dep
584514

585-
// Group tables by schema name
586515
const grouped: Record<
587516
string,
588517
NonNullable<NonNullable<typeof schema.data>['tables']>
@@ -817,7 +746,7 @@ function TabNavigation({
817746
<A
818747
params={{ drawer: activeTab === 'tables' ? 'insert' : null }}
819748
onClick={activeTab === 'queries'
820-
? () => onRun(url.params.q || '')
749+
? () => runQuery(url.params.q || '')
821750
: undefined}
822751
class='btn btn-primary btn-sm'
823752
>

0 commit comments

Comments
 (0)