Skip to content

The open-source app everyone uses to manage agents at work

10.4 KB
Raw Blame
import { useState } from "react";
import type { Meta, StoryObj } from "@storybook/react-vite";
import { expect, userEvent, within, waitFor } from "storybook/test";
import { IssueShareSheet } from "@/components/IssueShareSheet";
import { Button } from "@/components/ui/button";
import { privacyCompanyId } from "../../../fixtures/privateTasks";
import {
  mobile,
  privacyDecorator,
  privacyParameters,
  StoryFrame,
  usePrivacyStory,
} from "../../../support/private-tasks/PrivacyStory";

function Sharing({
  add = "",
  implicit = false,
  child = false,
}: {
  add?: string;
  implicit?: boolean;
  child?: boolean;
}) {
  const state = usePrivacyStory();
  const [open, setOpen] = useState(true);
  return (
    <StoryFrame
      title={
        child ? "Share only the research subtask" : "Who can access this task"
      }
      story={
        child
          ? "As the CEO, I share a research subtask with Morgan while keeping its parent briefing and sibling compensation task private."
          : "As the owner, I can inspect why each person or agent has access, share the task, or remove a saved grant."
      }
      checks={[
        "Owner, explicit, assignment, and project grants have distinct source badges.",
        "Inherited access is managed at its source. Removing one saved grant does not cancel other access.",
        "A child grant includes that child's descendants; it gives no access to ancestors or siblings.",
      ]}
    >
      <Button onClick={() => setOpen(true)}>Open sharing</Button>
      <IssueShareSheet
        issueId={child ? "privacy-child" : "privacy-root"}
        companyId={privacyCompanyId}
        canManage={state.options.role !== "reader"}
        open={open}
        onOpenChange={setOpen}
        initialView={add ? "add" : "list"}
        initialAddSelection={add === "choose" ? "" : add}
        implicitPrincipals={
          implicit
            ? [
                {
                  id: "user:user-board",
                  displayName: "Avery Chen",
                  roleLabel: "Owner",
                },
                {
                  id: "agent:agent-dedicated",
                  displayName: "Executive assistant",
                  roleLabel: "Current assignee",
                },
              ]
            : []
        }
      />
    </StoryFrame>
  );
}
const meta = {
  id: "private-tasks-02-sharing",
  title: "Getting started and access/Login, invitations, and access/Private tasks/02 Sharing",
  decorators: [privacyDecorator],
  parameters: { ...privacyParameters, docs: { description: { component: "Task menu → Share → Add someone. Inspect avatars in search and selected values, inherited grants, reader access, confirmations, empty/loading/error states and mobile. The sticky-access paragraph is removed." } },
    paperclip: {
      "capabilities": [
        "access/resources",
        "tasks/properties"
      ],
      "implementation": "production",
      "boundary": "fixtures",
      "notes": "The filter uses one lowercase label without helper text. All chat-channel tasks count as bot-created; the focused visibility stories cover human-initiated channel work."
    },
  },
  render: () => <Sharing />,
} satisfies Meta;
export default meta;
type Story = StoryObj<typeof meta>;
export const AllAccessSources: Story = { parameters: { privacy: { taskProject: true } } };
export const OwnerAndCurrentAssignee: Story = {
  render: () => <Sharing implicit />,
};
export const OnlyYou: Story = { parameters: { privacy: { grants: "empty" } } };
export const ReadOnlyMember: Story = {
  parameters: { privacy: { role: "reader" } },
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await page.findByText("Morgan Reed");
    await expect(
      page.queryByRole("button", { name: "Add someone" }),
    ).not.toBeInTheDocument();
    await expect(
      page.queryByRole("button", { name: "Revoke" }),
    ).not.toBeInTheDocument();
  },
};
export const InheritedAccess: Story = {
  parameters: { privacy: { grants: "inherited" } },
  render: () => <Sharing child />,
};
export const StickyAssignmentAfterUnassignment: Story = {
  parameters: { privacy: { grants: "assignment" } },
};
export const RemoveCurrentAssigneesSavedGrant: Story = {
  parameters: { privacy: { grants: "assignment" } },
  render: () => <Sharing implicit />,
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await userEvent.click(
      await page.findByRole("button", { name: "Remove saved grant" }),
    );
    await expect(await page.findByRole("alertdialog")).toHaveTextContent(
      "current assignment",
    );
  },
};
export const RevokeConfirmation: Story = {
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await userEvent.click(
      (await page.findAllByRole("button", { name: "Revoke" }))[0]!,
    );
    await expect(await page.findByRole("alertdialog")).toHaveTextContent(
      "descendants",
    );
  },
};
export const RevokeFailure: Story = {
  parameters: { privacy: { failure: "revoke" } },
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await userEvent.click(
      (await page.findAllByRole("button", { name: "Revoke" }))[0]!,
    );
    await userEvent.click(
      await page.findByRole("button", { name: "Remove grant" }),
    );
    await expect(await page.findByRole("alertdialog")).toBeVisible();
  },
};
export const ChoosePersonOrAgent: Story = {
  parameters: { privacy: { grants: "empty" } },
  render: () => <Sharing add="choose" />,
};
export const ShareChildWithPerson: Story = {
  parameters: { privacy: { grants: "empty" } },
  render: () => <Sharing add="user:user-product" child />,
};
export const DedicatedAgent: Story = {
  parameters: { privacy: { grants: "empty" } },
  render: () => <Sharing add="agent:agent-dedicated" />,
};
export const SharedAgentCaution: Story = {
  parameters: { privacy: { grants: "empty" } },
  render: () => <Sharing add="agent:agent-shared" />,
  play: async ({ canvasElement }) => {
    await expect(
      await within(canvasElement.ownerDocument.body).findByRole("note", {
        name: "Shared agent caution",
      }),
    ).toHaveTextContent("residual private context");
  },
};
export const LegacyAgentCaution: Story = {
  parameters: { privacy: { grants: "empty" } },
  render: () => <Sharing add="agent:agent-unspecified" />,
};
export const AddFailure: Story = {
  parameters: { privacy: { grants: "empty", failure: "add" } },
  render: () => <Sharing add="user:user-product" />,
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await userEvent.keyboard("{Escape}");
    await userEvent.click(await page.findByRole("button", { name: "Add" }));
    await waitFor(() =>
      expect(page.getByText("Couldn't add access")).toBeVisible(),
    );
  },
};
export const Loading: Story = {
  parameters: { privacy: { loading: "grants" } },
};
export const LoadFailure: Story = {
  parameters: { privacy: { failure: "grants" } },
};
export const RetryRecovers: Story = {
  parameters: { privacy: { failure: "grants", retryOnce: true } },
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    await userEvent.click(await page.findByRole("button", { name: "Retry" }));
    await expect(await page.findByText("Morgan Reed")).toBeVisible();
  },
};
export const LongNamesMobile: Story = {
  globals: mobile,
  parameters: { privacy: { grants: "long" } },
};
export const MobileSharing: Story = { globals: mobile };
export const LightSharing: Story = { globals: { theme: "light" } };

