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(user): use branded type for Macroid string with validation (#809)

teo7fe161d90248
diff --git a/js/app/packages/block-channel/component/AttachmentsModal.tsx b/js/app/packages/block-channel/component/AttachmentsModal.tsx
index 5b015e213e..722f3fb5b1 100644
--- a/js/app/packages/block-channel/component/AttachmentsModal.tsx
+++ b/js/app/packages/block-channel/component/AttachmentsModal.tsx
@@ -14,7 +14,7 @@ import {
isDocumentPreviewItem,
useItemPreview,
} from '@core/signal/preview';
-import { useDisplayName } from '@core/user';
+import { tryMacroId, useDisplayName } from '@core/user';
import { isErr } from '@core/util/maybeResult';
import BracketLeft from '@macro-icons/macro-group-bracket-left.svg';
import PaperclipIcon from '@phosphor-icons/core/regular/paperclip.svg?component-solid';
@@ -151,7 +151,7 @@ function AttachmentItem(props: AttachmentItemProps) {
});
const senderId = () => message()?.sender_id || '';
- const [userName] = useDisplayName(senderId());
+ const [userName] = useDisplayName(tryMacroId(senderId()));
const [preview] = useItemPreview({
id: props.attachment.entity_id,
diff --git a/js/app/packages/block-channel/component/Compose.tsx b/js/app/packages/block-channel/component/Compose.tsx
index 6f24a64f68..31134c81a1 100644
--- a/js/app/packages/block-channel/component/Compose.tsx
+++ b/js/app/packages/block-channel/component/Compose.tsx
@@ -10,7 +10,11 @@ import { RecipientSelector } from '@core/component/RecipientSelector';
import { fileFolderDrop } from '@core/directive/fileFolderDrop';
import { useCombinedRecipients } from '@core/signal/useCombinedRecipient';
import type { InputAttachment } from '@core/store/cacheChannelInput';
-import { useDisplayName, type WithCustomUserInput } from '@core/user';
+import {
+ tryMacroId,
+ useDisplayName,
+ type WithCustomUserInput,
+} from '@core/user';
import { handleFileFolderDrop } from '@core/util/upload';
import { createEffect, createMemo, createSignal, on, Show } from 'solid-js';
import { createStore } from 'solid-js/store';
@@ -64,7 +68,7 @@ export function ChannelCompose() {
const dmUserName = createMemo<() => string | undefined>(() => {
const id = dmUserId();
if (!id) return () => undefined;
- return useDisplayName(id)[0];
+ return useDisplayName(tryMacroId(id))[0];
});
const [triedToSubmit, _setTriedToSubmit] = createSignal(false);
@@ -81,7 +85,7 @@ export function ChannelCompose() {
.slice(0, 2)
.map((r) => {
if (r.kind === 'user') {
- return useDisplayName(r.data.id)[0]();
+ return useDisplayName(tryMacroId(r.data.id))[0]();
}
return r.data.email || 'Unknown';
})
diff --git a/js/app/packages/block-channel/component/Message/MessageContainer.tsx b/js/app/packages/block-channel/component/Message/MessageContainer.tsx
index bc7c5e97f1..013bf980b3 100644
--- a/js/app/packages/block-channel/component/Message/MessageContainer.tsx
+++ b/js/app/packages/block-channel/component/Message/MessageContainer.tsx
@@ -20,7 +20,7 @@ import {
STATIC_IMAGE,
STATIC_VIDEO,
} from '@core/store/cacheChannelInput';
-import { useDisplayName } from '@core/user';
+import { tryMacroId, useDisplayName } from '@core/user';
import { formatRelativeDate, isSameDay } from '@core/util/time';
import { ContextMenu } from '@kobalte/core/context-menu';
import { usePatchMessageMutation } from '@queries/channel/message';
@@ -149,11 +149,11 @@ export function MessageContainer(props: MessageProps) {
};
const userId = useUserId();
- const [currentUserName] = useDisplayName(userId());
+ const [currentUserName] = useDisplayName(tryMacroId(userId() ?? ''));
const attachmentStore = messageAttachmentsStore.get;
- const [displayName] = useDisplayName(message.sender_id);
+ const [displayName] = useDisplayName(tryMacroId(message.sender_id));
let messageContainerRef!: HTMLDivElement;
diff --git a/js/app/packages/block-email/component/BaseInput.tsx b/js/app/packages/block-email/component/BaseInput.tsx
index a67c0752cc..cda7bdd6a8 100644
--- a/js/app/packages/block-email/component/BaseInput.tsx
+++ b/js/app/packages/block-email/component/BaseInput.tsx
@@ -19,7 +19,7 @@ import { fileFolderDrop } from '@core/directive/fileFolderDrop';
import { TOKENS } from '@core/hotkey/tokens';
import { isMobileWidth } from '@core/mobile/mobileWidth';
import { trackMention } from '@core/signal/mention';
-import { useDisplayName } from '@core/user';
+import { tryMacroId, useDisplayName } from '@core/user';
import { handleFileFolderDrop } from '@core/util/upload';
import ArrowUp from '@icon/bold/arrow-up-bold.svg';
import Spinner from '@icon/bold/spinner-gap-bold.svg';
@@ -244,7 +244,7 @@ export function BaseInput(props: {
const userEmail = useEmail();
const userId = useUserId();
- const [userName] = useDisplayName(userId());
+ const [userName] = useDisplayName(tryMacroId(userId() ?? ''));
let bodyDiv!: HTMLDivElement;
let attachButtonRef!: HTMLDivElement;
diff --git a/js/app/packages/block-email/component/Compose.tsx b/js/app/packages/block-email/component/Compose.tsx
index c2f156383b..e636c76470 100644
--- a/js/app/packages/block-email/component/Compose.tsx
+++ b/js/app/packages/block-email/component/Compose.tsx
@@ -17,6 +17,7 @@ import { TOKENS } from '@core/hotkey/tokens';
import { useCombinedRecipients } from '@core/signal/useCombinedRecipient';
import {
type ContactInfo,
+ tryMacroId,
useDisplayName,
type WithCustomUserInput,
} from '@core/user';
@@ -211,7 +212,7 @@ export function EmailCompose() {
} else if (recipients.length === 1) {
const recipientName =
recipients[0].kind === 'user'
- ? useDisplayName(recipients[0].data.id)[0]()
+ ? useDisplayName(tryMacroId(recipients[0].data.id))[0]()
: recipients[0].data.email;
return recipientName ? `Email to ${recipientName}` : 'Draft email';
} else {
@@ -219,7 +220,7 @@ export function EmailCompose() {
.slice(0, 2)
.map((r) => {
if (r.kind === 'user') {
- return useDisplayName(r.data.id)[0]();
+ return useDisplayName(tryMacroId(r.data.id))[0]();
}
return r.data.email || 'Unknown';
})
diff --git a/js/app/packages/block-email/component/MessageContainer.tsx b/js/app/packages/block-email/component/MessageContainer.tsx
index 2ef884ae86..fb14d9cf95 100644
--- a/js/app/packages/block-email/component/MessageContainer.tsx
+++ b/js/app/packages/block-email/component/MessageContainer.tsx
@@ -10,7 +10,7 @@ import { Message } from '@core/component/Message';
import { toast } from '@core/component/Toast/Toast';
import { VideoPreview } from '@core/component/VideoPreview';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
-import { useDisplayName } from '@core/user';
+import { tryMacroId, useDisplayName } from '@core/user';
import { isErr } from '@core/util/maybeResult';
import { queryKeys, useQueryClient } from '@macro-entity';
import { logger } from '@observability';
@@ -60,7 +60,7 @@ export function MessageContainer(props: MessageContainerProps) {
const [showReply, setShowReply] = createSignal<boolean>(false);
const userId = useUserId();
- const [currentUserName] = useDisplayName(userId());
+ const [currentUserName] = useDisplayName(tryMacroId(userId() ?? ''));
const isBodyExpanded = createMemo(() => {
return props.expandedMessageBodyIds[props.message.db_id ?? ''];
diff --git a/js/app/packages/core/component/NotificationRenderer.tsx b/js/app/packages/core/component/NotificationRenderer.tsx
index 7933073e73..3926c7ca63 100644
--- a/js/app/packages/core/component/NotificationRenderer.tsx
+++ b/js/app/packages/core/component/NotificationRenderer.tsx
@@ -1,6 +1,6 @@
import { InlineItemPreview } from '@core/component/ItemPreview';
import { StaticMarkdown } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
-import { useDisplayName } from '@core/user/displayName';
+import { tryMacroId, useDisplayName } from '@core/user';
import { formatDate } from '@core/util/date';
import {
extractNotificationData,
@@ -32,7 +32,7 @@ export function NotificationRenderer(props: NotificationRendererProps) {
<Show when={data()}>
{(d) => {
const actorId = d().actor?.id ?? '';
- const [actorName] = useDisplayName(actorId);
+ const [actorName] = useDisplayName(tryMacroId(actorId));
const displayName = () => actorName() || 'Someone';
diff --git a/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx b/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx
index 66ce198eae..58d2a47e4e 100644
--- a/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx
+++ b/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx
@@ -4,7 +4,7 @@ import { EntityIcon } from '@core/component/EntityIcon';
import { UserIcon } from '@core/component/UserIcon';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
import type { ChannelWithParticipants, IUser } from '@core/user';
-import { idToEmail, useContacts, useDisplayName } from '@core/user';
+import { idToEmail, tryMacroId, useContacts, useDisplayName } from '@core/user';
import { createFreshSearch } from '@core/util/freshSort';
import CheckIcon from '@icon/bold/check-bold.svg';
import CompanyIcon from '@icon/duotone/building-duotone.svg';
@@ -219,7 +219,9 @@ export function PropertyEntitySelector(props: EntityInputProps) {
// Get current user info for injection into contacts
const currentUserId = useUserId();
- const [currentUserDisplayName] = useDisplayName(currentUserId());
+ const [currentUserDisplayName] = useDisplayName(
+ tryMacroId(currentUserId() ?? '')
+ );
// Contacts with current user injected at the beginning
const contactsWithCurrentUser = createMemo((): IUser[] => {
diff --git a/js/app/packages/core/component/References.tsx b/js/app/packages/core/component/References.tsx
index e50a346015..736d3a6679 100644
--- a/js/app/packages/core/component/References.tsx
+++ b/js/app/packages/core/component/References.tsx
@@ -9,7 +9,7 @@ import {
type PreviewItem,
useItemPreview,
} from '@core/signal/preview';
-import { useDisplayName } from '@core/user';
+import { tryMacroId, useDisplayName } from '@core/user';
import { isErr } from '@core/util/maybeResult';
import { useSplitNavigationHandler } from '@core/util/useSplitNavigationHandler';
import { commsServiceClient } from '@service-comms/client';
@@ -166,7 +166,7 @@ export function References(props: ReferenceProps) {
<For each={sortedReferences()}>
{(ref) => {
if (isChannelReference(ref)) {
- const [userName] = useDisplayName(ref.sender_id);
+ const [userName] = useDisplayName(tryMacroId(ref.sender_id));
const hasMessageContent =
ref.message_content && ref.message_content.trim().length > 0;
@@ -208,7 +208,7 @@ export function References(props: ReferenceProps) {
if (isGenericReference(ref)) {
const userId = ref.user_id!;
- const [userName] = useDisplayName(userId);
+ const [userName] = useDisplayName(tryMacroId(userId));
const [item] = useItemPreview({
id: ref.source_entity_id,
type: ref.source_entity_type as any,
diff --git a/js/app/packages/core/user/displayName.ts b/js/app/packages/core/user/displayName.ts
index ac8628a6e6..aa4238683e 100644
--- a/js/app/packages/core/user/displayName.ts
+++ b/js/app/packages/core/user/displayName.ts
@@ -3,8 +3,8 @@ import { isErr } from '@core/util/maybeResult';
import { authServiceClient } from '@service-auth/client';
import { createEffect, createSignal } from 'solid-js';
import { createStore, unwrap } from 'solid-js/store';
+import { type MacroId, macroIdToEmail } from './macroId';
import type { UserNameItem, UserNamePreviewFetcher } from './types';
-import { idToEmail } from './util';
const DEFAULT_CACHE_TIME_SECONDS = 60 * 10;
@@ -24,7 +24,8 @@ function queueItemsForFetch(items: string[]) {
}
function defaultNameTransform(item: UserNameItem): string {
- const email = idToEmail(item.id);
+ // TODO: UserNameItem needs to be ported to use MacroId
+ const email = macroIdToEmail(item.id as MacroId);
if (item.loading) return email;
@@ -93,7 +94,7 @@ async function batchFetchNames(ids: string[]) {
}
export function useDisplayName(
- id: string | undefined | null
+ id: MacroId | undefined | null
): UserNamePreviewFetcher {
if (!id) {
const dummy_accessor = () => {
diff --git a/js/app/packages/core/user/index.ts b/js/app/packages/core/user/index.ts
index b6c6654430..1c49bcc7c4 100644
--- a/js/app/packages/core/user/index.ts
+++ b/js/app/packages/core/user/index.ts
@@ -8,6 +8,13 @@ export {
} from './combinedRecipient';
export { useContacts } from './contactService';
export { useDisplayName } from './displayName';
+export {
+ emailToMacroId,
+ isMacroId,
+ type MacroId,
+ macroIdToEmail,
+ tryMacroId,
+} from './macroId';
export {
useOrganization,
useOrganizationId,
diff --git a/js/app/packages/core/user/macroId.ts b/js/app/packages/core/user/macroId.ts
new file mode 100644
index 0000000000..a61e83f15e
--- /dev/null
+++ b/js/app/packages/core/user/macroId.ts
@@ -0,0 +1,39 @@
+/**
+ * Branded type for MacroId strings.
+ * Format: `macro|[email protected]`
+ */
+declare const MacroIdBrand: unique symbol;
+export type MacroId = string & { readonly [MacroIdBrand]: never };
+
+/**
+ * Type guard to check if a string is a valid MacroId.
+ */
+export function isMacroId(str: string): str is MacroId {
+ return str.startsWith('macro|') && str.slice(6).includes('@');
+}
+
+/**
+ * Attempts to parse a string as a MacroId.
+ * Returns the MacroId if valid, undefined otherwise.
+ */
+export function tryMacroId(str: string): MacroId | undefined {
+ return isMacroId(str) ? str : undefined;
+}
+
+/**
+ * Extracts the email from a MacroId.
+ */
+export function macroIdToEmail(id: MacroId): string {
+ return id.slice(6);
+}
+
+/**
+ * Creates a MacroId from an email address.
+ * Returns undefined if the email is invalid.
+ */
+export function emailToMacroId(email: string): MacroId | undefined {
+ if (!email.includes('@')) {
+ return undefined;
+ }
+ return `macro|${email}` as MacroId;
+}
diff --git a/js/app/packages/core/user/util.ts b/js/app/packages/core/user/util.ts
index dccb59825c..810e9664a2 100644
--- a/js/app/packages/core/user/util.ts
+++ b/js/app/packages/core/user/util.ts
@@ -1,20 +1,35 @@
import type { ChannelParticipant } from '@service-comms/generated/models';
import { useDisplayName } from './displayName';
+import {
+ emailToMacroId,
+ type MacroId,
+ macroIdToEmail,
+ tryMacroId,
+} from './macroId';
import type { IUser } from './types';
+export { emailToMacroId, macroIdToEmail, tryMacroId, type MacroId };
+
// TODO: consolidate idToEmail, see idToEmail in email.ts
-/** Converts a user id to an email address */
+/**
+ * Converts a user id to an email address.
+ * @deprecated Use `macroIdToEmail` with a validated `MacroId` instead.
+ */
export function idToEmail(id: string): string {
return id.replace('macro|', '');
}
-/** Converts an email address to a user id */
+/**
+ * Converts an email address to a user id.
+ * @deprecated Use `emailToMacroId` instead for type-safe MacroId creation.
+ */
export function emailToId(email: string): string {
return `macro|${email}`;
}
export function idToDisplayName(id: string): string {
- const [displayName] = useDisplayName(id);
+ const macroId = tryMacroId(id);
+ const [displayName] = useDisplayName(macroId);
return displayName();
}
diff --git a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
index 144603ef75..eeecfddc1c 100644
--- a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
+++ b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
@@ -14,7 +14,7 @@ import { getIconConfig } from 'core/component/EntityIcon';
import { StaticMarkdown } from 'core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { unifiedListMarkdownTheme } from 'core/component/LexicalMarkdown/theme';
import { UserIcon } from 'core/component/UserIcon';
-import { emailToId, useDisplayName } from 'core/user';
+import { emailToMacroId, tryMacroId, useDisplayName } from 'core/user';
import type { ParentProps, Ref } from 'solid-js';
import {
createDeferred,
@@ -96,7 +96,7 @@ function GenericContentHit(props: { data: ContentHitData }) {
}
function ChannelMessageContentHit(props: { data: ChannelContentHitData }) {
- const [userName] = useDisplayName(props.data.senderId);
+ const [userName] = useDisplayName(tryMacroId(props.data.senderId));
return (
<div class="flex gap-2 items-center min-w-0">
@@ -268,7 +268,9 @@ function NotificationRow(props: {
onClick?: NotificationClickHandler;
entity: EntityData;
}) {
- const [userName] = useDisplayName(props.notification.senderId);
+ const [userName] = useDisplayName(
+ tryMacroId(props.notification.senderId ?? '')
+ );
const ActionContent = () => {
if (
@@ -537,7 +539,7 @@ export function EntityWithEverything(
if (!participant.email) return;
if (me && participant.email === me) return;
const macroDisplayName = useDisplayName(
- emailToId(participant.email)
+ emailToMacroId(participant.email)
)[0]?.();
const participantFullName = participant.name ?? '';
if (macroDisplayName && !isLikelyEmail(macroDisplayName)) {
@@ -687,7 +689,7 @@ export function EntityWithEverything(
const userNameFromSender = createMemo(() => {
const senderId = channelEntity()?.latestMessage?.senderId;
if (!senderId) return;
- const [userName] = useDisplayName(senderId);
+ const [userName] = useDisplayName(tryMacroId(senderId));
return userName();
});
@@ -844,7 +846,7 @@ export function EntityWithEverything(
return false;
}
return {
- ownerDisplayName: useDisplayName(props.entity.ownerId)[0],
+ ownerDisplayName: useDisplayName(tryMacroId(props.entity.ownerId))[0],
ownerId: props.entity.ownerId,
};
};
diff --git a/js/app/packages/notifications/notification-resolvers.ts b/js/app/packages/notifications/notification-resolvers.ts
index 37a7c898e4..b41a8e75f8 100644
--- a/js/app/packages/notifications/notification-resolvers.ts
+++ b/js/app/packages/notifications/notification-resolvers.ts
@@ -2,7 +2,7 @@ import type { BlockName } from '@core/block';
import { itemToResolvedBlockName } from '@core/constant/allBlocks';
import { isAccessiblePreviewItem, useItemPreview } from '@core/signal/preview';
import type { EntityType } from '@core/types';
-import { useDisplayName } from '@core/user/displayName';
+import { tryMacroId, useDisplayName } from '@core/user';
import type { ItemType } from '@service-storage/client';
import { raceTimeout, until } from '@solid-primitives/promise';
@@ -20,7 +20,7 @@ export type NotificationBlockNameResolver = (
const RESOLVER_TIMEOUT = 1000;
export const DefaultUserNameResolver: UserNameResolver = async (id: string) =>
- raceTimeout(until(useDisplayName(id)[0]), RESOLVER_TIMEOUT);
+ raceTimeout(until(useDisplayName(tryMacroId(id))[0]), RESOLVER_TIMEOUT);
const getPreview = async (id: string, type: EntityType) => {
const [preview] = useItemPreview({ id, type: type as ItemType });