Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(scheduling): auto-create default booking link and add dropdown for multiple links (#7790)

Jacob Beckerman84eb4feb9072
diff --git a/apps/web/src/features/scheduling/calendar-actions.tsx b/apps/web/src/features/scheduling/calendar-actions.tsx
index 0991f101c6..f4cdc50e9a 100644
--- a/apps/web/src/features/scheduling/calendar-actions.tsx
+++ b/apps/web/src/features/scheduling/calendar-actions.tsx
@@ -5,46 +5,104 @@ import { useSettingsState } from '@core/constant/SettingsState';
import { useUserId } from '@core/context/user';
import { writeClipboardData } from '@core/util/dataTransfer';
import { getWebOrigin } from '@core/util/webOrigin';
+import { DropdownMenu } from '@kobalte/core/dropdown-menu';
+import CaretDownIcon from '@phosphor/caret-down.svg';
import GearIcon from '@phosphor/gear.svg';
import LinkIcon from '@phosphor/link.svg';
import { Button } from '@ui';
-import { Suspense } from 'solid-js';
-import { useSchedulingProfileQuery } from './queries/source';
+import { For, Show, Suspense } from 'solid-js';
+import type { EventType } from './core/types';
+import { useSchedulingProfileWithAutoSetupQuery } from './queries/source';
function Actions() {
const user = useUserId();
const settings = useSettingsState();
- const source = useSchedulingProfileQuery(() => ({
- id: user() ?? 'me',
- name: 'Personal',
- canEdit: true,
- }));
- const copy = async () => {
+ const source = useSchedulingProfileWithAutoSetupQuery(
+ () => ({
+ id: user() ?? 'me',
+ name: 'Personal',
+ canEdit: true,
+ }),
+ () => user() ?? ''
+ );
+
+ const enabledLinks = () => {
+ const profile = source.isSuccess ? source.data : undefined;
+ if (!profile?.revision) return [];
+ return profile.eventTypes.filter((e) => e.enabled);
+ };
+
+ const copyLink = async (event?: EventType) => {
const profile = source.isSuccess ? source.data : undefined;
- if (!profile?.revision || !profile.eventTypes.some((e) => e.enabled)) {
+ if (!profile?.revision) {
settings.openSettings('Booking links');
return;
}
- if (
- await writeClipboardData({
- 'text/plain': `${getWebOrigin()}/app/book/${encodeURIComponent(profile.id)}`,
- })
- )
+
+ const slug = event?.slug;
+ const url = slug
+ ? `${getWebOrigin()}/app/book/${encodeURIComponent(profile.id)}/${encodeURIComponent(slug)}`
+ : `${getWebOrigin()}/app/book/${encodeURIComponent(profile.id)}`;
+
+ if (await writeClipboardData({ 'text/plain': url }))
toast.success('Booking link copied');
else toast.failure('Could not copy booking link');
};
+
+ const handleClick = async () => {
+ const links = enabledLinks();
+ if (links.length === 0) {
+ settings.openSettings('Booking links');
+ } else if (links.length === 1) {
+ await copyLink(links[0]);
+ }
+ };
+
return (
<div class="flex shrink-0 items-center gap-1">
- <Button
- variant="ghost"
- size="sm"
- label="Copy booking link"
- disabled={source.isPending}
- onClick={() => void copy()}
+ <Show
+ when={enabledLinks().length > 1}
+ fallback={
+ <Button
+ variant="ghost"
+ size="sm"
+ label="Copy booking link"
+ disabled={source.isPending}
+ onClick={() => void handleClick()}
+ >
+ <LinkIcon class="size-3.5" />
+ <span class="hidden lg:inline">Booking link</span>
+ </Button>
+ }
>
- <LinkIcon class="size-3.5" />
- <span class="hidden lg:inline">Booking link</span>
- </Button>
+ <DropdownMenu>
+ <DropdownMenu.Trigger
+ as={Button}
+ variant="ghost"
+ size="sm"
+ label="Copy booking link"
+ disabled={source.isPending}
+ >
+ <LinkIcon class="size-3.5" />
+ <span class="hidden lg:inline">Booking link</span>
+ <CaretDownIcon class="size-3" />
+ </DropdownMenu.Trigger>
+ <DropdownMenu.Portal>
+ <DropdownMenu.Content class="z-action-menu min-w-48 rounded-xl border border-edge-muted bg-menu p-1 text-sm shadow-lg">
+ <For each={enabledLinks()}>
+ {(event) => (
+ <DropdownMenu.Item
+ class="rounded-lg px-3 py-2 outline-none data-highlighted:bg-hover"
+ onSelect={() => void copyLink(event)}
+ >
+ {event.title}
+ </DropdownMenu.Item>
+ )}
+ </For>
+ </DropdownMenu.Content>
+ </DropdownMenu.Portal>
+ </DropdownMenu>
+ </Show>
<Button
variant="ghost"
size="icon-sm"
diff --git a/apps/web/src/features/scheduling/core/types.ts b/apps/web/src/features/scheduling/core/types.ts
index 2fda6356a9..ee8ae2a407 100644
--- a/apps/web/src/features/scheduling/core/types.ts
+++ b/apps/web/src/features/scheduling/core/types.ts
@@ -153,6 +153,36 @@ export function newEventType(
};
}
+export function newDefaultBookingLink(
+ scheduleId: string,
+ userId: string,
+ firstName: string
+): EventType {
+ const displayName = firstName || 'My';
+ const title = `${displayName}'s Booking Link`;
+ return {
+ id: crypto.randomUUID(),
+ title,
+ slug: slugify(title),
+ description: `Book a meeting with ${displayName}`,
+ durationMinutes: 30,
+ location: '',
+ googleMeet: true,
+ enabled: true,
+ scheduleId,
+ mode: 'individual',
+ hosts: [userId],
+ beforeMinutes: 0,
+ afterMinutes: 0,
+ noticeMinutes: 120,
+ horizonDays: 60,
+ intervalMinutes: 30,
+ dailyLimit: null,
+ requiresConfirmation: false,
+ questions: [],
+ };
+}
+
export function slugify(value: string): string {
return value
.toLowerCase()
diff --git a/apps/web/src/features/scheduling/queries/source.ts b/apps/web/src/features/scheduling/queries/source.ts
index 62c94f1329..2d64b55998 100644
--- a/apps/web/src/features/scheduling/queries/source.ts
+++ b/apps/web/src/features/scheduling/queries/source.ts
@@ -1,10 +1,16 @@
+import { getDisplayNameParts, type MacroId } from '@core/user';
import { throwOnErr } from '@core/util/result';
import { queryClient } from '@queries/client';
import { schedulingClient } from '@service-email/scheduling';
import { useMutation, useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
import type { SchedulingSource } from '../context/scheduling-context';
-import type { SchedulingProfile, SchedulingScope } from '../core/types';
+import {
+ newDefaultBookingLink,
+ newSchedule,
+ type SchedulingProfile,
+ type SchedulingScope,
+} from '../core/types';
import { schedulingKeys } from './keys';
export function useSchedulingProfileQuery(scope: Accessor<SchedulingScope>) {
@@ -106,3 +112,61 @@ export function useManageBookingMutation() {
mutationFn: (id: string) => throwOnErr(() => schedulingClient.manage(id)),
}));
}
+
+async function createDefaultBookingLinkIfNeeded(
+ profile: SchedulingProfile,
+ userId: string
+): Promise<SchedulingProfile | null> {
+ if (profile.revision !== 0 || profile.eventTypes.length > 0) {
+ return null;
+ }
+
+ const { firstName } = getDisplayNameParts(userId as MacroId);
+ const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ const schedule = newSchedule(timeZone);
+ const bookingLink = newDefaultBookingLink(schedule.id, userId, firstName);
+
+ const updatedProfile: SchedulingProfile = {
+ ...profile,
+ schedules: [schedule],
+ defaultScheduleId: schedule.id,
+ eventTypes: [bookingLink],
+ };
+
+ try {
+ const result = await throwOnErr(() =>
+ schedulingClient.save(updatedProfile, undefined)
+ );
+ return result.profile;
+ } catch {
+ return null;
+ }
+}
+
+export function useSchedulingProfileWithAutoSetupQuery(
+ scope: Accessor<SchedulingScope>,
+ userId: Accessor<string>
+) {
+ return useQuery(() => ({
+ queryKey: schedulingKeys.settings(scope().id).queryKey,
+ queryFn: async () => {
+ const result = await throwOnErr(() =>
+ schedulingClient.settings(scope().teamId)
+ );
+ const profile = result.profile;
+
+ if (!scope().teamId && userId()) {
+ const updated = await createDefaultBookingLinkIfNeeded(
+ profile,
+ userId()
+ );
+ if (updated) {
+ return updated;
+ }
+ }
+
+ return profile;
+ },
+ retry: false,
+ }));
+}