// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnchorProps } from '@mantine/core';
import { Anchor } from '@mantine/core';
import { showNotification } from '@mantine/notifications';
import { ensureTrailingSlash, locationUtils, normalizeErrorString } from '@medplum/core';
import type { ClientApplication, Encounter, Patient, Reference, SmartAppLaunch } from '@medplum/fhirtypes';
import { useMedplum } from '@medplum/react-hooks';
import type { JSX, ReactNode } from 'react';
export interface SmartAppLaunchLinkProps extends AnchorProps {
readonly client: ClientApplication;
readonly patient?: Reference<Patient>;
readonly encounter?: Reference<Encounter>;
readonly children?: ReactNode;
}
export function SmartAppLaunchLink(props: SmartAppLaunchLinkProps): JSX.Element | null {
const medplum = useMedplum();
const { client, patient, encounter, children, ...rest } = props;
function launchApp(): void {
medplum
.createResource<SmartAppLaunch>({
resourceType: 'SmartAppLaunch',
patient,
encounter,
})
.then((result) => {
const url = new URL(client.launchUri as string);
url.searchParams.set('iss', ensureTrailingSlash(medplum.fhirUrl().toString()));
url.searchParams.set('launch', result.id as string);
locationUtils.assign(url.toString());
})
.catch((err) => showNotification({ color: 'red', message: normalizeErrorString(err), autoClose: false }));
}
return (
<Anchor onClick={() => launchApp()} {...rest}>
{children}
</Anchor>
);
}