export const SearchPeopleAndAgents: Story = {
  render: () => <Sharing add="choose" />,
  parameters: { privacy: { grants: "empty" }, docs: { description: { story: "Task sharing search: human avatar or initials and agent character avatars stay visible in results and after selection." } } },
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    const dialog = await page.findByRole("dialog", { name: "Who can access this task" });
    const trigger = within(dialog).getByRole("combobox");
    if (trigger.getAttribute("aria-expanded") !== "true") await userEvent.click(trigger);
    await expect(await page.findByRole("option", { name: "Executive assistant" })).toBeVisible();
  },
};
export const MobileSearchPeopleAndAgents: Story = { ...SearchPeopleAndAgents, globals: mobile };

export const AddDirectGrantAlongsideInheritedAccess: Story = {
  parameters: { privacy: { grants: "inherited" }, docs: { description: { story: "Give Morgan an independent grant to this child before removing parent access. The dropdown includes people whose current access is inherited." } } },
  render: () => <Sharing add="choose" child />,
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    const dialog = await page.findByRole("dialog", { name: "Who can access this task" });
    const trigger = within(dialog).getByRole("combobox");
    if (trigger.getAttribute("aria-expanded") !== "true") await userEvent.click(trigger);
    await userEvent.click(await page.findByRole("option", { name: "Morgan Reed" }));
    await userEvent.click(page.getByRole("button", { name: "Add" }));
    await page.findByText("Access granted");
    await waitFor(() => expect(page.getAllByText("Morgan Reed")).toHaveLength(2));
    await expect((await page.findAllByRole("button", { name: "Revoke" }))[0]!).toBeEnabled();
  },
};
export const MobileIndependentChildGrant: Story = { ...AddDirectGrantAlongsideInheritedAccess, globals: mobile };

export const CurrentAssigneeAlreadyHasSavedAccess: Story = {
  parameters: { privacy: { grants: "assignment" }, docs: { description: { story: "A direct assignment grant is already saved on this task. The API deduplicates subjects, so the add picker does not promise a second independent grant for the same assignee." } } },
  render: () => <Sharing add="choose" />,
  play: async ({ canvasElement }) => {
    const page = within(canvasElement.ownerDocument.body);
    const dialog = await page.findByRole("dialog", { name: "Who can access this task" });
    const trigger = within(dialog).getByRole("combobox");
    if (trigger.getAttribute("aria-expanded") !== "true") await userEvent.click(trigger);
    await page.findByRole("option", { name: "Morgan Reed" });
    await expect(page.queryByRole("option", { name: "Executive assistant" })).not.toBeInTheDocument();
  },
};