33import { CreatePolicySheet } from '@/components/sheets/create-policy-sheet' ;
44import { api } from '@/lib/api-client' ;
55import { Add , Download } from '@carbon/icons-react' ;
6- import type { AuditLog , Member , Organization , Policy , User } from '@db' ;
6+ import type { Policy } from '@db' ;
77import { Button , HStack } from '@trycompai/design-system' ;
88import { usePathname , useRouter , useSearchParams } from 'next/navigation' ;
99import { useState } from 'react' ;
10+ import { toast } from 'sonner' ;
1011import { usePermissions } from '@/hooks/use-permissions' ;
1112
12- type AuditLogWithRelations = AuditLog & {
13- user : User | null ;
14- member : Member | null ;
15- organization : Organization ;
16- } ;
17-
1813interface PolicyPageActionsProps {
1914 policies : Policy [ ] ;
2015}
@@ -29,21 +24,23 @@ export function PolicyPageActions({ policies }: PolicyPageActionsProps) {
2924 const handleDownloadAll = async ( ) => {
3025 setIsDownloadingAll ( true ) ;
3126 try {
32- const logsEntries = await Promise . all (
33- policies . map ( async ( policy ) => {
34- const res = await api . get < { data : AuditLogWithRelations [ ] } > (
35- `/v1/audit-logs?entityType=policy&entityId=${ policy . id } ` ,
36- ) ;
37- const allLogs = Array . isArray ( res . data ?. data ) ? res . data . data : [ ] ;
38- const approvalLogs = allLogs . filter ( ( log ) =>
39- log . description ?. toLowerCase ( ) . includes ( 'published' ) ,
40- ) ;
41- return [ policy . id , approvalLogs ] as const ;
42- } ) ,
27+ const res = await api . get < { downloadUrl : string ; name : string ; policyCount : number } > (
28+ '/v1/policies/download-all' ,
4329 ) ;
44- const policyLogs = Object . fromEntries ( logsEntries ) ;
45- const { downloadAllPolicies } = await import ( '@/lib/pdf-generator' ) ;
46- await downloadAllPolicies ( policies , policyLogs ) ;
30+
31+ if ( res . error || ! res . data ?. downloadUrl ) {
32+ toast . error ( 'Failed to generate PDF. Please try again.' ) ;
33+ return ;
34+ }
35+
36+ const link = document . createElement ( 'a' ) ;
37+ link . href = res . data . downloadUrl ;
38+ link . download = `${ res . data . name ?? 'all-policies' } .pdf` ;
39+ document . body . appendChild ( link ) ;
40+ link . click ( ) ;
41+ document . body . removeChild ( link ) ;
42+ } catch {
43+ toast . error ( 'Failed to download policies.' ) ;
4744 } finally {
4845 setIsDownloadingAll ( false ) ;
4946 }
0 commit comments