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.

fix(crm): hide Open contact unless the team has CRM enabled (#5767)

teo69b3710f4035
diff --git a/apps/web/src/lib/core/component/UserTooltip.test.tsx b/apps/web/src/lib/core/component/UserTooltip.test.tsx
index 6cc9ff5a89..b63a7728fc 100644
--- a/apps/web/src/lib/core/component/UserTooltip.test.tsx
+++ b/apps/web/src/lib/core/component/UserTooltip.test.tsx
@@ -9,7 +9,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { UserTooltip } from './UserTooltip';
const mocks = vi.hoisted(() => ({
- crmEnabled: true,
+ crmFlagEnabled: true,
+ teamCrmEnabled: true as boolean | null,
fetchCrmContactByEmail: vi.fn(),
openWithSplit: vi.fn(),
onClose: vi.fn(),
@@ -17,7 +18,7 @@ const mocks = vi.hoisted(() => ({
vi.mock('@app/lib/analytics/posthog', () => ({
useFeatureFlag: () => () => ({
- enabled: mocks.crmEnabled,
+ enabled: mocks.crmFlagEnabled,
payload: undefined,
}),
}));
@@ -49,6 +50,15 @@ vi.mock('@queries/crm/contacts', () => ({
fetchCrmContactByEmail: mocks.fetchCrmContactByEmail,
}));
+vi.mock('@queries/team/teams', () => ({
+ useCurrentTeamQuery: () => ({
+ get data() {
+ if (mocks.teamCrmEnabled === null) return null;
+ return { team: { crm_enabled: mocks.teamCrmEnabled } };
+ },
+ }),
+}));
+
vi.mock('@ui', () => ({
cn: (...classes: Array<string | undefined>) =>
classes.filter(Boolean).join(' '),
@@ -62,7 +72,8 @@ vi.mock('./UserIcon', () => ({
}));
beforeEach(() => {
- mocks.crmEnabled = true;
+ mocks.crmFlagEnabled = true;
+ mocks.teamCrmEnabled = true;
mocks.fetchCrmContactByEmail.mockReset();
mocks.fetchCrmContactByEmail.mockResolvedValue({ id: 'contact-1' });
mocks.openWithSplit.mockReset();
@@ -81,7 +92,9 @@ describe('UserTooltip CRM contact action', () => {
/>
));
- await user.click(screen.getByRole('button', { name: 'Open contact' }));
+ await user.click(
+ await screen.findByRole('button', { name: 'Open contact' })
+ );
expect(mocks.fetchCrmContactByEmail).toHaveBeenCalledWith('[email protected]');
expect(mocks.openWithSplit).toHaveBeenCalledWith(
@@ -91,8 +104,28 @@ describe('UserTooltip CRM contact action', () => {
expect(mocks.onClose).toHaveBeenCalledOnce();
});
- it('hides the contact action when CRM is disabled', () => {
- mocks.crmEnabled = false;
+ it('hides the contact action when the CRM feature flag is off', () => {
+ mocks.crmFlagEnabled = false;
+
+ render(() => (
+ <UserTooltip displayName="Panat Taranat" email="[email protected]" />
+ ));
+
+ expect(screen.queryByRole('button', { name: 'Open contact' })).toBeNull();
+ });
+
+ it('hides the contact action when CRM is disabled for the team', () => {
+ mocks.teamCrmEnabled = false;
+
+ render(() => (
+ <UserTooltip displayName="Panat Taranat" email="[email protected]" />
+ ));
+
+ expect(screen.queryByRole('button', { name: 'Open contact' })).toBeNull();
+ });
+
+ it('hides the contact action when the user has no team', () => {
+ mocks.teamCrmEnabled = null;
render(() => (
<UserTooltip displayName="Panat Taranat" email="[email protected]" />
diff --git a/apps/web/src/lib/core/component/UserTooltip.tsx b/apps/web/src/lib/core/component/UserTooltip.tsx
index a30c2e84a6..bb7fc726ce 100644
--- a/apps/web/src/lib/core/component/UserTooltip.tsx
+++ b/apps/web/src/lib/core/component/UserTooltip.tsx
@@ -14,9 +14,10 @@ import WideTask from '@icon/wide-task.svg';
import IconCheck from '@phosphor/check.svg';
import { useGetOrCreateDirectMessageMutation } from '@queries/channel/get-or-create-dm';
import { fetchCrmContactByEmail } from '@queries/crm/contacts';
+import { useCurrentTeamQuery } from '@queries/team/teams';
import { debounce } from '@solid-primitives/scheduled';
import { cn, Surface } from '@ui';
-import { createSignal, type JSX, Show } from 'solid-js';
+import { createSignal, type JSX, Show, Suspense } from 'solid-js';
import { UserIcon } from './UserIcon';
type UserTooltipProps = {
@@ -30,7 +31,6 @@ type UserTooltipProps = {
export function UserTooltip(props: UserTooltipProps) {
const [copied, setCopied] = createSignal(false);
- const [openingContact, setOpeningContact] = createSignal(false);
const resetCopied = debounce(() => setCopied(false), 800);
function handleCopyEmail(e: MouseEvent) {
@@ -75,28 +75,6 @@ export function UserTooltip(props: UserTooltipProps) {
}
};
- const openContact = async (e: MouseEvent) => {
- e.preventDefault();
- e.stopPropagation();
- const email = props.email;
- if (!email || openingContact()) return;
-
- const preferNewSplit = e.shiftKey;
- setOpeningContact(true);
- try {
- const contact = await fetchCrmContactByEmail(email);
- openWithSplit(
- { type: 'contact', id: contact.id },
- { preferNewSplit, reopen: 'latest' }
- );
- } catch {
- toast.failure('Failed to open contact');
- } finally {
- setOpeningContact(false);
- props.onClose?.();
- }
- };
-
const openTaskComposer = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
@@ -156,11 +134,12 @@ export function UserTooltip(props: UserTooltipProps) {
Copy email
</ActionItem>
</Show>
- <Show when={crmFlag().enabled && props.email}>
- <ActionItem onClick={openContact} disabled={openingContact()}>
- <WideContact class="size-3.5" />
- Open contact
- </ActionItem>
+ <Show when={crmFlag().enabled ? props.email : undefined}>
+ {(email) => (
+ <Suspense fallback={<div class="h-8" />}>
+ <OpenContactAction email={email()} onClose={props.onClose} />
+ </Suspense>
+ )}
</Show>
<Show when={canTreatAsUser() && props.id !== currentUserId()}>
<ActionItem onClick={openDM}>
@@ -181,6 +160,47 @@ export function UserTooltip(props: UserTooltipProps) {
);
}
+/**
+ * Inner action: lives inside a local `<Suspense>` so reading
+ * `useCurrentTeamQuery().data` suspends only this button, not the tooltip.
+ */
+function OpenContactAction(props: { email: string; onClose?: () => void }) {
+ const [openingContact, setOpeningContact] = createSignal(false);
+ const { openWithSplit } = useSplitLayout();
+ const currentTeamQuery = useCurrentTeamQuery();
+ const crmEnabled = () => currentTeamQuery.data?.team.crm_enabled === true;
+
+ const openContact = async (e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ if (openingContact() || !crmEnabled()) return;
+
+ const preferNewSplit = e.shiftKey;
+ setOpeningContact(true);
+ try {
+ const contact = await fetchCrmContactByEmail(props.email);
+ openWithSplit(
+ { type: 'contact', id: contact.id },
+ { preferNewSplit, reopen: 'latest' }
+ );
+ } catch {
+ toast.failure('Failed to open contact');
+ } finally {
+ setOpeningContact(false);
+ props.onClose?.();
+ }
+ };
+
+ return (
+ <Show when={crmEnabled()}>
+ <ActionItem onClick={openContact} disabled={openingContact()}>
+ <WideContact class="size-3.5" />
+ Open contact
+ </ActionItem>
+ </Show>
+ );
+}
+
function ActionItem(props: {
children: JSX.Element;
onClick: JSX.EventHandler<HTMLButtonElement, MouseEvent>;