Skip to content
Closed
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
149 changes: 148 additions & 1 deletion src/bundled/tasks/TaskView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const theirs = revision("c".repeat(64), "Their title");
const project = "33333333-3333-4333-8333-333333333333";
const launch = "44444444-4444-4444-8444-444444444444";
const leo = "c".repeat(64);
const riley = "9".repeat(64);
/** The relay's answer for `home`: Leo can see it, Riley can't. */
const relayReaders = async (_channel: string, pubkeys: readonly string[]) =>
new Map(pubkeys.map((key) => [key, key !== riley]));
const dm = "55555555-5555-4555-8555-555555555555";

type Answer = ChannelReachState;
Expand All @@ -50,13 +54,17 @@ const opened = (post: boolean): Answer => ({
function setup(
publish: () => Promise<string>,
answers: Record<string, Answer> = { [home]: opened(true) },
readers: RelaySession["channelReaders"] = relayReaders,
) {
const signed: { tags: string[][] }[] = [];
const channels: ChannelSummary[] = [
{ id: home, name: "design", channelType: "stream", viewerRole: "member" },
{ id: launch, name: "launch", channelType: "stream", readOnly: true },
];
const profiles = new Map([[leo, { name: "Leo" }]]);
const profiles = new Map([
[leo, { name: "Leo" }],
[riley, { name: "Riley" }],
]);
const list = { status: "ready", channels };
const metadata = (d: string, name: string, extra: string[][] = []) => ({
id: d.slice(0, 8).padEnd(64, "0"),
Expand Down Expand Up @@ -115,6 +123,7 @@ function setup(
["d", home],
["p", viewer],
["p", leo],
["p", riley],
],
},
].filter((event) =>
Expand All @@ -125,6 +134,7 @@ function setup(
? [theirs]
: [];
}),
channelReaders: readers,
configArtifactWriter: {
sign: vi.fn(async (template: { tags: string[][] }) => {
signed.push(template);
Expand Down Expand Up @@ -300,3 +310,140 @@ it("starts a new task from a DM with no place, and won't create until one is pic
fireEvent.click(create);
expect(signed).toHaveLength(0);
});

const assignedTo = (...people: string[]) =>
taskOf({
...revision("d".repeat(64), "Fix the header"),
tags: [
...revision("d".repeat(64), "Fix the header").tags,
...people.map((key) => ["assignee", key]),
],
}) as Task;

it("names someone the relay says lost access, and unassigns only them", async () => {
const { session, signed } = setup(async () => "");
const onSaved = vi.fn();
render(
<TaskView
session={session}
task={assignedTo(leo, riley)}
openConversation={() => {}}
onSaved={onSaved}
/>,
);
expect(
await screen.findByText("Riley can’t see this task anymore."),
).toBeInTheDocument();
expect(screen.queryByText(/Leo can’t see/)).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Unassign Riley" }));
await vi.waitFor(() => expect(onSaved).toHaveBeenCalled());
const tags = signed[0]?.tags ?? [];
expect(tags.filter(([name]) => name === "assignee")).toEqual([
["assignee", leo],
]);
});

it("offers to unassign several who lost access all at once", async () => {
const { session } = setup(
async () => "",
undefined,
async (_, pubkeys) => new Map(pubkeys.map((key) => [key, false])),
);
render(
<TaskView
session={session}
task={assignedTo(leo, riley)}
openConversation={() => {}}
onSaved={() => {}}
/>,
);
expect(
await screen.findByText("Leo and Riley can’t see this task anymore."),
).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Unassign all" }),
).toBeInTheDocument();
});

it("names someone who lost access to a view-only reader, with no way to unassign", async () => {
const { session } = setup(async () => "", { [home]: opened(false) });
render(
<TaskView
session={session}
task={assignedTo(riley)}
openConversation={() => {}}
onSaved={() => {}}
/>,
);
expect(
await screen.findByText("Riley can’t see this task anymore."),
).toBeInTheDocument();
expect(
screen.queryByRole("button", { name: /Unassign/ }),
).not.toBeInTheDocument();
});

it("offers only people the relay says can see the task", async () => {
const { session } = setup(async () => "");
render(
<TaskView
session={session}
task={mine}
openConversation={() => {}}
onSaved={() => {}}
/>,
);
fireEvent.click(await screen.findByRole("combobox", { name: "Assignees" }));
expect(await screen.findByRole("option", { name: /Leo/ })).toBeVisible();
expect(screen.queryByRole("option", { name: /Riley/ })).toBeNull();
});

it("says nothing about access, and offers everyone, when the relay can't say", async () => {
const { session } = setup(
async () => "",
undefined,
async () => {
throw new Error("not-allowed");
},
);
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
render(
<TaskView
session={session}
task={assignedTo(riley)}
openConversation={() => {}}
onSaved={() => {}}
/>,
);
fireEvent.click(await screen.findByRole("combobox", { name: "Assignees" }));
expect(await screen.findByRole("option", { name: /Leo/ })).toBeVisible();
expect(screen.queryByText(/can’t see/)).toBeNull();
warn.mockRestore();
});

it("unassigns on a move only those the relay says can't see the destination", async () => {
const { session, signed } = setup(async () => "", {
[home]: opened(true),
[launch]: opened(true),
});
const onSaved = vi.fn();
render(
<TaskView
session={session}
task={assignedTo(leo, riley)}
openConversation={() => {}}
onSaved={onSaved}
/>,
);
fireEvent.click(await screen.findByRole("button", { name: "Move…" }));
fireEvent.click(await screen.findByRole("combobox", { name: "Move to" }));
fireEvent.click(await screen.findByRole("option", { name: /launch/ }));
expect(
await screen.findByText(/Riley can’t see #launch and will be unassigned/),
).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Move" }));
await vi.waitFor(() => expect(signed).toHaveLength(1));
expect(
(signed[0]?.tags ?? []).filter(([name]) => name === "assignee"),
).toEqual([["assignee", leo]]);
});
116 changes: 96 additions & 20 deletions src/bundled/tasks/TaskView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ import {
type Place,
placeLabel,
projectOf,
readReaders,
readMembers,
whoCanRead,
} from "./places";
import { usePlaceAccess, usePlaces, useTaskPlaces } from "./react";
import { saveTask, type SaveTarget } from "./store";
Expand Down Expand Up @@ -161,6 +162,7 @@ function TaskDetails({
const places = usePlaces(session, [task.home]);
const project = projectOf(task, places);
const home = places.get(task.home);
const lostRead = useWhoCanRead(session, task.home, task.assignees);
// Immediate changes: status and assignees, on the revision shown.
const change = async (next: Partial<TaskFields>) => {
if (saving) return;
Expand Down Expand Up @@ -265,6 +267,18 @@ function TaskDetails({
</>
)}
</div>
<LostAccess
session={session}
assignees={task.assignees}
read={lostRead}
disabled={saving}
{...(canEdit && {
unassign: (lost: readonly string[]) =>
void change({
assignees: task.assignees.filter((key) => !lost.includes(key)),
}),
})}
/>
{task.description.trim() ? (
<MarkdownDocument
content={task.description}
Expand Down Expand Up @@ -378,6 +392,40 @@ function Assignees({

type Person = ReturnType<ReturnType<typeof usePeople>>;

/** Assignees the relay says can't see the task anymore, and to someone who
* can edit, a way to unassign them. Never why, and nothing when the relay
* can't say. */
function LostAccess({
session,
assignees,
read,
disabled,
unassign,
}: {
session: RelaySession;
assignees: readonly string[];
read: ReadonlyMap<string, boolean> | null | undefined;
disabled: boolean;
unassign?: (lost: readonly string[]) => void;
}) {
const person = usePeople(session, assignees);
const lost = assignees.filter((key) => read?.get(key) === false);
if (!lost.length) return null;
const names = listNames(lost.map((key) => person(key).name));
return (
<div className={styles.actions}>
<p className="text-body-sm text-subtle m-0">
{names} can’t see this task anymore.
</p>
{unassign ? (
<Button size="sm" disabled={disabled} onClick={() => unassign(lost)}>
{lost.length === 1 ? `Unassign ${names}` : "Unassign all"}
</Button>
) : null}
</div>
);
}

/** Up to four people, chosen from the people who can see the task. Agents
* are never offered: a person always answers for the work. */
function AssigneePicker({
Expand All @@ -393,12 +441,21 @@ function AssigneePicker({
disabled: boolean;
onChange(assignees: readonly string[]): void;
}) {
// Its members and those of the places it is filed under. An open place
// has more readers than these; the relay checks every assignee either way.
const readers = useReadersOf(session, home);
// The members the viewer can see here and where it is filed, less anyone
// the relay says can't see it. When it can't say, all of them: the relay
// refuses a bad save by name.
const members = useMembersOf(session, home);
const candidates = useMemo(
() => [...(members ?? [])].filter((key) => !assignees.includes(key)),
[members, assignees],
);
const read = useWhoCanRead(session, home, candidates);
const pubkeys = useMemo(
() => [...new Set([...assignees, ...(readers?.people ?? [])])],
[assignees, readers],
() => [
...assignees,
...candidates.filter((key) => read?.get(key) !== false),
],
[assignees, candidates, read],
);
const person = usePeople(session, pubkeys);
const [search, setSearch] = useState("");
Expand Down Expand Up @@ -727,14 +784,12 @@ function MoveTask({
const change = to ? accessChange(task.home, to, named) : undefined;
const destination = to && named.get(to);
const label = destination ? placeLabel(destination, named) : "that place";
const readers = useReadersOf(session, to);
// Only those the relay says can't see it; when it can't say, nobody.
const read = useWhoCanRead(session, to, task.assignees);
const person = usePeople(session, task.assignees);
const dropped =
readers && !readers.anyone
? task.assignees.filter((pubkey) => !readers.people.has(pubkey))
: [];
const dropped = task.assignees.filter((key) => read?.get(key) === false);
const problem = to ? placeProblem(target.access, label) : "";
const ready = !!to && problem === undefined && !!readers;
const ready = !!to && problem === undefined && read !== undefined;
const move = async () => {
if (!to || !ready) return;
setPending(true);
Expand Down Expand Up @@ -838,21 +893,42 @@ const placeList = (
const listNames = (names: readonly string[]) =>
new Intl.ListFormat("en", { type: "conjunction" }).format(names);

/** Who can see what lives in `home`; undefined until read, or with no home. */
function useReadersOf(session: RelaySession, home: string | undefined) {
const [readers, setReaders] =
useState<Awaited<ReturnType<typeof readReaders>>>();
/** People to offer for a task in `home`, from the member lists the viewer
* can read; undefined until read, or with no home. */
function useMembersOf(session: RelaySession, home: string | undefined) {
const [members, setMembers] = useState<ReadonlySet<string>>();
useEffect(() => {
setReaders(undefined);
setMembers(undefined);
if (!home) return;
const controller = new AbortController();
readReaders(session, home, controller.signal).then(
(read) => !controller.signal.aborted && setReaders(read),
readMembers(session, home, controller.signal).then(
(read) => !controller.signal.aborted && setMembers(read),
() => {},
);
return () => controller.abort();
}, [session, home]);
return readers;
return members;
}

/** The relay's answer to which of `pubkeys` can see `place`: undefined while
* asking, null when it can't say, so nobody is flagged or unassigned. */
function useWhoCanRead(
session: RelaySession,
place: string | undefined,
pubkeys: readonly string[],
) {
const [read, setRead] = useState<ReadonlyMap<string, boolean> | null>();
const key = pubkeys.join(",");
useEffect(() => {
setRead(undefined);
if (!place) return;
const controller = new AbortController();
whoCanRead(session, place, key ? key.split(",") : [], controller.signal)
.then((answer) => !controller.signal.aborted && setRead(answer ?? null))
.catch(() => {});
return () => controller.abort();
}, [session, place, key]);
return read;
}

function TaskEditor({
Expand Down
Loading
Loading