Skip to content

Commit b26a177

Browse files
authored
feat: refacto codebase (#484)
1 parent 436c291 commit b26a177

61 files changed

Lines changed: 1234 additions & 477 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

drizzle.config.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'dotenv/config'
2-
import { defineConfig } from 'drizzle-kit'
2+
import { defineConfig, type Config } from 'drizzle-kit'
33

44
export default defineConfig({
5-
out: './src/server/db/migrations',
6-
schema: './src/server/db/schema.ts',
5+
out: './src/server/databases/migrations',
6+
schema: './src/server/databases/schema.ts',
77
dialect: 'postgresql',
8-
dbCredentials: {
9-
url: process.env.DATABASE_URL
10-
}
11-
})
8+
dbCredentials: { url: process.env.DATABASE_URL }
9+
}) satisfies Config

src/app/(studio)/studio/activities/logs/page.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ import { Skeleton } from '@/components/ui/skeleton'
4949
import { readableTimestamp, formatToReadable, cn } from '@/lib/utils'
5050
import { LOG_TYPES, deviceIcons } from '@/lib/constants'
5151

52-
import { getAllLogs, countLogs } from '@/server/actions/log'
53-
import type { LogSelect } from '@/server/types'
52+
import { getAllLogs, countLogs } from '@/server/actions'
53+
import type { LogWithTimestamps } from '@/server/types/extended'
5454

5555
import { useEvents, EventTypes } from '@/hooks/use-events'
5656

