Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ jobs:
app/tests/browse-steps-layout.test.tsx
app/tests/chunk-recovery.test.tsx
app/tests/command-card-layout.test.tsx
app/tests/component-bots-layout.test.tsx
app/tests/composer-dictation-language.test.tsx
app/tests/composer-keeps-draft-browser.test.tsx
app/tests/composer-touch-return.test.tsx
Expand Down
193 changes: 193 additions & 0 deletions app/src/components/manage/bot-switch-rows.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { Fragment, type ReactNode } from "react";
import { IconChevronDown, IconUser, IconUsers } from "@/components/icons";
import { PageRows } from "@/components/layout/page-shell";
import { PersonAvatar } from "@/components/organizations/person-avatar";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemMedia,
ItemTitle,
} from "@/components/ui/item";
import { Separator } from "@/components/ui/separator";
import { Switch } from "@/components/ui/switch";
import {
type BotEntry,
botEntries,
botWithOwner,
EVERYONE,
onePerPerson,
ownerLabel,
ownerName,
YOURS,
} from "@/lib/agents/owners";
import type { AgentProfile } from "@/lib/agents/queries";

/**
* An address has no spaces to wrap at, so on a phone it ran off its row. It breaks anywhere instead,
* and the line clamp ends it with an ellipsis.
*/
const ADDRESS = "wrap-anywhere";

/**
* A switch per Bot, on Admin's screens that list every Bot in the organization, with each row saying
* whose the Bot is.
*
* Every person has their own copy of the Bots OpenBot starts with, all under one name, so a plain
* list of names drew "General Assistant" once per person and an admin couldn't tell whose they were
* switching. Here a Bot of its own names its owner under its name, and the copies of one Bot are
* gathered under a row that opens onto a switch per person (`botEntries`).
*/
export function BotSwitchRows({
bots,
isOn,
onChange,
busy,
testId,
}: {
bots: readonly AgentProfile[];
isOn: (bot: AgentProfile) => boolean;
onChange: (bot: AgentProfile, next: boolean) => void;
/** Whether this Bot's own write is in flight, so switching one Bot doesn't freeze the rest. */
busy?: (bot: AgentProfile) => boolean;
/** The test id of a Bot's switch. */
testId?: (bot: AgentProfile) => string;
}) {
const entries = botEntries(bots);
const toggle = (bot: AgentProfile) => (
<Switch
aria-label={botWithOwner(bot)}
checked={isOn(bot)}
data-testid={testId?.(bot)}
disabled={busy?.(bot) ?? false}
onCheckedChange={(next) => onChange(bot, next)}
/>
);
return (
<PageRows>
{entries.map((entry, index) => (
<Fragment
key={entry.kind === "bot" ? entry.bot.id : `copies:${entry.name}`}
>
{index === 0 ? null : <Separator />}
{entry.kind === "bot" ? (
<Item data-bot={entry.bot.id} size="sm">
<OwnerMedia bot={entry.bot} />
<ItemContent>
<ItemTitle>{entry.bot.name}</ItemTitle>
<ItemDescription className={ADDRESS}>
{ownerLabel(entry.bot)}
</ItemDescription>
</ItemContent>
<ItemActions>{toggle(entry.bot)}</ItemActions>
</Item>
) : (
<Copies entry={entry} isOn={isOn} toggle={toggle} />
)}
</Fragment>
))}
</PageRows>
);
}

/**
* People's Bots of one name, most often each person's own copy of a Bot OpenBot starts with: a row
* saying how many there are and how many are on, which opens onto a row per person. No switch for
* them all: each is its owner's, and an admin decides for them one at a time.
*/
function Copies({
entry,
isOn,
toggle,
}: {
entry: Extract<BotEntry, { kind: "copies" }>;
isOn: (bot: AgentProfile) => boolean;
toggle: (bot: AgentProfile) => ReactNode;
}) {
const count = entry.bots.length;
const on = entry.bots.filter(isOn).length;
const state =
on === count
? `On for all ${count}.`
: on === 0
? `Off for all ${count}.`
: `On for ${on} of ${count}.`;
return (
<Collapsible data-copies={entry.name}>
<Item
className="w-full text-left transition-colors hover:bg-muted/50"
/* No children on the render element: they would replace the row's own. */
render={<CollapsibleTrigger />}
size="sm"
>
<ItemMedia variant="icon">
<IconUsers />
</ItemMedia>
<ItemContent>
<ItemTitle>{entry.name}</ItemTitle>
<ItemDescription>
{onePerPerson(entry.bots)
? `${count} people each have one.`
: `${count} Bots with this name.`}{" "}
{state}
</ItemDescription>
</ItemContent>
<ItemActions>
<IconChevronDown className="size-4 text-muted-foreground transition-transform in-data-[panel-open]:rotate-180" />
</ItemActions>
</Item>
<CollapsibleContent>
{/* Indented to the gathering row's title, so each person's row reads as one of its copies. */}
<div className="pl-11">
{entry.bots.map((bot) => {
const name = ownerName(bot);
return (
<Fragment key={bot.id}>
<Separator />
<Item data-bot={bot.id} size="sm">
<OwnerMedia bot={bot} />
<ItemContent>
<ItemTitle className={ADDRESS}>
{name ?? ownerLabel(bot)}
</ItemTitle>
{/* Yours says so; anybody else's says the address that tells two Sams apart. */}
{bot.mine && name ? (
<ItemDescription>{YOURS}</ItemDescription>
) : !bot.mine && bot.owner?.name?.trim() ? (
<ItemDescription className={ADDRESS}>
{bot.owner.email}
</ItemDescription>
) : null}
</ItemContent>
<ItemActions>{toggle(bot)}</ItemActions>
</Item>
</Fragment>
);
})}
</div>
</CollapsibleContent>
</Collapsible>
);
}

