@@ -10,7 +10,7 @@ import { cn } from '@conar/ui/lib/utils'
1010import { RiCloseLine , RiFileList3Line , RiInformationLine , RiKey2Line , RiRefreshLine , RiTable2 } from '@remixicon/react'
1111import { createFileRoute } from '@tanstack/react-router'
1212import { AnimatePresence , motion } from 'motion/react'
13- import { useMemo , useState } from 'react'
13+ import { useState } from 'react'
1414import { useConnectionIndexes , useConnectionTablesAndSchemas } from '~/entities/connection/queries'
1515
1616const MotionCard = motion . create ( Card )
@@ -31,68 +31,63 @@ interface GroupedIndex extends IndexItem {
3131
3232type IndexType = 'primary' | 'unique' | 'regular'
3333
34- const dropDownItems : { label : string , value : IndexType } [ ] = [
34+ const dropdownItems : { label : string , value : IndexType } [ ] = [
3535 { label : 'Primary Key' , value : 'primary' } ,
3636 { label : 'Unique Index' , value : 'unique' } ,
3737 { label : 'Regular Index' , value : 'regular' } ,
3838]
3939
40+ function getIndexType ( indexItem : IndexItem ) : IndexType {
41+ if ( indexItem . isPrimary )
42+ return 'primary'
43+ if ( indexItem . isUnique )
44+ return 'unique'
45+ return 'regular'
46+ }
47+
4048function DatabaseIndexesPage ( ) {
4149 const { connection } = Route . useLoaderData ( )
4250 const { data : indexes , refetch, isRefetching } = useConnectionIndexes ( { connection } )
4351 const { data } = useConnectionTablesAndSchemas ( { connection } )
44- const schemas = useMemo ( ( ) => data ?. schemas . map ( ( { name } ) => name ) ?? [ ] , [ data ] )
52+ const schemas = data ?. schemas . map ( ( { name } ) => name ) ?? [ ]
4553 const [ selectedSchema , setSelectedSchema ] = useState ( schemas [ 0 ] )
4654 const [ search , setSearch ] = useState ( '' )
4755 const [ filterType , setFilterType ] = useState < IndexType | 'all' > ( 'all' )
4856
4957 if ( schemas . length > 0 && ( ! selectedSchema || ! schemas . includes ( selectedSchema ) ) )
5058 setSelectedSchema ( schemas [ 0 ] )
5159
52- const groupedIndexes = useMemo ( ( ) => {
53- return indexes ?. reduce < Record < string , GroupedIndex > > ( ( acc : Record < string , GroupedIndex > , item : IndexItem ) => {
54- const indexItem = item
55-
56- if ( indexItem . schema !== selectedSchema )
57- return acc
58-
59- const isPrimary = indexItem . isPrimary
60- const isUnique = indexItem . isUnique && ! indexItem . isPrimary
61-
62- let type : IndexType = 'regular'
63- if ( isPrimary )
64- type = 'primary'
65- else if ( isUnique )
66- type = 'unique'
67-
68- const matchesFilter = filterType === 'all' || filterType === type
69-
70- if ( ! matchesFilter )
71- return acc
72-
73- const matchesSearch = ! search
74- || indexItem . name . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
75- || indexItem . table . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
76- || indexItem . column . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
77-
78- if ( ! matchesSearch )
79- return acc
80-
81- const key = `${ indexItem . schema } -${ indexItem . table } -${ indexItem . name } `
82- if ( ! acc [ key ] ) {
83- acc [ key ] = {
84- ...indexItem ,
85- columns : [ indexItem . column ] ,
86- }
87- }
88- else {
89- if ( ! acc [ key ] . columns . includes ( indexItem . column ) ) {
90- acc [ key ] . columns . push ( indexItem . column )
91- }
92- }
60+ const groupedIndexes = indexes ?. reduce < Record < string , GroupedIndex > > ( ( acc , indexItem ) => {
61+ if ( indexItem . schema !== selectedSchema )
9362 return acc
94- } , { } )
95- } , [ indexes , selectedSchema , search , filterType ] )
63+
64+ const matchesFilter = filterType === 'all' || filterType === getIndexType ( indexItem )
65+
66+ if ( ! matchesFilter )
67+ return acc
68+
69+ const matchesSearch = ! search
70+ || indexItem . name . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
71+ || indexItem . table . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
72+ || indexItem . column . toLowerCase ( ) . includes ( search . toLowerCase ( ) )
73+
74+ if ( ! matchesSearch )
75+ return acc
76+
77+ const key = `${ indexItem . schema } -${ indexItem . table } -${ indexItem . name } `
78+
79+ return {
80+ ...acc ,
81+ [ key ] : acc [ key ]
82+ ? {
83+ ...acc [ key ] ,
84+ columns : acc [ key ] . columns . includes ( indexItem . column )
85+ ? acc [ key ] . columns
86+ : [ ...acc [ key ] . columns , indexItem . column ] ,
87+ }
88+ : { ...indexItem , columns : [ indexItem . column ] } ,
89+ }
90+ } , { } )
9691
9792 const indexList = Object . values ( groupedIndexes ?? { } )
9893
@@ -128,7 +123,7 @@ function DatabaseIndexesPage() {
128123 </ SelectTrigger >
129124 < SelectContent >
130125 < SelectItem value = "all" > All Types</ SelectItem >
131- { dropDownItems . map ( type => (
126+ { dropdownItems . map ( type => (
132127 < SelectItem key = { type . value } value = { type . value } >
133128 { type . label }
134129 </ SelectItem >
@@ -165,10 +160,12 @@ function DatabaseIndexesPage() {
165160 { indexList . length === 0 && (
166161 < MotionCard
167162 layout
168- initial = { { opacity : 0 , scale : 0.95 } }
163+ initial = { { opacity : 0 , scale : 0.75 } }
169164 animate = { { opacity : 1 , scale : 1 } }
165+ exit = { { opacity : 0 , scale : 0.75 } }
166+ transition = { { duration : 0.15 } }
170167 className = { `
171- mt-4 w-full border border-dashed border-muted-foreground/20
168+ w-full border border-dashed border-muted-foreground/20
172169 bg-muted/10 p-10 text-center
173170 ` }
174171 >
@@ -179,7 +176,6 @@ function DatabaseIndexesPage() {
179176 < h3 className = "text-lg font-medium" > No indexes found</ h3 >
180177 </ MotionCard >
181178 ) }
182-
183179 { indexList . map ( item => (
184180 < MotionCard
185181 key = { `${ item . schema } -${ item . table } -${ item . name } ` }
0 commit comments