5757
const Page = () => {
5858
const { userId } = useAuth()
5959
const { events } = useEvents((event) => event.type === EventTypes.REPORT_IMPORTED)
6060

61-
const [logs, setLogs] = useState<LogSelect[]>([])
61+
const [logs, setLogs] = useState<LogWithTimestamps[]>([])
6262
const [isLoading, setIsLoading] = useState<boolean>(true)
6363
const [searchTerm, setSearchTerm] = useState<string>('')
6464
const [currentPage, setCurrentPage] = useState<number>(1)
@@ -81,13 +81,25 @@ const Page = () => {
8181

8282
const fetchLogs = useCallback(() => {
8383
getAllLogs({ userId: userId as string, currentPage, searchTerm, itemsPerPage })
84-
.then((data) => {
85-
setLogs(data)
86-
countLogs({ userId: userId as string }).then((count) => setTotal(count[0].count))
84+
.then((response) => {
85+
if (response.success && response.data) {
86+
setLogs(response.data as LogWithTimestamps[])
87+
88+
// Get total count for pagination
89+
countLogs({ userId: userId as string }).then((countResponse) => {
90+
if (countResponse.success && countResponse.data) {
91+
setTotal(countResponse.data.count)
92+
} else {
93+
console.error('Error fetching logs count:', countResponse.error)
94+
}
95+
})
96+
} else {
97+
console.error('Error fetching logs:', response.error)
98+
}
8799
})
88100
.catch((err) => console.log(err))
89101
.finally(() => setIsLoading(false))
90-
}, [userId, currentPage, searchTerm])
102+
}, [userId, currentPage, searchTerm, itemsPerPage])
91103

92104
useEffect(() => {
93105
fetchLogs()
@@ -227,7 +239,13 @@ const Page = () => {
227239
)
228240
}
229241

230-
const ActivitiesTable = ({ isLoading, logs }: { isLoading: boolean; logs: LogSelect[] }) => {
242+
const ActivitiesTable = ({
243+
isLoading,
244+
logs
245+
}: {
246+
isLoading: boolean
247+
logs: LogWithTimestamps[]
248+
}) => {
231249
return (
232250
<Table>
233251
<TableHeader>

src/app/(studio)/studio/page.tsx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ import { ChartDataItem } from '@/lib/definitions'
2323
import { readableTimestamp } from '@/lib/utils'
2424
import { months, deviceIcons } from '@/lib/constants'
2525

26-
import { getLastThreeReport } from '@/server/actions/report'
27-
import { getLastThreeLogs } from '@/server/actions/log'
28-
import { getCurrentYearMetrics } from '@/server/actions/metric'
29-
import type { ReportSelect, LogSelect } from '@/server/types'
26+
import { getLastThreeReport, getLastThreeLogs, getCurrentYearMetrics } from '@/server/actions'
27+
import type { LogWithTimestamps, ReportWithTimestamps } from '@/server/types/extended'
3028

3129
import { useEvents, EventTypes } from '@/hooks/use-events'
3230

@@ -38,9 +36,9 @@ const Page = () => {
3836
const { events } = useEvents((event) => event.type === EventTypes.REPORT_IMPORTED)
3937

4038
const [isLoading, setIsLoading] = useState<boolean>(true)
41-
const [reports, setReports] = useState<ReportSelect[]>([])
39+
const [reports, setReports] = useState<ReportWithTimestamps[]>([])
4240
const [metrics, setMetrics] = useState<ChartDataItem[]>()
43-
const [logs, setLogs] = useState<LogSelect[]>([])
41+
const [logs, setLogs] = useState<LogWithTimestamps[]>([])
4442

4543
const currentYear = new Date().getFullYear()
4644

@@ -50,22 +48,39 @@ const Page = () => {
5048
getLastThreeReport({ userId: userId as string }),
5149
getLastThreeLogs({ userId: userId as string })
5250
])
53-
.then(([metricsData, reportsData, activitiesData]) => {
51+
.then(([metricsResponse, reportsResponse, logsResponse]) => {
52+
// Initialize chart data with zero values
5453
const data = months.map((name) => ({
5554
name,
5655
total: 0
5756
}))
5857

59-
metricsData.forEach((metric) => {
60-
const monthIndex = metric.month - 1
61-
if (monthIndex >= 0 && monthIndex < 12) {
62-
data[monthIndex].total = metric.total
63-
}
64-
})
58+
// Process metrics data if successful
59+
if (metricsResponse.success && metricsResponse.data) {
60+
metricsResponse.data.forEach((metric) => {
61+
const monthIndex = metric.month - 1
62+
if (monthIndex >= 0 && monthIndex < 12) {
63+
data[monthIndex].total = metric.total
64+
}
65+
})
66+
setMetrics(data as ChartDataItem[])
67+
} else {
68+
console.error('Error fetching metrics:', metricsResponse.error)
69+
}
70+
71+
// Process reports data if successful
72+
if (reportsResponse.success && reportsResponse.data) {
73+
setReports(reportsResponse.data as ReportWithTimestamps[])
74+
} else {
75+
console.error('Error fetching reports:', reportsResponse.error)
76+
}
6577

66-
setMetrics(data as ChartDataItem[])
67-
setReports(reportsData as ReportSelect[])
68-
setLogs(activitiesData as LogSelect[])
78+
// Process logs data if successful
79+
if (logsResponse.success && logsResponse.data) {
80+
setLogs(logsResponse.data as LogWithTimestamps[])
81+
} else {
82+
console.error('Error fetching logs:', logsResponse.error)
83+
}
6984
})
7085
.catch((err) => console.log(err))
7186
.finally(() => setIsLoading(false))

src/app/(studio)/studio/reports/[reportSlug]/cases/[caseId]/page.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import { humanizeDuration, readableTimestamp } from '@/lib/utils'
2626
import { CASE_STATUS, PATH } from '@/lib/constants'
2727
import { TContentCase, TContentSubCase } from '@/lib/definitions'
2828

29-
import { getReport } from '@/server/actions/report'
30-
import type { ReportModiefied } from '@/server/types'
29+
import { getReport } from '@/server/actions'
30+
import type { ReportWithTimestamps } from '@/server/types/extended'
31+
import type { ReportModiefied } from '@/server/databases/types'
3132

3233
const Page = () => {
3334
const { reportSlug, caseId } = useParams()
@@ -43,9 +44,14 @@ const Page = () => {
4344

4445
useEffect(() => {
4546
getReport({ userId: userId as string, reportSlug: reportSlug as string })
46-
.then((data) => {
47-
const parsedData = data as ReportModiefied[]
48-
setReport(parsedData[0])
47+
.then((response) => {
48+
if (response.success && response.data) {
49+
// Get report from the response data
50+
const report = response.data as ReportWithTimestamps & ReportModiefied
51+
setReport(report)
52+
} else {
53+
console.error('Error fetching report for case detail:', response.error)
54+
}
4955
})
5056
.catch((err) => console.log(err))
5157
}, [userId, reportSlug])

src/app/(studio)/studio/reports/[reportSlug]/page.tsx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,27 @@ import { LabelledPieChart } from '@/components/charts/labelled-pie-chart'
1313
import { humanizeDuration } from '@/lib/utils'
1414
import { Metric } from '@/lib/definitions'
1515

16-
import { getReport } from '@/server/actions/report'
17-
import type { ReportModiefied } from '@/server/types'
16+
import { getReport } from '@/server/actions'
17+
import type { ReportWithTimestamps } from '@/server/types/extended'
18+
import type { ReportModiefied } from '@/server/databases/types'
1819

1920
const Page = ({ params }: { params: { reportSlug: string } }) => {
2021
const { userId } = useAuth()
21-
const [metrics, setMetrics] = useState<Metric>()
22+
const [metrics, setMetrics] = useState<Metric | undefined>()
2223

2324
useEffect(() => {
2425
getReport({ userId: userId as string, reportSlug: params.reportSlug })
25-
.then((data) => {
26-
const parsedData = data as ReportModiefied[]
27-
setMetrics(parsedData[0]?.metadata?.drowser?.metrics as Metric)
26+
.then((response) => {
27+
if (response.success && response.data) {
28+
// Get metrics from the response data
29+
const report = response.data as ReportWithTimestamps & ReportModiefied
30+
// Fixed type assertion by ensuring proper type matching
31+
if (report?.metadata?.drowser?.metrics) {
32+
setMetrics(report.metadata.drowser.metrics as unknown as Metric)
33+
}
34+
} else {
35+
console.error('Error fetching report:', response.error)
36+
}
2837
})
2938
.catch((err) => console.log(err))
3039
}, [userId, params.reportSlug])
@@ -37,70 +46,70 @@ const Page = ({ params }: { params: { reportSlug: string } }) => {
3746
<CardHeader>
3847
<CardTitle>Total Tests</CardTitle>
3948
<CardDescription>
40-
<span className='text-4xl font-bold'>{metrics.total_tests ?? 0}</span>
49+
<span className='text-4xl font-bold'>{metrics?.total_tests ?? 0}</span>
4150
</CardDescription>
4251
</CardHeader>
4352
<CardContent>
44-
<LineChart className='aspect-[4/3]' data={metrics.graphs.total_tests} />
53+
<LineChart className='aspect-[4/3]' data={metrics?.graphs?.total_tests} />
4554
</CardContent>
4655
</Card>
4756
<Card>
4857
<CardHeader>
4958
<CardTitle>Passing Tests</CardTitle>
5059
<CardDescription>
51-
<span className='text-4xl font-bold'>{metrics.passing_tests ?? 0}</span>
60+
<span className='text-4xl font-bold'>{metrics?.passing_tests ?? 0}</span>
5261
</CardDescription>
5362
</CardHeader>
5463
<CardContent>
55-
<BarChart className='aspect-[4/3]' data={metrics.graphs.passing_tests} />
64+
<BarChart className='aspect-[4/3]' data={metrics?.graphs?.passing_tests} />
5665
</CardContent>
5766
</Card>
5867
<Card>
5968
<CardHeader>
6069
<CardTitle>Failed Tests</CardTitle>
6170
<CardDescription>
62-
<span className='text-4xl font-bold'>{metrics.failed_tests ?? 0}</span>
71+
<span className='text-4xl font-bold'>{metrics?.failed_tests ?? 0}</span>
6372
</CardDescription>
6473
</CardHeader>
6574
<CardContent>
66-
<BarChart className='aspect-[4/3]' data={metrics.graphs.failed_tests} />
75+
<BarChart className='aspect-[4/3]' data={metrics?.graphs?.failed_tests} />
6776
</CardContent>
6877
</Card>
6978
<Card>
7079
<CardHeader>
7180
<CardTitle>Test Coverage</CardTitle>
7281
<CardDescription>
7382
<span className='text-4xl font-bold'>{`${(
74-
metrics.test_coverage ?? 0
83+
metrics?.test_coverage ?? 0
7584
).toFixed()}%`}</span>
7685
</CardDescription>
7786
</CardHeader>
7887
<CardContent>
79-
<BarChart className='aspect-[4/3]' data={metrics.graphs.test_coverage} />
88+
<BarChart className='aspect-[4/3]' data={metrics?.graphs?.test_coverage} />
8089
</CardContent>
8190
</Card>
8291
<Card>
8392
<CardHeader>
8493
<CardTitle>Avg. Test Duration</CardTitle>
8594
<CardDescription>
8695
<span className='text-4xl font-bold'>
87-
{humanizeDuration(metrics.avg_test_duration ?? 0)}
96+
{humanizeDuration(metrics?.avg_test_duration ?? 0)}
8897
</span>
8998
</CardDescription>
9099
</CardHeader>
91100
<CardContent>
92-
<BarChart className='aspect-[4/3]' data={metrics.graphs.avg_test_duration} />
101+
<BarChart className='aspect-[4/3]' data={metrics?.graphs?.avg_test_duration} />
93102
</CardContent>
94103
</Card>
95104
<Card>
96105
<CardHeader>
97106
<CardTitle>Flaky Tests</CardTitle>
98107
<CardDescription>
99-
<span className='text-4xl font-bold'>{metrics.flaky_tests ?? 0}</span>
108+
<span className='text-4xl font-bold'>{metrics?.flaky_tests ?? 0}</span>
100109
</CardDescription>
101110
</CardHeader>
102111
<CardContent>
103-
<LabelledPieChart className='aspect-[4/3]' data={metrics.graphs.flaky_tests} />
112+
<LabelledPieChart className='aspect-[4/3]' data={metrics?.graphs?.flaky_tests} />
104113
</CardContent>
105114
</Card>
106115
</div>
@@ -111,7 +120,7 @@ const Page = ({ params }: { params: { reportSlug: string } }) => {
111120
<h2 className='text-2xl font-bold'>No Metrics Available</h2>
112121
<p className='text-gray-500'>
113122
It looks like there are no metrics to display at the moment. Please provide a verified
114-
or conform json configuration form the <strong>Drowser</strong> package
123+
or conform json configuration from the <strong>Drowser</strong> package
115124
</p>
116125
</div>
117126
</div>

src/app/(studio)/studio/reports/[reportSlug]/visualize/[[...rest]]/page.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import { TContentCase, TContentSubCase } from '@/lib/definitions'
2929
import { humanizeDuration, readableTimestamp } from '@/lib/utils'
3030
import { CASE_STATUS, PATH } from '@/lib/constants'
3131

32-
import { getReport } from '@/server/actions/report'
33-
import type { ReportModiefied } from '@/server/types'
32+
import { getReport } from '@/server/actions'
33+
import type { ReportWithTimestamps } from '@/server/types/extended'
34+
import type { ReportModiefied } from '@/server/databases/types'
3435

3536
type Node = {
3637
id: string
@@ -106,9 +107,14 @@ const Page = () => {
106107

107108
useEffect(() => {
108109
getReport({ userId: userId as string, reportSlug: reportSlug as string })
109-
.then((data) => {
110-
const parsedData = data as ReportModiefied[]
111-
setReport(parsedData[0])
110+
.then((response) => {
111+
if (response.success && response.data) {
112+
// Get report from the response data
113+
const report = response.data as ReportWithTimestamps & ReportModiefied
114+
setReport(report)
115+
} else {
116+
console.error('Error fetching report for visualization:', response.error)
117+
}
112118
})
113119
.catch((err) => console.log(err))
114120
}, [userId, reportSlug])

0 commit comments

Comments
 (0)