/** The owner's picture or initials; a mark for everyone's Bot, or for one whose owner wasn't said. */
function OwnerMedia({ bot }: { bot: AgentProfile }) {
if (bot.owner)
return (
<ItemMedia>
<PersonAvatar person={bot.owner} />
</ItemMedia>
);
const everyone = ownerLabel(bot) === EVERYONE;
return (
<ItemMedia variant="icon">
{everyone ? <IconUsers /> : <IconUser />}
</ItemMedia>
);
}
100 changes: 100 additions & 0 deletions app/src/components/manage/component-bots-page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { IconBolt } from "@/components/icons";
import { PageEmpty, PageShell } from "@/components/layout/page-shell";
import { BotSwitchRows } from "@/components/manage/bot-switch-rows";
import { useBotListScope } from "@/lib/agents/list-scope";
import { agentListQueryOptions } from "@/lib/agents/queries";
import { setComponentGrantMutationOptions } from "@/lib/components/mutations";
import { componentListQueryOptions } from "@/lib/components/queries";
import { failureSaid, loadFailure } from "@/lib/load-failure";
import { queryClient } from "@/query-client";

const BOTS_FAILED = "Couldn't load your Bots. Try again in a moment.";
const COMPONENTS_FAILED = "Couldn't load components. Try again in a moment.";

/**
* Which Bots may answer with one component: a switch per Bot, each row saying whose the Bot is.
*
* Its own screen, behind the component's "Available to" row. It was a dialog over the component's
* page, which listed every Bot in the organization by name alone: each person's own copy of a Bot
* OpenBot starts with came out as one more "General Assistant", and in an organization of a few
* hundred people the list ran past the dialog's footer. Each switch takes effect as it is switched.
*/
export function ComponentBotsPage({ name }: { name: string }) {
const components = useQuery(componentListQueryOptions());
// Under Admin, every Bot in the organization, each with its owner (lib/agents/list-scope.tsx).
const agents = useQuery(agentListQueryOptions(false, useBotListScope()));
const setGrant = useMutation(setComponentGrantMutationOptions(queryClient));

const back = {
label: "UI components",
linkProps: { to: "/admin/components" },
} as const;

// Nothing while the components are read: this app draws no loading placeholder.
if (components.isPending) return null;
if (components.error)
return (
<PageShell backButton={back} title="Available to">
<p className="text-destructive text-sm" role="alert">
{loadFailure(components.error, COMPONENTS_FAILED)}
</p>
</PageShell>
);

const component = components.data.find((entry) => entry.name === name);
// A name in the address can be stale or mistyped, as on the component's own page.
if (!component)
return (
<PageShell
backButton={back}
description="There is no component with that name."
title="No such component"
>
<PageEmpty title={name}>
It may have been renamed, or OpenBot no longer ships it.
</PageEmpty>
</PageShell>
);

const withheld = new Set(component.withheldFrom);

return (
<PageShell
backButton={{
label: component.title,
linkProps: { to: "/admin/components/$name", params: { name } },
}}
description="Turn a Bot off and it never learns this component exists. Changes apply right away."
title="Available to"
>
{setGrant.error ? (
<p className="text-destructive text-sm" role="alert">
{failureSaid(setGrant.error)}
</p>
) : null}
{/* A roster that couldn't be read is not a roster of none. */}
{agents.isPending ? null : agents.error ? (
<p className="text-destructive text-sm" role="alert">
{loadFailure(agents.error, BOTS_FAILED)}
</p>
) : agents.data.length === 0 ? (
<PageEmpty icon={IconBolt} title="No Bots yet">
Bots appear here once someone creates one.
</PageEmpty>
) : (
<BotSwitchRows
bots={agents.data}
busy={(bot) =>
setGrant.isPending && setGrant.variables?.agentId === bot.id
}
isOn={(bot) => !withheld.has(bot.id)}
onChange={(bot, granted) =>
setGrant.mutate({ agentId: bot.id, granted, name })
}
testId={(bot) => `grant-${name}-${bot.id}`}
/>
)}
</PageShell>
);
}
Loading
Loading