|
1 | | -import { db } from '@sim/db' |
2 | | -import { templateCreators, templateStars, templates } from '@sim/db/schema' |
3 | | -import { and, eq } from 'drizzle-orm' |
4 | | -import { notFound } from 'next/navigation' |
5 | | -import { getSession } from '@/lib/auth' |
6 | | -import { createLogger } from '@/lib/logs/console/logger' |
7 | 1 | import TemplateDetails from '@/app/workspace/[workspaceId]/templates/[id]/template' |
8 | 2 |
|
9 | | -const logger = createLogger('TemplatePage') |
10 | | - |
11 | | -interface TemplatePageProps { |
12 | | - params: Promise<{ |
13 | | - workspaceId: string |
14 | | - id: string |
15 | | - }> |
16 | | -} |
17 | | - |
18 | | -export default async function TemplatePage({ params }: TemplatePageProps) { |
19 | | - const { workspaceId, id } = await params |
20 | | - |
21 | | - try { |
22 | | - if (!id || typeof id !== 'string' || id.length !== 36) { |
23 | | - notFound() |
24 | | - } |
25 | | - |
26 | | - const session = await getSession() |
27 | | - |
28 | | - const templateData = await db |
29 | | - .select({ |
30 | | - template: templates, |
31 | | - creator: templateCreators, |
32 | | - }) |
33 | | - .from(templates) |
34 | | - .leftJoin(templateCreators, eq(templates.creatorId, templateCreators.id)) |
35 | | - .where(eq(templates.id, id)) |
36 | | - .limit(1) |
37 | | - |
38 | | - if (templateData.length === 0) { |
39 | | - notFound() |
40 | | - } |
41 | | - |
42 | | - const { template, creator } = templateData[0] |
43 | | - |
44 | | - if (!session?.user?.id && template.status !== 'approved') { |
45 | | - notFound() |
46 | | - } |
47 | | - |
48 | | - if (!template.id || !template.name) { |
49 | | - logger.error('Template missing required fields:', { |
50 | | - id: template.id, |
51 | | - name: template.name, |
52 | | - }) |
53 | | - notFound() |
54 | | - } |
55 | | - |
56 | | - let isStarred = false |
57 | | - if (session?.user?.id) { |
58 | | - try { |
59 | | - const starData = await db |
60 | | - .select({ id: templateStars.id }) |
61 | | - .from(templateStars) |
62 | | - .where( |
63 | | - and( |
64 | | - eq(templateStars.templateId, template.id), |
65 | | - eq(templateStars.userId, session.user.id) |
66 | | - ) |
67 | | - ) |
68 | | - .limit(1) |
69 | | - isStarred = starData.length > 0 |
70 | | - } catch { |
71 | | - isStarred = false |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - const serializedTemplate = { |
76 | | - ...template, |
77 | | - creator: creator || null, |
78 | | - createdAt: template.createdAt.toISOString(), |
79 | | - updatedAt: template.updatedAt.toISOString(), |
80 | | - isStarred, |
81 | | - } |
82 | | - |
83 | | - return ( |
84 | | - <TemplateDetails |
85 | | - template={JSON.parse(JSON.stringify(serializedTemplate))} |
86 | | - workspaceId={workspaceId} |
87 | | - currentUserId={session?.user?.id || null} |
88 | | - /> |
89 | | - ) |
90 | | - } catch (error) { |
91 | | - logger.error('Error loading template:', error) |
92 | | - return ( |
93 | | - <div className='flex h-[100vh] items-center justify-center pl-64'> |
94 | | - <div className='text-center'> |
95 | | - <h1 className='mb-[14px] font-medium text-[18px]'>Error Loading Template</h1> |
96 | | - <p className='text-[#888888] text-[14px]'>There was an error loading this template.</p> |
97 | | - <p className='mt-[10px] text-[#888888] text-[12px]'>Template ID: {id}</p> |
98 | | - <p className='mt-[10px] text-[12px] text-red-500'> |
99 | | - {error instanceof Error ? error.message : 'Unknown error'} |
100 | | - </p> |
101 | | - </div> |
102 | | - </div> |
103 | | - ) |
104 | | - } |
| 3 | +/** |
| 4 | + * Workspace-scoped template detail page. |
| 5 | + * Data fetching is handled client-side in the TemplateDetails component. |
| 6 | + */ |
| 7 | +export default function TemplatePage() { |
| 8 | + return <TemplateDetails /> |
105 | 9 | } |
0 commit comments