Skip to content

Commit 7e1d4ca

Browse files
committed
refactor: simplify
1 parent 3e6699b commit 7e1d4ca

3 files changed

Lines changed: 61 additions & 69 deletions

File tree

apps/api/drizzle/schema/subscriptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { baseTable } from '../base-table'
1212
import { users } from './auth'
1313

14-
export const subscriptionPeriod = pgEnum('subscription_period', ['monthly', 'yearly'] as const)
14+
export const subscriptionPeriod = pgEnum('subscription_period', ['monthly', 'yearly'])
1515

1616
export const subscriptions = pgTable('subscriptions', {
1717
...baseTable,

apps/desktop/src/routes/_protected/database/$id/definitions/constraints/index.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { cn } from '@conar/ui/lib/utils'
1111
import { RiCloseLine, RiDatabase2Line, RiInformationLine, RiKey2Line, RiLinksLine, RiRefreshLine, RiTable2 } from '@remixicon/react'
1212
import { createFileRoute } from '@tanstack/react-router'
1313
import { AnimatePresence, motion } from 'motion/react'
14-
import { useEffect, useMemo, useState } from 'react'
14+
import { useState } from 'react'
1515
import { useConnectionConstraints, useConnectionTablesAndSchemas } from '~/entities/connection/queries'
1616

1717
const MotionCard = motion.create(Card)
@@ -57,28 +57,24 @@ function DatabaseConstraintsPage() {
5757
const { connection } = Route.useLoaderData()
5858
const { data: constraints, refetch, isRefetching } = useConnectionConstraints({ connection })
5959
const { data } = useConnectionTablesAndSchemas({ connection })
60-
const schemas = useMemo(() => data?.schemas.map(({ name }) => name) ?? [], [data])
60+
const schemas = data?.schemas.map(({ name }) => name) ?? []
6161
const [selectedSchema, setSelectedSchema] = useState(schemas[0])
6262
const [search, setSearch] = useState('')
6363
const [filterType, setFilterType] = useState<ConstraintItem['type'] | 'all'>('all')
6464

65-
useEffect(() => {
66-
if (schemas.length > 0 && (!selectedSchema || !schemas.includes(selectedSchema)))
67-
setSelectedSchema(schemas[0])
68-
}, [schemas, selectedSchema])
65+
if (schemas.length > 0 && (!selectedSchema || !schemas.includes(selectedSchema)))
66+
setSelectedSchema(schemas[0])
6967

70-
const filteredConstraints = useMemo(() => {
71-
return constraints?.filter(item =>
72-
item.schema === selectedSchema
73-
&& (filterType === 'all' || filterType === item.type)
74-
&& (!search
75-
|| item.name.toLowerCase().includes(search.toLowerCase())
76-
|| item.table.toLowerCase().includes(search.toLowerCase())
77-
|| (item.column && item.column.toLowerCase().includes(search.toLowerCase()))
78-
|| (item.type && item.type.toLowerCase().includes(search.toLowerCase()))
79-
),
80-
) ?? []
81-
}, [constraints, selectedSchema, search, filterType])
68+
const filteredConstraints = constraints?.filter(item =>
69+
item.schema === selectedSchema
70+
&& (filterType === 'all' || filterType === item.type)
71+
&& (!search
72+
|| item.name.toLowerCase().includes(search.toLowerCase())
73+
|| item.table.toLowerCase().includes(search.toLowerCase())
74+
|| (item.column && item.column.toLowerCase().includes(search.toLowerCase()))
75+
|| (item.type && item.type.toLowerCase().includes(search.toLowerCase()))
76+
),
77+
) ?? []
8278

8379
return (
8480
<>

apps/desktop/src/routes/_protected/database/$id/definitions/indexes/index.tsx

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { cn } from '@conar/ui/lib/utils'
1010
import { RiCloseLine, RiFileList3Line, RiInformationLine, RiKey2Line, RiRefreshLine, RiTable2 } from '@remixicon/react'
1111
import { createFileRoute } from '@tanstack/react-router'
1212
import { AnimatePresence, motion } from 'motion/react'
13-
import { useMemo, useState } from 'react'
13+
import { useState } from 'react'
1414
import { useConnectionIndexes, useConnectionTablesAndSchemas } from '~/entities/connection/queries'
1515

1616
const MotionCard = motion.create(Card)
@@ -31,68 +31,63 @@ interface GroupedIndex extends IndexItem {
3131

3232
type 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+
4048
function 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

Comments
 (0)