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(onboarding): pre-add teammates to the team invite list (#5522)

teo48900dcfd93f
diff --git a/apps/web/src/features/setup/flow/OnboardingFlow.tsx b/apps/web/src/features/setup/flow/OnboardingFlow.tsx
index ab67e6fc94..baacaa35e6 100644
--- a/apps/web/src/features/setup/flow/OnboardingFlow.tsx
+++ b/apps/web/src/features/setup/flow/OnboardingFlow.tsx
@@ -185,7 +185,7 @@ function buildSteps(
...connectorSteps,
{
key: 'team',
- title: 'Set up your team',
+ title: 'Macro is meant for teams',
subtitle:
'Macro is built to be used with others. Invite your team to share docs, channels, and context from day one.',
render: (controls) => (
diff --git a/apps/web/src/features/setup/flow/TeamStep.tsx b/apps/web/src/features/setup/flow/TeamStep.tsx
index b67087b1ba..2f1d867a54 100644
--- a/apps/web/src/features/setup/flow/TeamStep.tsx
+++ b/apps/web/src/features/setup/flow/TeamStep.tsx
@@ -3,7 +3,8 @@ import { useEmail } from '@core/context/user';
import { idToDisplayName } from '@core/user/util';
import CheckIcon from '@phosphor/check.svg';
import Plus from '@phosphor/plus.svg';
-import { useContacts } from '@queries/contacts/contacts';
+import XIcon from '@phosphor/x.svg';
+import { useContacts, useContactsQuery } from '@queries/contacts/contacts';
import { useOnboardingQuery } from '@queries/onboarding';
import {
useJoinTeamMutation,
@@ -26,14 +27,18 @@ import {
import {
ContinueButton,
deriveTeamName,
- emailDomain,
FormInput,
- isPlausibleEmail,
SkipButton,
} from './shared';
+import {
+ prefillableTeammates,
+ removeInviteSlot,
+ validInviteEmails,
+} from './teamInvites';
/** Set up your team: already a member → confirmation, pending invites →
- * join, otherwise create (with domain-derived prefill + suggestions). */
+ * join, otherwise create (with a domain-derived name and same-domain
+ * teammates pre-added to the invite list). */
export function TeamStep(props: {
onContinue: () => void;
onSkip: () => void;
@@ -133,16 +138,14 @@ function InvitesPanel(props: {
);
}
-const INITIAL_INVITE_SLOTS = ['', ''];
-const SUGGESTION_CAP = 6;
-
-/** Create a team: pre-derived name + same-domain invite suggestions when the
- * user has a custom domain; the plain form otherwise. */
+/** Create a team: same-domain teammates arrive pre-added to the invite list
+ * (remove to opt them out) when the user has a custom domain; the plain form
+ * otherwise. Waits for contacts and the domain suggestion so the form mounts
+ * once, fully formed — nothing rewrites the user's rows afterwards. */
function CreateTeamForm(props: { onContinue: () => void; onSkip: () => void }) {
- const analytics = useAnalytics();
const email = useEmail();
const contacts = useContacts();
- const createTeam = useCreateTeamWithInvitesMutation();
+ const contactsQuery = useContactsQuery();
const onboardingQuery = useOnboardingQuery();
// Server-judged with the same list the teams service uses for
@@ -150,52 +153,61 @@ function CreateTeamForm(props: { onContinue: () => void; onSkip: () => void }) {
const customDomain = () =>
onboardingQuery.data?.suggested_team_domain ?? undefined;
- const [name, setName] = createSignal('');
- const [nameTouched, setNameTouched] = createSignal(false);
- // Prefill once the suggestion arrives, never over a typed name.
- createEffect(() => {
- const domain = customDomain();
- if (domain && !nameTouched()) setName(deriveTeamName(domain));
- });
- const [inviteSlots, setInviteSlots] = createSignal<string[]>([
- ...INITIAL_INVITE_SLOTS,
- ]);
- let inviteListEl: HTMLDivElement | undefined;
+ // Settled, not succeeded: an errored query opens the plain form.
+ const ready = () =>
+ !contactsQuery.isPending && !onboardingQuery.isPlaceholderData;
- const validInvites = () =>
- [...new Set(inviteSlots().map((value) => value.trim()))].filter(
- (value) => isPlausibleEmail(value) && value !== email()
- );
+ return (
+ <Show when={ready()}>
+ <TeamForm
+ domain={customDomain()}
+ prefilledTeammates={prefillableTeammates({
+ contacts: contacts(),
+ domain: customDomain(),
+ ownEmail: email(),
+ })}
+ onContinue={props.onContinue}
+ onSkip={props.onSkip}
+ />
+ </Show>
+ );
+}
- const suggestions = createMemo(() => {
- const suffix = customDomain();
- if (!suffix) return [];
- const own = email();
- const taken = new Set(inviteSlots().map((value) => value.trim()));
- return contacts()
- .filter(
- (contact) =>
- contact.email !== own &&
- emailDomain(contact.email) === suffix &&
- !taken.has(contact.email)
- )
- .slice(0, SUGGESTION_CAP);
- });
+/** The create-team form proper — mounted with its prefill inputs resolved,
+ * so the name and invite slots initialize once and stay user-owned. */
+function TeamForm(props: {
+ domain: string | undefined;
+ prefilledTeammates: string[];
+ onContinue: () => void;
+ onSkip: () => void;
+}) {
+ const analytics = useAnalytics();
+ const email = useEmail();
+ const createTeam = useCreateTeamWithInvitesMutation();
- const addInvite = (address: string) => {
- setInviteSlots((slots) => {
- const empty = slots.findIndex((value) => value.trim() === '');
- if (empty === -1) return [...slots, address];
- return slots.map((value, i) => (i === empty ? address : value));
- });
- };
+ const [name, setName] = createSignal(
+ props.domain ? deriveTeamName(props.domain) : ''
+ );
+ // Same-domain teammates are pre-added rather than offered: the default is
+ // "invite them", and removing a row is how you opt one out.
+ const prefilled = props.prefilledTeammates;
+ const [inviteSlots, setInviteSlots] = createSignal<string[]>(
+ prefilled.length > 0 ? [...prefilled, ''] : ['', '']
+ );
+ let inviteListEl: HTMLDivElement | undefined;
+
+ const validInvites = () => validInviteEmails(inviteSlots(), email());
+
+ // The X means "don't invite this person", so it belongs on rows that name
+ // one. Blank rows need no removing — they're dropped on submit anyway.
+ const canRemoveSlot = (value: string) => value.trim() !== '';
const addEmptyInvite = () => {
setInviteSlots((slots) => [...slots, '']);
requestAnimationFrame(() => {
- inviteListEl?.scrollTo({
- top: inviteListEl.scrollHeight,
+ inviteListEl?.lastElementChild?.scrollIntoView({
behavior: 'smooth',
+ block: 'nearest',
});
});
};
@@ -203,55 +215,89 @@ function CreateTeamForm(props: { onContinue: () => void; onSkip: () => void }) {
const create = async () => {
if (createTeam.isPending || name().trim().length === 0) return;
// The mutation owns its toasts; stay put (form intact) on failure.
- const invitesSent = validInvites().length;
+ const invites = validInvites();
try {
await createTeam.mutateAsync({
name: name().trim(),
- invites: validInvites().map((address) => ({ email: address })),
+ invites: invites.map((address) => ({ email: address })),
});
} catch {
return;
}
+ const kept = new Set(invites);
analytics.track('onboarding_v4_team', {
action: 'created',
- invites_sent: invitesSent,
- used_domain_suggestion: customDomain() !== undefined,
+ invites_sent: invites.length,
+ invites_prefilled: prefilled.length,
+ invites_removed: prefilled.filter((address) => !kept.has(address)).length,
+ used_domain_suggestion: props.domain !== undefined,
});
props.onContinue();
};
return (
<div class="flex flex-col gap-3">
- <FormInput
- id="team-name"
- placeholder="Team name"
- value={name()}
- autoFocus={!customDomain()}
- onInput={(value) => {
- setNameTouched(true);
- setName(value);
- }}
- />
+ {/* Same row shape as an invite, with the remove gutter left empty, so
+ every input in the form shares one width. Labelled, because the
+ name arrives pre-filled — a placeholder alone would be invisible
+ exactly when the field needs explaining. */}
+ <div class="flex items-center gap-1.5">
+ <div class="flex min-w-0 flex-1 flex-col gap-1.5">
+ <label for="team-name" class="text-xs text-ink-muted">
+ Team name
+ </label>
+ <FormInput
+ id="team-name"
+ // An example, not "Team name" again — the label says that.
+ placeholder="Acme Inc."
+ value={name()}
+ autoFocus={!props.domain}
+ onInput={setName}
+ />
+ </div>
+ <div class="size-7 shrink-0" />
+ </div>
{/* Index, not For: slots are edited strings, and For keys by value —
- each keystroke would recreate the input node and drop focus. */}
- <div
- ref={(el) => (inviteListEl = el)}
- class="flex max-h-48 flex-col gap-3 overflow-y-auto overscroll-contain"
- >
+ each keystroke would recreate the input node and drop focus.
+ No inner scroller: the list opens pre-filled now, and a capped box
+ left a row sliced in half above the buttons — the flow's own
+ scroll container takes the height instead. */}
+ <div ref={(el) => (inviteListEl = el)} class="flex flex-col gap-3">
<Index each={inviteSlots()}>
{(slot, i) => (
- <FormInput
- id={`invite-${i}`}
- type="email"
- placeholder="[email protected]"
- value={slot()}
- onInput={(value) =>
- setInviteSlots((slots) =>
- slots.map((v, j) => (j === i ? value : v))
- )
- }
- />
+ <div class="flex items-center gap-1.5">
+ <div class="min-w-0 flex-1">
+ <FormInput
+ id={`invite-${i}`}
+ type="email"
+ placeholder="[email protected]"
+ value={slot()}
+ onInput={(value) =>
+ setInviteSlots((slots) =>
+ slots.map((v, j) => (j === i ? value : v))
+ )
+ }
+ />
+ </div>
+ {/* Gutter is always reserved, so a blank row's input still lines
+ up with the ones carrying a remove button. */}
+ <div class="flex size-7 shrink-0 items-center justify-center">
+ <Show when={canRemoveSlot(slot())}>
+ <button
+ type="button"
+ aria-label={`Don't invite ${slot().trim()}`}
+ title={`Don't invite ${slot().trim()}`}
+ onClick={() =>
+ setInviteSlots((slots) => removeInviteSlot(slots, i))
+ }
+ class="rounded-md p-1.5 text-ink-extra-muted transition-colors hover:bg-ink/5 hover:text-ink"
+ >
+ <XIcon class="size-4" />
+ </button>
+ </Show>
+ </div>
+ </div>
)}
</Index>
</div>
@@ -266,29 +312,6 @@ function CreateTeamForm(props: { onContinue: () => void; onSkip: () => void }) {
Add another teammate
</Button>
- <Show when={suggestions().length > 0}>
- <div class="flex flex-col gap-1.5">
- <p class="text-xs text-ink-muted">
- From your contacts at {customDomain()}:
- </p>
- <div class="flex flex-wrap gap-1.5">
- <For each={suggestions()}>
- {(contact) => (
- <button
- type="button"
- title={`Invite ${contact.email}`}
- onClick={() => addInvite(contact.email)}
- class="inline-flex h-7 max-w-72 items-center gap-1.5 rounded-full border border-ink/10 bg-surface px-2.5 text-xs text-ink transition-colors hover:border-ink/20"
- >
- <Plus class="size-3 shrink-0 text-ink-extra-muted" />
- <span class="min-w-0 truncate">{contact.email}</span>
- </button>
- )}
- </For>
- </div>
- </div>
- </Show>
-
<ContinueButton
label={
validInvites().length > 0
diff --git a/apps/web/src/features/setup/flow/teamInvites.test.ts b/apps/web/src/features/setup/flow/teamInvites.test.ts
new file mode 100644
index 0000000000..9c8d0814b6
--- /dev/null
+++ b/apps/web/src/features/setup/flow/teamInvites.test.ts
@@ -0,0 +1,71 @@
+import { describe, expect, it } from 'vitest';
+import {
+ PREFILL_CAP,
+ prefillableTeammates,
+ removeInviteSlot,
+ validInviteEmails,
+} from './teamInvites';
+
+const contact = (email: string) => ({ email });
+
+describe('prefillableTeammates', () => {
+ it('picks same-domain contacts, skipping the user and duplicates', () => {
+ expect(
+ prefillableTeammates({
+ contacts: [
+ contact('[email protected]'),
+ contact('[email protected]'),
+ contact('[email protected]'),
+ contact('[email protected]'),
+ contact('[email protected]'),
+ ],
+ domain: 'macro.com',
+ ownEmail: '[email protected]',
+ })
+ });
+
+ it('pre-adds nothing without a suggested domain', () => {
+ expect(
+ prefillableTeammates({
+ contacts: [contact('[email protected]')],
+ domain: undefined,
+ ownEmail: '[email protected]',
+ })
+ ).toEqual([]);
+ });
+
+ it('caps how many teammates get pre-added', () => {
+ const contacts = Array.from({ length: PREFILL_CAP + 3 }, (_, i) =>
+ contact(`teammate${i}@macro.com`)
+ );
+ expect(
+ prefillableTeammates({
+ contacts,
+ domain: 'macro.com',
+ ownEmail: '[email protected]',
+ })
+ ).toHaveLength(PREFILL_CAP);
+ });
+});
+
+describe('removeInviteSlot', () => {
+ it('drops the row at the index, keeping one empty row when the last goes', () => {
+ expect(removeInviteSlot(['[email protected]', '[email protected]', ''], 1)).toEqual([
+ '',
+ ]);
+ expect(removeInviteSlot(['[email protected]'], 0)).toEqual(['']);
+ });
+});
+
+describe('validInviteEmails', () => {
+ it('trims, dedupes, and drops blanks, junk, and the user themselves', () => {
+ expect(
+ validInviteEmails(
+ )
+ ).toEqual(['[email protected]']);
+ });
+});
diff --git a/apps/web/src/features/setup/flow/teamInvites.ts b/apps/web/src/features/setup/flow/teamInvites.ts
new file mode 100644
index 0000000000..59700a6c8f
--- /dev/null
+++ b/apps/web/src/features/setup/flow/teamInvites.ts
@@ -0,0 +1,47 @@
+import { emailDomain, isPlausibleEmail } from './shared';
+
+/** How many same-domain teammates get pre-added to the invite list. */
+export const PREFILL_CAP = 6;
+
+/**
+ * Same-domain teammates worth pre-adding to the invite list: the user's
+ * contacts on `domain`, minus themselves, deduped and capped.
+ *
+ * Empty when the domain isn't team-worthy — the server decides that via
+ * `suggested_team_domain`.
+ */
+export function prefillableTeammates(args: {
+ contacts: { email: string }[];
+ domain: string | undefined;
+ ownEmail: string | undefined;
+}): string[] {
+ const { contacts, domain, ownEmail } = args;
+ if (!domain) return [];
+ const teammates = new Set(
+ contacts
+ .map((contact) => contact.email)
+ .filter(
+ (address) => address !== ownEmail && emailDomain(address) === domain
+ )
+ );
+ return [...teammates].slice(0, PREFILL_CAP);
+}
+
+/**
+ * Drops row `index`, keeping at least one (empty) row so the form never loses
+ * its input.
+ */
+export function removeInviteSlot(slots: string[], index: number): string[] {
+ const next = slots.filter((_, i) => i !== index);
+ return next.length > 0 ? next : [''];
+}
+
+/** Deduped, plausible addresses that aren't the user's own. */
+export function validInviteEmails(
+ slots: string[],
+ ownEmail: string | undefined
+): string[] {
+ return [...new Set(slots.map((value) => value.trim()))].filter(
+ (value) => isPlausibleEmail(value) && value !== ownEmail
+ );
+}
diff --git a/apps/web/src/lib/analytics/app-events.ts b/apps/web/src/lib/analytics/app-events.ts
index 94ad9e592e..abb3355baa 100644
--- a/apps/web/src/lib/analytics/app-events.ts
+++ b/apps/web/src/lib/analytics/app-events.ts
@@ -73,6 +73,10 @@ export type AppEvents = {
onboarding_v4_team: {
action: 'created' | 'joined_invite' | 'already_on_team';
invites_sent?: number;
+ /** Same-domain teammates the step pre-added to the invite list. */
+ invites_prefilled?: number;
+ /** How many of those the user removed before creating the team. */
+ invites_removed?: number;
used_domain_suggestion?: boolean;
};
/**
diff --git a/apps/web/src/lib/queries/contacts/contacts.ts b/apps/web/src/lib/queries/contacts/contacts.ts
index c067d9fe36..8acc8cdf0a 100644
--- a/apps/web/src/lib/queries/contacts/contacts.ts
+++ b/apps/web/src/lib/queries/contacts/contacts.ts
@@ -14,7 +14,8 @@ function contactsQueryOptions() {
};
}
-function useContactsQuery() {
+/** The raw contacts query — for callers that need its loading state. */
+export function useContactsQuery() {
return useQuery(() => contactsQueryOptions());
}