@@ -6,60 +6,88 @@ import type { Member } from '@comp/db/types';
66import { headers } from 'next/headers' ;
77import { redirect } from 'next/navigation' ;
88import { OrganizationDashboard } from './components/OrganizationDashboard' ;
9+ import type { FleetPolicy , Host } from './types' ;
910
1011export default async function OrganizationPage ( { params } : { params : Promise < { orgId : string } > } ) {
11- const { orgId } = await params ;
12+ try {
13+ const { orgId } = await params ;
1214
13- const session = await auth . api . getSession ( {
14- headers : await headers ( ) ,
15- } ) ;
15+ const session = await auth . api . getSession ( {
16+ headers : await headers ( ) ,
17+ } ) ;
1618
17- if ( ! session ?. user ) {
18- redirect ( '/login' ) ; // Or appropriate login/auth route
19- }
19+ if ( ! session ?. user ) {
20+ redirect ( '/login' ) ; // Or appropriate login/auth route
21+ }
2022
21- const member = await db . member . findFirst ( {
22- where : {
23- userId : session . user . id ,
24- organizationId : orgId ,
25- } ,
26- include : {
27- user : true ,
28- organization : true , // Include organization details
29- } ,
30- } ) ;
23+ let member = null ;
3124
32- if ( ! member ) {
33- redirect ( '/' ) ; // Or appropriate login/auth route
34- }
25+ try {
26+ member = await db . member . findFirst ( {
27+ where : {
28+ userId : session . user . id ,
29+ organizationId : orgId ,
30+ } ,
31+ include : {
32+ user : true ,
33+ organization : true , // Include organization details
34+ } ,
35+ } ) ;
36+ } catch ( error ) {
37+ console . error ( 'Error fetching member:' , error ) ;
38+ // Return a fallback UI or redirect to error page
39+ redirect ( '/' ) ;
40+ }
41+
42+ if ( ! member ) {
43+ redirect ( '/' ) ; // Or appropriate login/auth route
44+ }
45+
46+ const { fleetPolicies, device } = await getFleetPolicies ( member ) ;
3547
36- const { fleetPolicies, device } = await getFleetPolicies ( member ) ;
37- const isFleetEnabled = await getPostHogClient ( ) ?. isFeatureEnabled (
38- 'is-fleet-enabled' ,
39- session ?. user . id ,
40- ) ;
48+ let isFleetEnabled = false ;
49+ try {
50+ const postHogClient = await getPostHogClient ( ) ;
51+ isFleetEnabled =
52+ ( await postHogClient ?. isFeatureEnabled ( 'is-fleet-enabled' , session ?. user . id ) ) ?? false ;
53+ } catch ( error ) {
54+ console . error ( 'Error checking fleet feature flag:' , error ) ;
55+ // Default to false if there's an error
56+ }
4157
42- return (
43- < OrganizationDashboard
44- key = { orgId } // Use organizationId as key
45- organizationId = { orgId }
46- member = { member }
47- fleetPolicies = { fleetPolicies }
48- host = { device }
49- isFleetEnabled = { isFleetEnabled ?? false }
50- />
51- ) ;
58+ return (
59+ < OrganizationDashboard
60+ key = { orgId } // Use organizationId as key
61+ organizationId = { orgId }
62+ member = { member }
63+ fleetPolicies = { fleetPolicies }
64+ host = { device }
65+ isFleetEnabled = { isFleetEnabled }
66+ />
67+ ) ;
68+ } catch ( error ) {
69+ console . error ( 'Error in OrganizationPage:' , error ) ;
70+ // Redirect to a safe page if there's an unexpected error
71+ redirect ( '/' ) ;
72+ }
5273}
5374
54- const getFleetPolicies = async ( member : Member ) => {
75+ const getFleetPolicies = async (
76+ member : Member ,
77+ ) : Promise < { fleetPolicies : FleetPolicy [ ] ; device : Host | null } > => {
5578 const deviceLabelId = member . fleetDmLabelId ;
5679 const fleet = await getFleetInstance ( ) ;
5780
5881 try {
5982 const deviceResponse = await fleet . get ( `/labels/${ deviceLabelId } /hosts` ) ;
60- const device = deviceResponse . data . hosts [ 0 ] ; // There should only be one device per label.
83+ const device : Host | undefined = deviceResponse . data . hosts [ 0 ] ; // There should only be one device per label.
84+
85+ if ( ! device ) {
86+ return { fleetPolicies : [ ] , device : null } ;
87+ }
88+
6189 const deviceWithPolicies = await fleet . get ( `/hosts/${ device . id } ` ) ;
62- const fleetPolicies = deviceWithPolicies . data . host . policies ;
90+ const fleetPolicies : FleetPolicy [ ] = deviceWithPolicies . data . host . policies ;
6391 return { fleetPolicies, device } ;
6492 } catch ( error ) {
6593 console . error ( error ) ;
0 commit comments