make onboarding + create client easier

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-12 22:31:54 +01:00
parent 36c26f2f75
commit 83816fa104
17 changed files with 383 additions and 506 deletions

View File

@@ -1,252 +0,0 @@
'use client';
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import { CheckboxInput } from '@/components/ui/checkbox';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAppParams } from '@/hooks/useAppParams';
import { clipboard } from '@/utils/clipboard';
import { zodResolver } from '@hookform/resolvers/zod';
import { Copy, SaveIcon } from 'lucide-react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import type { SubmitHandler } from 'react-hook-form';
import { Controller, useForm, useWatch } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';
import { api, handleError } from '../../../_trpc/client';
const validation = z.object({
name: z.string().min(1),
domain: z.string().optional(),
withSecret: z.boolean().optional(),
});
type IForm = z.infer<typeof validation>;
export function CreateClient() {
const [open, setOpen] = useState(false);
const { organizationId, projectId } = useAppParams();
const clients = api.client.list.useQuery({
organizationId,
});
const clientsCount = clients.data?.length;
useEffect(() => {
if (clientsCount === 0) {
setOpen(true);
}
}, [clientsCount]);
const router = useRouter();
const form = useForm<IForm>({
resolver: zodResolver(validation),
defaultValues: {
withSecret: false,
name: '',
domain: '',
},
});
const mutation = api.client.create2.useMutation({
onError: handleError,
onSuccess() {
toast.success('Client created');
router.refresh();
},
});
const onSubmit: SubmitHandler<IForm> = (values) => {
mutation.mutate({
name: values.name,
domain: values.withSecret ? undefined : values.domain,
organizationId,
projectId,
});
};
const watch = useWatch({
control: form.control,
name: 'withSecret',
});
return (
<Dialog open={open}>
{mutation.isSuccess ? (
<>
<DialogContent
className="sm:max-w-[425px]"
onClose={() => setOpen(false)}
>
<DialogHeader>
<DialogTitle>Success</DialogTitle>
<DialogDescription>
{mutation.data.clientSecret
? 'Use your client id and secret with our SDK to send events to us. '
: 'Use your client id with our SDK to send events to us. '}
See our{' '}
<Link href="https//openpanel.dev/docs" className="underline">
documentation
</Link>
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<button
className="mt-4 text-left"
onClick={() => clipboard(mutation.data.clientId)}
>
<Label>Client ID</Label>
<div className="flex items-center justify-between rounded bg-gray-100 p-2 px-3">
{mutation.data.clientId}
<Copy size={16} />
</div>
</button>
{mutation.data.clientSecret ? (
<button
className="mt-4 text-left"
onClick={() => clipboard(mutation.data.clientId)}
>
<Label>Secret</Label>
<div className="flex items-center justify-between rounded bg-gray-100 p-2 px-3">
{mutation.data.clientSecret}
<Copy size={16} />
</div>
</button>
) : (
<div className="mt-4 text-left">
<Label>Cors settings</Label>
<div className="flex items-center justify-between rounded bg-gray-100 p-2 px-3">
{mutation.data.cors}
</div>
<div className="text-sm italic mt-1">
You can update cors settings{' '}
<Link className="underline" href="/qwe/qwe/">
here
</Link>
</div>
</div>
)}
</div>
<DialogFooter>
<Button
type="button"
variant={'secondary'}
onClick={() => setOpen(false)}
>
Close
</Button>
</DialogFooter>
</DialogContent>
</>
) : (
<>
<DialogContent
className="sm:max-w-[425px]"
onClose={() => setOpen(false)}
>
<DialogHeader>
<DialogTitle>Let's connect</DialogTitle>
<DialogDescription>
Create a client so you can start send events to us 🚀
</DialogDescription>
</DialogHeader>
<form
className="flex flex-col gap-4"
onSubmit={form.handleSubmit(onSubmit)}
>
<div className="grid gap-4 py-4">
<div>
<Label>Client name</Label>
<Input
placeholder="Eg. My App Client"
error={form.formState.errors.name?.message}
{...form.register('name')}
/>
</div>
<Controller
name="withSecret"
control={form.control}
render={({ field }) => (
<CheckboxInput
defaultChecked={field.value}
onCheckedChange={(checked) => {
field.onChange(!checked);
}}
>
This is a website
</CheckboxInput>
)}
/>
<div>
<Label>Your domain name</Label>
<Input
placeholder="https://...."
error={form.formState.errors.domain?.message}
{...form.register('domain')}
disabled={watch}
/>
</div>
</div>
<DialogFooter>
<Button
type="button"
variant={'secondary'}
onClick={() => setOpen(false)}
>
Cancel
</Button>
<Button
type="submit"
icon={SaveIcon}
loading={mutation.isLoading}
>
Create
</Button>
</DialogFooter>
</form>
</DialogContent>
</>
)}
</Dialog>
);
}
// <div>
// <div className="text-lg">
// Select your framework and we'll generate a client for you.
// </div>
// <div className="flex flex-wrap gap-2 mt-8">
// <FeatureButton
// name="React"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png"
// />
// <FeatureButton
// name="React Native"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png"
// />
// <FeatureButton
// name="Next.js"
// logo="https://static-00.iconduck.com/assets.00/nextjs-icon-512x512-y563b8iq.png"
// />
// <FeatureButton
// name="Remix"
// logo="https://www.datocms-assets.com/205/1642515307-square-logo.svg"
// />
// <FeatureButton
// name="Vue"
// logo="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0UhQnp6TUPCwAr3ruTEwBDiTN5HLAWaoUD3AJIgtepQ&s"
// />
// <FeatureButton
// name="HTML"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/240px-HTML5_logo_and_wordmark.svg.png"
// />
// </div>
// </div>

