Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Export Health Summary #27

Merged
merged 8 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//
'use client'
import { kebabCase } from 'es-toolkit'
import { Download } from 'lucide-react'
import { useState } from 'react'
import { callables } from '@/modules/firebase/clientApp'

Check warning on line 12 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L9-L12

Added lines #L9 - L12 were not covered by tests
import { type ResourceType } from '@/modules/firebase/utils'
import { Button } from '@/packages/design-system/src/components/Button'
import { toast } from '@/packages/design-system/src/components/Toaster'
import { Tooltip } from '@/packages/design-system/src/components/Tooltip'
import {

Check warning on line 17 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L14-L17

Added lines #L14 - L17 were not covered by tests
base64ToBlob,
downloadFile,
} from '@/packages/design-system/src/utils/file'

interface GenerateHealthSummaryProps {
userId: string
userName: string
resourceType: ResourceType
}

export const GenerateHealthSummary = ({

Check warning on line 28 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L28

Added line #L28 was not covered by tests
userId,
resourceType,
userName,
}: GenerateHealthSummaryProps) => {
const [isPending, setIsPending] = useState(false)

Check warning on line 33 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L32-L33

Added lines #L32 - L33 were not covered by tests

const downloadHealthSummary = async () => {
setIsPending(true)
try {
const exportHealthPromise = callables.exportHealthSummary({ userId })
toast.promise(exportHealthPromise, {

Check warning on line 39 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L35-L39

Added lines #L35 - L39 were not covered by tests
loading: `Generating health summary for ${userName}...`,
success: `Health summary for ${userName} has been downloaded.`,
error: `Generating health summary for ${userName} failed. Please try later.`,
})
const response = await exportHealthPromise
const blob = base64ToBlob(response.data.content, 'application/pdf')
downloadFile(blob, `health-summary-${kebabCase(userName)}.pdf`)

Check warning on line 46 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L44-L46

Added lines #L44 - L46 were not covered by tests
} finally {
setIsPending(false)

Check warning on line 48 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L48

Added line #L48 was not covered by tests
}
}

return (

Check warning on line 52 in app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/GenerateHealthSummary.tsx#L52

Added line #L52 was not covered by tests
<Tooltip
open={resourceType === 'invitation' ? undefined : false}
tooltip="This user has not logged in to the application yet"
>
<Button
type="submit"
disabled={resourceType === 'invitation'}
onClick={downloadHealthSummary}
className="disabled:pointer-events-auto"
isPending={isPending}
>
<Download />
Export Health Summary
</Button>
</Tooltip>
)
}
12 changes: 11 additions & 1 deletion app/(dashboard)/patients/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
} from '@/packages/design-system/src/components/Tabs'
import { getUserName } from '@/packages/design-system/src/modules/auth/user'
import { PageTitle } from '@/packages/design-system/src/molecules/DashboardLayout'
import { GenerateHealthSummary } from './GenerateHealthSummary'

Check warning on line 41 in app/(dashboard)/patients/[id]/page.tsx

View check run for this annotation

Codecov / codecov/patch

app/(dashboard)/patients/[id]/page.tsx#L41

Added line #L41 was not covered by tests
import { DashboardLayout } from '../../DashboardLayout'
import { Medications, type MedicationsFormSchema } from '../Medications'

Expand Down Expand Up @@ -148,15 +149,24 @@
})
}

const userName = getUserName(authUser) ?? ''

return (
<DashboardLayout
title={
<PageTitle
title="Edit patient"
subTitle={getUserName(authUser)}
subTitle={userName}
icon={<Contact />}
/>
}
actions={
<GenerateHealthSummary
userId={userId}
resourceType={resourceType}
userName={userName}
/>
}
>
<Tabs defaultValue={Tab.information}>
<TabsList className="mb-6 w-full">
Expand Down
9 changes: 8 additions & 1 deletion modules/firebase/clientApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
// SPDX-License-Identifier: MIT
//
import { initializeApp } from '@firebase/app'
import { connectFunctionsEmulator, getFunctions } from '@firebase/functions'

Check warning on line 9 in modules/firebase/clientApp.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/clientApp.ts#L9

Added line #L9 was not covered by tests
import { OAuthProvider, getAuth, connectAuthEmulator } from 'firebase/auth'
import { env } from '@/env'
import { getCallables } from '@/modules/firebase/utils'

Check warning on line 12 in modules/firebase/clientApp.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/clientApp.ts#L12

Added line #L12 was not covered by tests
import { firebaseConfig } from './config'

export const app = initializeApp(firebaseConfig)
Expand All @@ -18,8 +20,13 @@
}

