OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
import { zodResolver } from '@hookform/resolvers/zod';
import { zShareOverview } from '@openpanel/core/modules/share/share.constants';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from '@tanstack/react-router';
import { CheckCircle2, Copy, ExternalLink, TrashIcon } from 'lucide-react';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
import type { z } from 'zod';
import { popModal } from '.';
import { ModalContent, ModalHeader } from './Modal/Container';
import { Button } from '@/components/ui/button';
import { DialogFooter } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Tooltiper } from '@/components/ui/tooltip';
import { useAppParams } from '@/hooks/use-app-params';
import { handleError, useTRPC } from '@/integrations/trpc/react';
import {
getSharePasswordFieldValue,
getSharePasswordToSubmit,
STORED_PASSWORD_PLACEHOLDER,
} from '@/utils/share-password';
const validator = zShareOverview;
type IForm = z.infer<typeof validator>;
export default function ShareOverviewModal() {
const { projectId, organizationId } = useAppParams();
const navigate = useNavigate();
const [copied, setCopied] = useState(false);
const trpc = useTRPC();
const queryClient = useQueryClient();
// Fetch current share status
const shareQuery = useQuery(
trpc.share.overviewSettings.queryOptions({
projectId,
})
);
const existingShare = shareQuery.data;
const isShared = existingShare?.public ?? false;
const shareUrl = existingShare?.id
? `${window.location.origin}/share/overview/${existingShare.id}`
: '';
const {
register,
handleSubmit,
watch,
formState: { dirtyFields },
} = useForm<IForm>({
resolver: zodResolver(validator),
// `values` rather than `defaultValues`: the share settings arrive after
// the first render, and the field must then show the stored password.
values: {
public: true,
password: getSharePasswordFieldValue(existingShare?.hasPassword),
projectId,
organizationId,
},
resetOptions: { keepDirtyValues: true },
});
const password = watch('password');
const mutation = useMutation(
trpc.share.createOverview.mutationOptions({
onError: handleError,
onSuccess(res) {
queryClient.invalidateQueries(trpc.share.overviewSettings.pathFilter());
toast('Success', {
description: `Your overview is now ${
res.public ? 'public' : 'private'
}`,
action: res.public
? {
label: 'View',
onClick: () =>
navigate({
to: '/share/overview/$shareId',
params: {
shareId: res.id,
},
}),
}
: undefined,
});
popModal();
},
})
);
const handleCopyLink = () => {
navigator.clipboard.writeText(shareUrl);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
toast('Link copied to clipboard');
};
const handleMakePrivate = () => {
mutation.mutate({
public: false,
password: null,
projectId,
organizationId,
});
};
return (
<ModalContent className="max-w-md">
<ModalHeader
text={
isShared
? 'Your overview is currently public and can be accessed by anyone with the link.'
: 'You can choose if you want to add a password to make it a bit more private.'
}
title="Overview public availability"
/>
{isShared && (
<div className="space-y-3 rounded-lg border bg-def-100 p-4">
<div className="flex items-center gap-2 text-green-600 text-sm dark:text-green-400">
<CheckCircle2 className="size-4" />
<span className="font-medium">Currently shared</span>
</div>
<div className="flex items-center gap-1">
<Input className="flex-1 text-sm" readOnly value={shareUrl} />
<Tooltiper asChild content="Copy link">
<Button
onClick={handleCopyLink}
size="sm"
type="button"
variant="outline"
>
{copied ? (
<CheckCircle2 className="size-4" />
) : (
<Copy className="size-4" />
)}
</Button>
</Tooltiper>
<Tooltiper asChild content="Open in new tab">
<Button
onClick={() => window.open(shareUrl, '_blank')}
size="sm"
type="button"
variant="outline"
>
<ExternalLink className="size-4" />
</Button>
</Tooltiper>
<Tooltiper asChild content="Make private">
<Button
onClick={handleMakePrivate}
type="button"
variant="destructive"
>
<TrashIcon className="size-4" />
</Button>
</Tooltiper>
</div>
</div>
)}
<form
onSubmit={handleSubmit((values) => {
mutation.mutate({
...values,
password: getSharePasswordToSubmit(
values.password,
!!dirtyFields.password
),
});
})}
>
<Input
{...register('password')}
placeholder="Enter your password (optional)"
size="large"
type={password === STORED_PASSWORD_PLACEHOLDER ? 'text' : 'password'}
/>
<DialogFooter>
<Button onClick={() => popModal()} type="button" variant="outline">
Cancel
</Button>
<Button type="submit">
{isShared ? 'Update' : 'Make it public'}
</Button>
</DialogFooter>
</form>
</ModalContent>
);
}