View File

@@ -4,11 +4,10 @@ import { useEffect } from 'react';
import { useAppParams } from '@/hooks/useAppParams';
import { cn } from '@/utils/cn';
import { useUser } from '@clerk/nextjs';
import type { IServiceDashboards } from '@openpanel/db';
import {
BookmarkIcon,
BuildingIcon,
CogIcon,
DotIcon,
GanttChartIcon,
KeySquareIcon,
LayoutPanelTopIcon,
@@ -21,6 +20,8 @@ import type { LucideProps } from 'lucide-react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import type { IServiceDashboards } from '@openpanel/db';
function LinkWithIcon({
href,
icon: Icon,
@@ -127,7 +128,7 @@ export default function LayoutMenu({ dashboards }: LayoutMenuProps) {
href={`/${params.organizationId}/${projectId}/settings/profile`}
/>
<LinkWithIcon
icon={UserIcon}
icon={BookmarkIcon}
label="References"
href={`/${params.organizationId}/${projectId}/settings/references`}
/>

View File

@@ -10,10 +10,10 @@ import OverviewTopGeo from '@/components/overview/overview-top-geo';
import OverviewTopPages from '@/components/overview/overview-top-pages';
import OverviewTopSources from '@/components/overview/overview-top-sources';
import { getExists } from '@/server/pageExists';
import { db } from '@openpanel/db';
import OverviewMetrics from '../../../../components/overview/overview-metrics';
import { CreateClient } from './create-client';
import { StickyBelowHeader } from './layout-sticky-below-header';
import { OverviewReportRange } from './overview-sticky-header';
@@ -38,7 +38,6 @@ export default async function Page({
return (
<PageLayout title="Overview" organizationSlug={organizationId}>
<CreateClient />
<StickyBelowHeader>
<div className="p-4 flex gap-2 justify-between">
<div className="flex gap-2">

View File

@@ -1,19 +1,23 @@
'use client';
import { buttonVariants } from '@/components/ui/button';
import { Widget, WidgetBody, WidgetHead } from '@/components/Widget';
import { SignOutButton } from '@clerk/nextjs';
export function Logout() {
return (
<Widget className="border-destructive">
<WidgetHead className="border-destructive">
<span className="title text-destructive">Sad part</span>
<WidgetHead>
<span className="title">Sad part</span>
</WidgetHead>
<WidgetBody>
<p className="mb-4">
Sometime&apos;s you need to go. See you next time
</p>
<SignOutButton />
<SignOutButton
// @ts-expect-error
className={buttonVariants({ variant: 'destructive' })}
/>
</WidgetBody>
</Widget>
);

View File

@@ -2,7 +2,6 @@
import { LogoSquare } from '@/components/Logo';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAppParams } from '@/hooks/useAppParams';
@@ -79,35 +78,3 @@ export function CreateProject() {
</>
);
}
// <div>
// <div className="text-lg">
// Select your framework and we'll generate a client for you.
// </div>
// <div className="flex flex-wrap gap-2 mt-8">
// <FeatureButton
// name="React"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png"
// />
// <FeatureButton
// name="React Native"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png"
// />
// <FeatureButton
// name="Next.js"
// logo="https://static-00.iconduck.com/assets.00/nextjs-icon-512x512-y563b8iq.png"
// />
// <FeatureButton
// name="Remix"
// logo="https://www.datocms-assets.com/205/1642515307-square-logo.svg"
// />
// <FeatureButton
// name="Vue"
// logo="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ0UhQnp6TUPCwAr3ruTEwBDiTN5HLAWaoUD3AJIgtepQ&s"
// />
// <FeatureButton
// name="HTML"
// logo="https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/240px-HTML5_logo_and_wordmark.svg.png"
// />
// </div>
// </div>

View File

@@ -30,7 +30,7 @@ export default async function Page({ params: { organizationId } }: PageProps) {
const isAccepted = await isWaitlistUserAccepted();
if (!isAccepted) {
return (
<div className="flex items-center justify-center h-screen">
<div className="p-4 flex items-center justify-center h-screen">
<div className="max-w-lg w-full">
<LogoSquare className="w-20 md:w-28 mb-8" />
<h1 className="font-medium text-3xl">Not quite there yet</h1>
@@ -46,7 +46,7 @@ export default async function Page({ params: { organizationId } }: PageProps) {
if (projects.length === 0) {
return (
<div className="flex items-center justify-center h-screen">
<div className="flex items-center justify-center h-screen p-4 ">
<div className="max-w-lg w-full">
<CreateProject />
</div>
@@ -59,7 +59,7 @@ export default async function Page({ params: { organizationId } }: PageProps) {
}
return (
<div className="max-w-xl w-full mx-auto flex flex-col gap-4 pt-20">
<div className="max-w-xl w-full mx-auto flex flex-col gap-4 pt-20 p-4 ">
<h1 className="font-medium text-xl">Select project</h1>
{projects.map((item) => (
<ProjectCard key={item.id} {...item} />

View File

@@ -1,11 +1,16 @@
'use client';
import { useState } from 'react';
import { CreateClientSuccess } from '@/components/clients/create-client-success';
import { LogoSquare } from '@/components/Logo';
import { Button } from '@/components/ui/button';
import { Button, buttonVariants } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { cn } from '@/utils/cn';
import { zodResolver } from '@hookform/resolvers/zod';
import { SaveIcon } from 'lucide-react';
import { SaveIcon, WallpaperIcon } from 'lucide-react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import type { SubmitHandler } from 'react-hook-form';
import { useForm } from 'react-hook-form';
@@ -13,10 +18,21 @@ import { z } from 'zod';
import { api, handleError } from '../_trpc/client';
const validation = z.object({
organization: z.string().min(4),
project: z.string().optional(),
});
const validation = z
.object({
organization: z.string().min(3),
project: z.string().min(3),
cors: z.string().nullable(),
tab: z.string(),
})
.refine(
(data) =>
data.tab === 'other' || (data.tab === 'website' && data.cors !== ''),
{
message: 'Cors is required',
path: ['cors'],
}
);
type IForm = z.infer<typeof validation>;
@@ -24,63 +40,117 @@ export function CreateOrganization() {
const router = useRouter();
const form = useForm<IForm>({
resolver: zodResolver(validation),
defaultValues: {
organization: '',
project: '',
cors: '',
tab: 'website',
},
});
const mutation = api.onboarding.organziation.useMutation({
onError: handleError,
onSuccess({ organization, project }) {
let url = `/${organization.slug}`;
if (project) {
url += `/${project.id}`;
}
router.replace(url);
},
});
const onSubmit: SubmitHandler<IForm> = (values) => {
mutation.mutate(values);
mutation.mutate({
...values,
cors: values.tab === 'website' ? values.cors : null,
});
};
return (
<>
<div>
<LogoSquare className="w-20 md:w-28 mb-8" />
<h1 className="font-medium text-3xl">Welcome to Openpanel</h1>
<div className="text-lg">
Create your organization below (can be personal or a company) and
optionally your first project 🤠
if (mutation.isSuccess && mutation.data.client) {
return (
<div className="card p-4 md:p-8">
<LogoSquare className="w-20 mb-4" />
<h1 className="font-medium text-3xl">Nice job!</h1>
<div className="mb-4">
You're ready to start using our SDK. Save the client ID and secret (if
you have any)
</div>
<CreateClientSuccess {...mutation.data.client} />
<div className="flex gap-4 mt-4">
<a
className={cn(buttonVariants({ variant: 'secondary' }), 'flex-1')}
href="https://docs.openpanel.dev/docs"
target="_blank"
>
Read docs
</a>
<Button
className="flex-1"
onClick={() => router.refresh()}
icon={WallpaperIcon}
>
Dashboard
</Button>
</div>
<form
className="mt-8 flex flex-col gap-4"
onSubmit={form.handleSubmit(onSubmit)}
>
<div>
<Label>Organization name *</Label>
<Input
placeholder="Organization name"
size="large"
error={form.formState.errors.organization?.message}
{...form.register('organization')}
/>
</div>
<div>
<Label>Project name</Label>
<Input
placeholder="Project name"
size="large"
error={form.formState.errors.project?.message}
{...form.register('project')}
/>
</div>
<div className="flex justify-end">
<Button
type="submit"
size="lg"
icon={SaveIcon}
loading={mutation.isLoading}
>
Create
</Button>
</div>
</form>
</div>
</>
);
}
return (
<div className="card p-4 md:p-8">
<LogoSquare className="w-20 mb-4" />
<h1 className="font-medium text-3xl">Welcome to Openpanel</h1>
<div className="text-lg">
Create your organization below (can be personal or a company) and your
first project.
</div>
<form
className="mt-8 flex flex-col gap-4"
onSubmit={form.handleSubmit(onSubmit)}
>
<div>
<Label>Organization name *</Label>
<Input
placeholder="Organization name"
error={form.formState.errors.organization?.message}
{...form.register('organization')}
/>
</div>
<div>
<Label>Project name *</Label>
<Input
placeholder="Project name"
error={form.formState.errors.project?.message}
{...form.register('project')}
/>
</div>
<Tabs
defaultValue="website"
onValueChange={(val) => form.setValue('tab', val)}
className="h-28"
>
<TabsList className="bg-slate-200">
<TabsTrigger value="website">Website</TabsTrigger>
<TabsTrigger value="other">Other</TabsTrigger>
</TabsList>
<TabsContent value="website">
<Label>Cors *</Label>
<Input
placeholder="https://example.com"
error={form.formState.errors.cors?.message}
{...form.register('cors')}
/>
</TabsContent>
<TabsContent value="other">
<div className="p-2 px-3 bg-white rounded text-sm">
🔑 You will get a secret to use for your API requests.
</div>
</TabsContent>
</Tabs>
<div className="flex justify-end">
<Button
type="submit"
size="lg"
icon={SaveIcon}
loading={mutation.isLoading}
>
Create
</Button>
</div>
</form>
</div>
);
}