export const auth = getAuth()
if (env.NEXT_PUBLIC_EMULATOR && !auth.emulatorConfig) {
const enableEmulation = env.NEXT_PUBLIC_EMULATOR && !auth.emulatorConfig
if (enableEmulation) {
connectAuthEmulator(auth, 'http://127.0.0.1:9099', {
disableWarnings: true,
})
}
const functions = getFunctions(app)

Check warning on line 29 in modules/firebase/clientApp.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/clientApp.ts#L29

Added line #L29 was not covered by tests
if (enableEmulation) connectFunctionsEmulator(functions, '127.0.0.1', 5001)

export const callables = getCallables(functions)

Check warning on line 32 in modules/firebase/clientApp.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/clientApp.ts#L32

Added line #L32 was not covered by tests
6 changes: 6 additions & 0 deletions modules/firebase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ export const getCallables = (functions: Functions) => ({
},
undefined
>(functions, 'updateUserInformation'),
exportHealthSummary: httpsCallable<
{
userId: string
},
{ content: string }
>(functions, 'exportHealthSummary'),
})

export const getDocData = async <T>(reference: DocumentReference<T>) => {
Expand Down
8 changes: 7 additions & 1 deletion modules/user/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@
}
const invitation = await getDocData(docRefs.invitation(userId))
return {
user: invitation?.user,
user:
invitation?.user ?
{
...invitation.user,
invitationCode: invitation.id,
}
: undefined,

Check warning on line 102 in modules/user/queries.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L102

Added line #L102 was not covered by tests
authUser:
invitation?.auth ?
{
Expand Down
4 changes: 3 additions & 1 deletion packages/design-system/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
{...props}
>
{isPending !== undefined ?
<ButtonPending isPending={isPending}>{children}</ButtonPending>
<ButtonPending size={size} isPending={isPending}>
{children}
</ButtonPending>
: children}
</Comp>
)
Expand Down
23 changes: 19 additions & 4 deletions packages/design-system/src/components/Button/ButtonPending.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import { Loader2 } from 'lucide-react'
import { forwardRef, type HTMLAttributes } from 'react'
import { cn } from '../../utils/className'
import type { ButtonProps } from '../Button'

interface ButtonPendingProps extends HTMLAttributes<HTMLSpanElement> {
interface ButtonPendingProps
extends HTMLAttributes<HTMLSpanElement>,
Pick<ButtonProps, 'size'> {
isPending?: boolean
}

Expand All @@ -18,8 +21,12 @@ interface ButtonPendingProps extends HTMLAttributes<HTMLSpanElement> {
* It's separated from Button to prevent redundant markup when unnecessary
* */
export const ButtonPending = forwardRef<HTMLSpanElement, ButtonPendingProps>(
({ children, isPending, className, ...props }, ref) => (
<span className={cn('relative', className)} ref={ref} {...props}>
({ children, isPending, className, size, ...props }, ref) => (
<span
className={cn('inline-flex-center relative', className)}
ref={ref}
{...props}
>
{isPending && (
<div
className="absolute -top-0.5 left-1/2 -translate-x-1/2"
Expand All @@ -29,7 +36,15 @@ export const ButtonPending = forwardRef<HTMLSpanElement, ButtonPendingProps>(
<Loader2 className="animate-spin" />
</div>
)}
<span className={cn(isPending && 'invisible')}>{children}</span>
<span
className={cn(
'inline-flex-center',
size === 'lg' ? 'gap-2.5' : 'gap-2',
isPending && 'invisible ',
)}
>
{children}
</span>
</span>
),
)
Expand Down
44 changes: 44 additions & 0 deletions packages/design-system/src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project
//
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

export const downloadFile = (

Check warning on line 9 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L9

Added line #L9 was not covered by tests
src: File | Blob | MediaSource,
fileName: string,
) => {
const url = URL.createObjectURL(src)

Check warning on line 13 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L12-L13

Added lines #L12 - L13 were not covered by tests

const link = document.createElement('a')
link.href = url
link.download = fileName
link.click()

Check warning on line 18 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L15-L18

Added lines #L15 - L18 were not covered by tests

URL.revokeObjectURL(url)

Check warning on line 20 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L20

Added line #L20 was not covered by tests
}

export const base64ToBlob = (

Check warning on line 23 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L23

Added line #L23 was not covered by tests
base64Data: string,
contentType = '',
sliceSize = 512,
) => {
const byteCharacters = atob(base64Data)
const byteArrays = []

Check warning on line 29 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L27-L29

Added lines #L27 - L29 were not covered by tests

for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize)

Check warning on line 32 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L31-L32

Added lines #L31 - L32 were not covered by tests

const byteNumbers = new Array(slice.length)
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i)

Check warning on line 36 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L34-L36

Added lines #L34 - L36 were not covered by tests
}

const byteArray = new Uint8Array(byteNumbers)
byteArrays.push(byteArray)

Check warning on line 40 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L39-L40

Added lines #L39 - L40 were not covered by tests
}

return new Blob(byteArrays, { type: contentType })

Check warning on line 43 in packages/design-system/src/utils/file.ts

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/utils/file.ts#L43

Added line #L43 was not covered by tests
}
Loading