import { css } from '@emotion/css'; import React from 'react'; import { useForm } from 'react-hook-form'; import { GrafanaTheme2, SelectableValue } from '@grafana/data/src'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors/src'; import { Button, ButtonGroup, Field, Input, InputControl, RadioButtonGroup, Spinner, useStyles2, } from '@grafana/ui/src'; import { useAddRecipientMutation, useDeleteRecipientMutation, useGetPublicDashboardQuery, useReshareAccessToRecipientMutation, useUpdatePublicDashboardMutation, } from 'app/features/dashboard/api/publicDashboardApi'; import { useSelector } from 'app/types'; import { PublicDashboard, PublicDashboardShareType, validEmailRegex } from '../SharePublicDashboardUtils'; interface EmailSharingConfigurationForm { shareType: PublicDashboardShareType; email: string; } const options: Array> = [ { label: 'Anyone with a link', value: PublicDashboardShareType.PUBLIC }, { label: 'Only specified people', value: PublicDashboardShareType.EMAIL }, ]; const selectors = e2eSelectors.pages.ShareDashboardModal.PublicDashboard.EmailSharingConfiguration; const EmailList = ({ recipients, dashboardUid, publicDashboardUid, }: { recipients: PublicDashboard['recipients']; dashboardUid: string; publicDashboardUid: string; }) => { const styles = useStyles2(getStyles); const [deleteEmail, { isLoading: isDeleteLoading }] = useDeleteRecipientMutation(); const [reshareAccess, { isLoading: isReshareLoading }] = useReshareAccessToRecipientMutation(); const isLoading = isDeleteLoading || isReshareLoading; const onDeleteEmail = (recipientUid: string) => { deleteEmail({ recipientUid, dashboardUid: dashboardUid, uid: publicDashboardUid }); }; const onReshare = (recipientUid: string) => { reshareAccess({ recipientUid, uid: publicDashboardUid }); }; return ( {recipients!.map((recipient) => ( ))}
{recipient.recipient}
); }; export const EmailSharingConfiguration = () => { const styles = useStyles2(getStyles); const dashboardState = useSelector((store) => store.dashboard); const dashboard = dashboardState.getModel()!; const { data: publicDashboard } = useGetPublicDashboardQuery(dashboard.uid); const [updateShareType] = useUpdatePublicDashboardMutation(); const [addEmail, { isLoading: isAddEmailLoading }] = useAddRecipientMutation(); const { register, setValue, control, watch, handleSubmit, formState: { errors }, reset, } = useForm({ defaultValues: { shareType: publicDashboard?.share || PublicDashboardShareType.PUBLIC, email: '', }, mode: 'onSubmit', }); const onShareTypeChange = (shareType: PublicDashboardShareType) => { const req = { dashboard, payload: { ...publicDashboard!, share: shareType, }, }; updateShareType(req); }; const onSubmit = async (data: EmailSharingConfigurationForm) => { await addEmail({ recipient: data.email, uid: publicDashboard!.uid, dashboardUid: dashboard.uid }).unwrap(); reset({ email: '', shareType: PublicDashboardShareType.EMAIL }); }; return (
{ const { ref, ...rest } = field; return ( { setValue('shareType', shareType); onShareTypeChange(shareType); }} /> ); }} /> {watch('shareType') === PublicDashboardShareType.EMAIL && ( <>
{!!publicDashboard?.recipients?.length && ( )} )} ); }; const getStyles = (theme: GrafanaTheme2) => ({ container: css` margin-bottom: ${theme.spacing(2)}; `, emailContainer: css` display: flex; gap: ${theme.spacing(1)}; `, emailInput: css` flex-grow: 1; `, table: css` display: flex; max-height: 220px; overflow-y: scroll; margin-bottom: ${theme.spacing(1)}; & tbody { display: flex; flex-direction: column; flex-grow: 1; } & tr { min-height: 40px; display: flex; align-items: center; justify-content: space-between; padding: ${theme.spacing(0.5, 1)}; :nth-child(odd) { background: ${theme.colors.background.secondary}; } } `, tableButtonsContainer: css` display: flex; justify-content: end; `, });