Skip to content
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
1 change: 1 addition & 0 deletions packages/app/src/components/HelpPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export const HelpPopover: FC = () => {
plus a gap rather than overlapping the menu. */}
<PopoverContent side="left" align="center" sideOffset={20} className="w-80">
<SubscribeForm
source="resources_menu"
autoFocus
onDismiss={() => setSubscribeOpen(false)}
onSuccess={() => subscribeCardStore.markSubscribed()}
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/components/SubscribeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function SubscribeCard({
>
<div className="px-3 py-2.5">
<SubscribeForm
source="post_update_card"
compactSubmit
description={<Trans>Product updates in your inbox.</Trans>}
onSuccess={() => {
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/components/SubscribeForm.dom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mock.module('@/lib/subscribe', () => ({ submitSubscribe }));

async function renderForm(onSuccess?: () => void) {
const { SubscribeForm } = await import('./SubscribeForm');
render(<SubscribeForm onSuccess={onSuccess} />);
render(<SubscribeForm source="resources_menu" onSuccess={onSuccess} />);
}

describe('SubscribeForm', () => {
Expand All @@ -44,7 +44,9 @@ describe('SubscribeForm', () => {
await userEvent.type(screen.getByTestId('subscribe-email'), 'someone@example.com');
await userEvent.click(screen.getByTestId('subscribe-submit'));

await waitFor(() => expect(submitSubscribe).toHaveBeenCalledWith('someone@example.com'));
await waitFor(() =>
expect(submitSubscribe).toHaveBeenCalledWith('someone@example.com', 'resources_menu'),
);
expect(onSuccess).toHaveBeenCalledTimes(1);
expect(await screen.findByText(/Watch your inbox/i)).not.toBeNull();
expect(screen.queryByTestId('subscribe-email')).toBeNull();
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/components/SubscribeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { Controller, useForm } from 'react-hook-form';
import { z } from 'zod';
import { Button } from '@/components/ui/button';
import { InputGroup, InputGroupAddon, InputGroupInput } from '@/components/ui/input-group';
import { submitSubscribe } from '@/lib/subscribe';
import { type SubscribeSource, submitSubscribe } from '@/lib/subscribe';
import { cn } from '@/lib/utils';

interface SubscribeValues {
email: string;
}

export interface SubscribeFormProps {
source: SubscribeSource;
title?: ReactNode;
description?: ReactNode;
onSuccess?: () => void;
Expand All @@ -25,6 +26,7 @@ export interface SubscribeFormProps {
}

export function SubscribeForm({
source,
title,
description,
onSuccess,
Expand Down Expand Up @@ -55,7 +57,7 @@ export function SubscribeForm({

async function onSubmit(values: SubscribeValues) {
form.clearErrors('root');
const result = await submitSubscribe(values.email);
const result = await submitSubscribe(values.email, source);
if (result.ok) {
setSubscribed(true);
onSuccess?.();
Expand Down
9 changes: 7 additions & 2 deletions packages/app/src/lib/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const SUBSCRIBE_ENDPOINT = 'https://openknowledge.ai/api/subscribe';

export type SubscribeSource = 'resources_menu' | 'post_update_card';

export type SubscribeResult =
| { ok: true }
| { ok: false; reason: 'invalid' | 'unavailable' | 'error' };

export async function submitSubscribe(email: string): Promise<SubscribeResult> {
export async function submitSubscribe(
email: string,
source: SubscribeSource,
): Promise<SubscribeResult> {
try {
const response = await fetch(SUBSCRIBE_ENDPOINT, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ email }),
body: JSON.stringify({ email, source }),
signal: AbortSignal.timeout(15_000),
});
if (response.ok) {
Expand Down