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(bots): show all bot channels (#5307)

Will Hutchinsone7a9e3a93239
diff --git a/apps/web/src/features/channel/Bots/BotCreatePage.tsx b/apps/web/src/features/channel/Bots/BotCreatePage.tsx
index bedd22edb2..cca93ac6b6 100644
--- a/apps/web/src/features/channel/Bots/BotCreatePage.tsx
+++ b/apps/web/src/features/channel/Bots/BotCreatePage.tsx
@@ -11,7 +11,6 @@ import {
useAddBotToChannelsMutation,
useCreateChannelScopedBotMutation,
} from '@queries/channel/channel-bots';
-import { ChannelTypeEnum } from '@service-storage/client';
import type { Bot } from '@service-storage/generated/schemas/bot';
import { Button } from '@ui';
import { createMemo, createSignal, Show } from 'solid-js';
@@ -19,6 +18,7 @@ import { createStore } from 'solid-js/store';
import { BotCreationResult } from './BotCreationResult';
import { BotFormSection } from './BotFormSection';
import { BotProfileFields } from './BotProfileFields';
+import { botAssignableChannelOptions } from './botChannelOptions';
import {
type BotFormErrors,
EMPTY_BOT_FORM,
@@ -52,14 +52,7 @@ export function BotCreate(props: { channelId?: string; onBack: () => void }) {
);
const channelOptions = createMemo(() =>
- channelsContext
- .channels()
- .filter((channel) => channel.channel_type === ChannelTypeEnum.Private)
- .map((channel) => ({
- id: channel.id,
- name: channel.name?.trim() || 'Unnamed channel',
- }))
- .sort((a, b) => a.name.localeCompare(b.name))
+ botAssignableChannelOptions(channelsContext.channels())
);
const resultChannels = createMemo(() => {
const selected = new Set(createdChannelIds());
diff --git a/apps/web/src/features/channel/Bots/BotCreationResult.tsx b/apps/web/src/features/channel/Bots/BotCreationResult.tsx
index 679511b885..c1f2099207 100644
--- a/apps/web/src/features/channel/Bots/BotCreationResult.tsx
+++ b/apps/web/src/features/channel/Bots/BotCreationResult.tsx
@@ -4,7 +4,7 @@ import type { Bot } from '@service-storage/generated/schemas/bot';
import { Button, Surface } from '@ui';
import { For, Show } from 'solid-js';
import { BotAvatar } from './BotAvatar';
-import type { BotChannelOption } from './ChannelMultiSelect';
+import type { BotChannelOption } from './botChannelOptions';
import { CredentialField } from './CredentialField';
import { channelWebhookUrl, webhookExample } from './webhook';
diff --git a/apps/web/src/features/channel/Bots/BotDetailPage.tsx b/apps/web/src/features/channel/Bots/BotDetailPage.tsx
index 7f500e3ec1..ec14f291f1 100644
--- a/apps/web/src/features/channel/Bots/BotDetailPage.tsx
+++ b/apps/web/src/features/channel/Bots/BotDetailPage.tsx
@@ -18,6 +18,7 @@ import { BotDetailActions } from './BotDetailActions';
import { BotFormSection } from './BotFormSection';
import { BotProfileFields } from './BotProfileFields';
import { BotWebhooksSection } from './BotWebhooksSection';
+import { sameChannelSelection } from './botChannelOptions';
import {
type BotFormErrors,
botToFormValues,
@@ -53,14 +54,23 @@ export function BotDetail(props: { botId: string; onBack: () => void }) {
on(
[() => botQuery.data, () => botChannelsQuery.data],
([bot, channels]) => {
- if (initialized() || !bot || channels === undefined) return;
- const nextForm = botToFormValues(bot);
- const nextChannelIds = channels.map((channel) => channel.channel_id);
- setForm(nextForm);
- setInitialForm(nextForm);
- setChannelIds(nextChannelIds);
- setInitialChannelIds(nextChannelIds);
- setInitialized(true);
+ if (!bot || channels === undefined) return;
+ const serverChannelIds = channels.map((channel) => channel.channel_id);
+ if (!initialized()) {
+ const nextForm = botToFormValues(bot);
+ setForm(nextForm);
+ setInitialForm(nextForm);
+ setChannelIds(serverChannelIds);
+ setInitialChannelIds(serverChannelIds);
+ setInitialized(true);
+ return;
+ }
+ // Later refetches (e.g. the bot joined a channel elsewhere) refresh
+ // the selection, but never clobber unsaved edits.
+ if (!sameChannelSelection(channelIds(), initialChannelIds())) return;
+ if (sameChannelSelection(serverChannelIds, channelIds())) return;
+ setChannelIds(serverChannelIds);
+ setInitialChannelIds(serverChannelIds);
}
)
);
@@ -82,12 +92,8 @@ export function BotDetail(props: { botId: string; onBack: () => void }) {
form.handle === original.handle &&
form.description === original.description &&
form.avatarUrl === original.avatarUrl;
- const originalChannels = [...initialChannelIds()].sort();
- const nextChannels = [...channelIds()].sort();
return (
- !sameForm ||
- originalChannels.length !== nextChannels.length ||
- originalChannels.some((id, index) => id !== nextChannels[index])
+ !sameForm || !sameChannelSelection(initialChannelIds(), channelIds())
);
});
const pending = () =>
@@ -245,6 +251,7 @@ export function BotDetail(props: { botId: string; onBack: () => void }) {
>
<ChannelMultiSelect
channelIds={channelIds()}
+ assignedChannels={assignedChannels()}
onChange={setChannelIds}
disabled={saving()}
/>
diff --git a/apps/web/src/features/channel/Bots/ChannelMultiSelect.tsx b/apps/web/src/features/channel/Bots/ChannelMultiSelect.tsx
index 33aedcea24..972c0e9d3d 100644
--- a/apps/web/src/features/channel/Bots/ChannelMultiSelect.tsx
+++ b/apps/web/src/features/channel/Bots/ChannelMultiSelect.tsx
@@ -7,11 +7,13 @@ import CaretDownIcon from '@phosphor/caret-down.svg';
import CheckIcon from '@phosphor/check.svg';
import HashIcon from '@phosphor/hash.svg';
import XIcon from '@phosphor/x.svg';
-import { ChannelTypeEnum } from '@service-storage/client';
import { Surface } from '@ui';
import { type Component, createMemo, For, Show } from 'solid-js';
-
-export type BotChannelOption = { id: string; name: string };
+import {
+ type BotChannelOption,
+ botAssignableChannelOptions,
+ mergeChannelOptions,
+} from './botChannelOptions';
const ChannelItem: Component<
ComboboxRootItemComponentProps<BotChannelOption>
@@ -32,19 +34,16 @@ const ChannelItem: Component<
export function ChannelMultiSelect(props: {
channelIds: string[];
+ assignedChannels?: BotChannelOption[];
onChange: (channelIds: string[]) => void;
disabled?: boolean;
}) {
const channelsContext = useChannelsContext();
const options = createMemo<BotChannelOption[]>(() =>
- channelsContext
- .channels()
- .filter((channel) => channel.channel_type === ChannelTypeEnum.Private)
- .map((channel) => ({
- id: channel.id,
- name: channel.name?.trim() || 'Unnamed channel',
- }))
- .sort((a, b) => a.name.localeCompare(b.name))
+ mergeChannelOptions(
+ props.assignedChannels ?? [],
+ botAssignableChannelOptions(channelsContext.channels())
+ )
);
const selectedOptions = createMemo(() => {
const selected = new Set(props.channelIds);
diff --git a/apps/web/src/features/channel/Bots/botChannelOptions.ts b/apps/web/src/features/channel/Bots/botChannelOptions.ts
new file mode 100644
index 0000000000..812722bce2
--- /dev/null
+++ b/apps/web/src/features/channel/Bots/botChannelOptions.ts
@@ -0,0 +1,59 @@
+import type { ApiChannelWithLatest } from '@service-storage/channel-list-types';
+import { ChannelType } from '@service-storage/generated/schemas/channelType';
+
+/** A channel a bot can belong to, as shown in bot settings pickers. */
+export type BotChannelOption = { id: string; name: string };
+
+type AssignableChannel = Pick<
+ ApiChannelWithLatest,
+ 'id' | 'name' | 'channel_type'
+>;
+
+/**
+ * Channels offered when assigning a bot — private and team channels, the same
+ * types whose participants panel allows managing bots.
+ */
+export function botAssignableChannelOptions(
+ channels: AssignableChannel[]
+): BotChannelOption[] {
+ return channels
+ .filter(
+ (channel) =>
+ channel.channel_type === ChannelType.private ||
+ channel.channel_type === ChannelType.team
+ )
+ .map((channel) => ({
+ id: channel.id,
+ name: channel.name?.trim() || 'Unnamed channel',
+ }))
+ .sort((a, b) => a.name.localeCompare(b.name));
+}
+
+/**
+ * Union of the channels a bot is already in and the channels the caller can
+ * assign. Assigned channels stay listed even when they fall outside the
+ * caller's assignable set (e.g. a private channel the caller is not in);
+ * dropping them would hide their chips and silently remove those assignments
+ * on the next save.
+ */
+export function mergeChannelOptions(
+ assigned: BotChannelOption[],
+ assignable: BotChannelOption[]
+): BotChannelOption[] {
+ const byId = new Map<string, BotChannelOption>();
+ for (const option of assigned) {
+ byId.set(option.id, option);
+ }
+ for (const option of assignable) {
+ byId.set(option.id, option);
+ }
+ return [...byId.values()].sort((a, b) => a.name.localeCompare(b.name));
+}
+
+/** Order-insensitive equality of two channel-id selections. */
+export function sameChannelSelection(a: string[], b: string[]): boolean {
+ if (a.length !== b.length) return false;
+ const sortedA = [...a].sort();
+ const sortedB = [...b].sort();
+ return sortedA.every((id, index) => id === sortedB[index]);
+}
diff --git a/apps/web/src/features/channel/Bots/tests/bot-channel-options.test.ts b/apps/web/src/features/channel/Bots/tests/bot-channel-options.test.ts
new file mode 100644
index 0000000000..83422d0266
--- /dev/null
+++ b/apps/web/src/features/channel/Bots/tests/bot-channel-options.test.ts
@@ -0,0 +1,53 @@
+import { ChannelType } from '@service-storage/generated/schemas/channelType';
+import { describe, expect, it } from 'vitest';
+import {
+ botAssignableChannelOptions,
+ mergeChannelOptions,
+ sameChannelSelection,
+} from '../botChannelOptions';
+
+const channel = (
+ id: string,
+ name: string | null,
+ channelType: ChannelType
+) => ({ id, name, channel_type: channelType });
+
+describe('bot channel options', () => {
+ it('offers private and team channels only, sorted by name', () => {
+ expect(
+ botAssignableChannelOptions([
+ channel('c-public', 'Announcements', ChannelType.public),
+ channel('c-dm', 'Direct message', ChannelType.direct_message),
+ channel('c-team', ' Team ops ', ChannelType.team),
+ channel('c-private', 'Alpha', ChannelType.private),
+ channel('c-unnamed', ' ', ChannelType.private),
+ ])
+ ).toEqual([
+ { id: 'c-private', name: 'Alpha' },
+ { id: 'c-team', name: 'Team ops' },
+ { id: 'c-unnamed', name: 'Unnamed channel' },
+ ]);
+ });
+
+ it('keeps assigned channels missing from the assignable list', () => {
+ expect(
+ mergeChannelOptions(
+ [
+ { id: 'c-hidden', name: 'Hidden channel' },
+ { id: 'c-shared', name: 'Stale name' },
+ ],
+ [{ id: 'c-shared', name: 'Fresh name' }]
+ )
+ ).toEqual([
+ { id: 'c-shared', name: 'Fresh name' },
+ { id: 'c-hidden', name: 'Hidden channel' },
+ ]);
+ });
+
+ it('compares channel selections ignoring order', () => {
+ expect(sameChannelSelection([], [])).toBe(true);
+ expect(sameChannelSelection(['a', 'b'], ['b', 'a'])).toBe(true);
+ expect(sameChannelSelection(['a'], ['a', 'b'])).toBe(false);
+ expect(sameChannelSelection(['a', 'b'], ['a', 'c'])).toBe(false);
+ });
+});