Skip to content

Commit b1d9e86

Browse files
authored
feat: add env vars for ai and rename activities to logs (#452)
1 parent 381fe1f commit b1d9e86

17 files changed

Lines changed: 354 additions & 120 deletions

File tree

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ import { Icon } from '@/components/ui/icon'
4747
import { Skeleton } from '@/components/ui/skeleton'
4848

4949
import { readableTimestamp, formatToReadable, cn } from '@/lib/utils'
50-
import { ACTIVITIES_TYPES, deviceIcons } from '@/lib/constants'
50+
import { LOG_TYPES, deviceIcons } from '@/lib/constants'
5151

52-
import { getAllActivities, countActivities } from '@/server/actions/activity'
53-
import type { ActivitySelect } from '@/server/types'
52+
import { getAllLogs, countLogs } from '@/server/actions/log'
53+
import type { LogSelect } from '@/server/types'
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 [activities, setActivities] = useState<ActivitySelect[]>([])
61+
const [logs, setLogs] = useState<LogSelect[]>([])
6262
const [isLoading, setIsLoading] = useState<boolean>(true)
6363
const [searchTerm, setSearchTerm] = useState<string>('')
6464
const [currentPage, setCurrentPage] = useState<number>(1)
@@ -79,26 +79,26 @@ const Page = () => {
7979
setDate(undefined)
8080
}
8181

82-
const fetchActivities = useCallback(() => {
83-
getAllActivities({ userId: userId as string, currentPage, searchTerm, itemsPerPage })
82+
const fetchLogs = useCallback(() => {
83+
getAllLogs({ userId: userId as string, currentPage, searchTerm, itemsPerPage })
8484
.then((data) => {
85-
setActivities(data)
86-
countActivities({ userId: userId as string }).then((count) => setTotal(count[0].count))
85+
setLogs(data)
86+
countLogs({ userId: userId as string }).then((count) => setTotal(count[0].count))
8787
})
8888
.catch((err) => console.log(err))
8989
.finally(() => setIsLoading(false))
9090
}, [userId, currentPage, searchTerm])
9191

9292
useEffect(() => {
93-
fetchActivities()
93+
fetchLogs()
9494
// eslint-disable-next-line react-hooks/exhaustive-deps
9595
}, [userId, currentPage, searchTerm, filters])
9696

9797
useEffect(() => {
9898
if (Array.isArray(events) && events.length > 0) {
9999
for (const event of events) {
100100
if (event.type === EventTypes.REPORT_IMPORTED) {
101-
fetchActivities()
101+
fetchLogs()
102102
}
103103
}
104104
}
@@ -110,7 +110,7 @@ const Page = () => {
110110
<section className='w-full mx-auto pb-12 md:pb-16 lg:pb-20 gap-6 flex flex-col'>
111111
<Card className='w-full'>
112112
<CardHeader>
113-
<CardDescription>View and filter activity actions</CardDescription>
113+
<CardDescription>View and filter log actions</CardDescription>
114114
</CardHeader>
115115
<CardContent>
116116
<div className='w-full flex flex-col gap-6'>
@@ -121,7 +121,7 @@ const Page = () => {
121121
</div>
122122
<Input
123123
type='text'
124-
placeholder='Search activity logs...'
124+
placeholder='Search logs...'
125125
value={searchTerm}
126126
onChange={(e) => setSearchTerm(e.target.value)}
127127
className='pl-10 pr-4 py-2 rounded-md w-full border border-input bg-background focus:outline-none focus:ring-1 focus:ring-primary focus:border-primary'
@@ -150,7 +150,7 @@ const Page = () => {
150150
</SelectTrigger>
151151
<SelectContent>
152152
<SelectGroup>
153-
{ACTIVITIES_TYPES.map((type: string) => (
153+
{LOG_TYPES.map((type: string) => (
154154
<SelectItem key={type} value={type}>
155155
{formatToReadable(type)}
156156
</SelectItem>
@@ -210,7 +210,7 @@ const Page = () => {
210210
</DropdownMenu>
211211
</div>
212212
<div className='overflow-x-auto'>
213-
<ActivitiesTable isLoading={isLoading} activities={activities} />
213+
<ActivitiesTable isLoading={isLoading} logs={logs} />
214214
</div>
215215
<div className='flex items-center justify-between mt-6'>
216216
<ActivitiesTablePagination
@@ -227,18 +227,12 @@ const Page = () => {
227227
)
228228
}
229229

230-
const ActivitiesTable = ({
231-
isLoading,
232-
activities
233-
}: {
234-
isLoading: boolean
235-
activities: ActivitySelect[]
236-
}) => {
230+
const ActivitiesTable = ({ isLoading, logs }: { isLoading: boolean; logs: LogSelect[] }) => {
237231
return (
238232
<Table>
239233
<TableHeader>
240234
<TableRow>
241-
<TableHead>Activity Type</TableHead>
235+
<TableHead>Type</TableHead>
242236
<TableHead>Description</TableHead>
243237
<TableHead>Timestamp</TableHead>
244238
<TableHead>Device</TableHead>
@@ -249,16 +243,14 @@ const ActivitiesTable = ({
249243
<SkeletonLoader />
250244
) : (
251245
<>
252-
{activities.map((activity) => (
253-
<TableRow key={activity.id}>
254-
<TableCell>{formatToReadable(activity.type)}</TableCell>
255-
<TableCell>{activity.description}</TableCell>
256-
<TableCell>{readableTimestamp(activity.created.toString())}</TableCell>
246+
{logs.map((log) => (
247+
<TableRow key={log.id}>
248+
<TableCell>{formatToReadable(log.type)}</TableCell>
249+
<TableCell>{log.description}</TableCell>
250+
<TableCell>{readableTimestamp(log.created.toString())}</TableCell>
257251
<TableCell>
258252
<Icon
259-
name={
260-
deviceIcons[activity.device as keyof typeof deviceIcons] as keyof typeof icons
261-
}
253+
name={deviceIcons[log.device as keyof typeof deviceIcons] as keyof typeof icons}
262254
size={20}
263255
/>
264256
</TableCell>

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import { readableTimestamp } from '@/lib/utils'
2424
import { months, deviceIcons } from '@/lib/constants'
2525

2626
import { getLastThreeReport } from '@/server/actions/report'
27-
import { getLastThreeActivity } from '@/server/actions/activity'
27+
import { getLastThreeLogs } from '@/server/actions/log'
2828
import { getCurrentYearMetrics } from '@/server/actions/metric'
29-
import type { ReportSelect, ActivitySelect } from '@/server/types'
29+
import type { ReportSelect, LogSelect } from '@/server/types'
3030

3131
import { useEvents, EventTypes } from '@/hooks/use-events'
3232

@@ -40,15 +40,15 @@ const Page = () => {
4040
const [isLoading, setIsLoading] = useState<boolean>(true)
4141
const [reports, setReports] = useState<ReportSelect[]>([])
4242
const [metrics, setMetrics] = useState<ChartDataItem[]>()
43-
const [activities, setActivities] = useState<ActivitySelect[]>([])
43+
const [logs, setLogs] = useState<LogSelect[]>([])
4444

4545
const currentYear = new Date().getFullYear()
4646

4747
const getOverviewData = useCallback(() => {
4848
Promise.all([
4949
getCurrentYearMetrics({ userId: userId as string, currentYear }),
5050
getLastThreeReport({ userId: userId as string }),
51-
getLastThreeActivity({ userId: userId as string })
51+
getLastThreeLogs({ userId: userId as string })
5252
])
5353
.then(([metricsData, reportsData, activitiesData]) => {
5454
const data = months.map((name) => ({
@@ -65,7 +65,7 @@ const Page = () => {
6565

6666
setMetrics(data as ChartDataItem[])
6767
setReports(reportsData as ReportSelect[])
68-
setActivities(activitiesData as ActivitySelect[])
68+
setLogs(activitiesData as LogSelect[])
6969
})
7070
.catch((err) => console.log(err))
7171
.finally(() => setIsLoading(false))
@@ -160,16 +160,16 @@ const Page = () => {
160160
</TableRow>
161161
</TableHeader>
162162
<TableBody>
163-
{activities.map((activity) => (
164-
<TableRow key={activity.id}>
165-
<TableCell>{activity.type}</TableCell>
166-
<TableCell>{activity.description}</TableCell>
167-
<TableCell>{readableTimestamp(activity.created.toString())}</TableCell>
163+
{logs.map((log) => (
164+
<TableRow key={log.id}>
165+
<TableCell>{log.type}</TableCell>
166+
<TableCell>{log.description}</TableCell>
167+
<TableCell>{readableTimestamp(log.created.toString())}</TableCell>
168168
<TableCell>
169169
<Icon
170170
name={
171171
deviceIcons[
172-
activity.device as keyof typeof deviceIcons
172+
log.device as keyof typeof deviceIcons
173173
] as keyof typeof icons
174174
}
175175
size={20}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Page = () => {
2+
return <div>Ask AI</div>
3+
}
4+
5+
export default Page

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip
4646
import { readableTimestamp } from '@/lib/utils'
4747

4848
import { getAllReport, countReports, deleteReport } from '@/server/actions/report'
49-
import { saveActivity } from '@/server/actions/activity'
49+
import { saveLog } from '@/server/actions/log'
5050
import type { ReportSelect } from '@/server/types'
5151

5252
import { useEvents, EventTypes } from '@/hooks/use-events'
@@ -90,7 +90,7 @@ const Page = () => {
9090

9191
const device = (await axios.get('/api/device')).data.device
9292

93-
await saveActivity({
93+
await saveLog({
9494
type: 'report_deleted',
9595
description: 'Report deleted',
9696
userId: userId as string,

src/app/api/clerk/webhooks/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { WebhookEvent } from '@clerk/nextjs/server'
44

55
import { getDeviceType } from '@/lib/utils'
66

7-
import { saveActivity } from '@/server/actions/activity'
7+
import { saveLog } from '@/server/actions/log'
88

99
export const POST = async (req: Request) => {
1010
const CLERK_WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET
@@ -44,7 +44,7 @@ export const POST = async (req: Request) => {
4444
}
4545

4646
if (evt.type === 'user.created') {
47-
await saveActivity({
47+
await saveLog({
4848
type: 'account_created',
4949
description: 'User account created',
5050
userId: evt.data.id as string,
@@ -53,7 +53,7 @@ export const POST = async (req: Request) => {
5353
}
5454

5555
if (evt.type === 'session.created') {
56-
await saveActivity({
56+
await saveLog({
5757
type: 'login',
5858
description: 'Account logged in',
5959
userId: evt.data.user_id as string,
@@ -62,7 +62,7 @@ export const POST = async (req: Request) => {
6262
}
6363

6464
if (evt.type === 'session.removed') {
65-
await saveActivity({
65+
await saveLog({
6666
type: 'logout',
6767
description: 'Account logged out',
6868
userId: evt.data.user_id as string,

src/components/mods/studio/import-report.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { TFileContent } from '@/lib/definitions'
2525
import { isValidFileContent } from '@/lib/utils'
2626

2727
import { saveReport } from '@/server/actions/report'
28-
import { saveActivity } from '@/server/actions/activity'
28+
import { saveLog } from '@/server/actions/log'
2929
import { saveMetric, getMetric, updateMetric } from '@/server/actions/metric'
3030
import type { ReportInferType } from '@/server/types'
3131

@@ -106,7 +106,7 @@ export const ImportReport = ({ children }: { children: React.ReactElement }) =>
106106

107107
const device = (await axios.get('/api/device')).data.device
108108

109-
await saveActivity({
109+
await saveLog({
110110
type: 'report_imported',
111111
description: 'Report imported',
112112
userId: userId as string,

src/components/mods/studio/studio-sidebar.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
LifeBuoy,
1414
SquareStack,
1515
FileText,
16-
Shield
16+
Shield,
17+
Logs
1718
} from 'lucide-react'
1819
import { useUser, useAuth, UserButton } from '@clerk/nextjs'
1920
import { usePathname, useParams } from 'next/navigation'
@@ -42,6 +43,7 @@ import { Button } from '@/components/ui/button'
4243
import { ToggleTheme } from '@/components/toogle-theme'
4344
import { Icon } from '@/components/ui/icon'
4445
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'
46+
import { Badge } from '@/components/ui/badge'
4547

4648
import { MenuType, TContentCase } from '@/lib/definitions'
4749
import { readableTimestamp } from '@/lib/utils'
@@ -62,9 +64,9 @@ const menus = [
6264
icon: 'Files'
6365
},
6466
{
65-
label: 'Activities',
66-
path: '/studio/activities',
67-
icon: 'Activity'
67+
label: 'Ask AI',
68+
path: '/studio/reports/ask',
69+
icon: 'Sparkles'
6870
}
6971
] as MenuType[]
7072

@@ -222,6 +224,22 @@ export const StudioSidebar = () => {
222224
</SidebarGroup>
223225
) : null}
224226

227+
<SidebarGroup>
228+
<SidebarGroupLabel>Activities</SidebarGroupLabel>
229+
<SidebarGroupContent>
230+
<SidebarMenu>
231+
<SidebarMenuItem>
232+
<SidebarMenuButton asChild>
233+
<Link href={'/studio/activities/logs'}>
234+
<Logs className='h-4 w-4' />
235+
<span>Logs</span>
236+
</Link>
237+
</SidebarMenuButton>
238+
</SidebarMenuItem>
239+
</SidebarMenu>
240+
</SidebarGroupContent>
241+
</SidebarGroup>
242+
225243
<SidebarGroup>
226244
<SidebarGroupLabel>Help</SidebarGroupLabel>
227245
<SidebarGroupContent>
@@ -298,11 +316,11 @@ export const StudioSidebar = () => {
298316

299317
<SidebarMenuItem>
300318
<SidebarMenuButton asChild size={'lg'}>
301-
<div className='flex items-center'>
319+
<div className='flex items-center gap-4 w-full'>
302320
<UserButton />
303-
<div className='flex flex-col gap-1'>
304-
<span>{user?.firstName}</span>
305-
<span className='text-xs'>{user?.emailAddresses[0].emailAddress}</span>
321+
<div className='flex items-center justify-between w-full'>
322+
<span>{user?.firstName?.split(' ')[0]}</span>
323+
<Badge variant='outline'>Free</Badge>
306324
</div>
307325
</div>
308326
</SidebarMenuButton>

src/env.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ declare global {
1010
CLERK_SECRET_KEY: string
1111
CLERK_WEBHOOK_SECRET: string
1212
CRYPTO_PASSPHRASE: string
13+
14+
AI_TOKEN: string
15+
AI_BASE_URL: string
1316
}
1417
}
1518
}

src/lib/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const BASE_EVENT_TYPES = [
1010
'logout'
1111
] as string[]
1212

13-
export const ACTIVITIES_TYPES = BASE_EVENT_TYPES
13+
export const LOG_TYPES = BASE_EVENT_TYPES
1414

1515
export const EVENT_TYPES: { [key: string]: string } = {
1616
unknown: 'unknown',

0 commit comments

Comments
 (0)