Skip to content

Commit 45437ef

Browse files
committed
fix(tasks): a wide screen opens the first task that needs you
Arriving at Tasks on a screen wide enough to show a task beside the list drew an empty "No task open" pane while something waited on the person. The first task in Needs you now opens there instead. Only on arriving: closing a task later leaves the pane empty, and a narrow screen, where the list has the screen to itself, keeps the list.
1 parent 60e4ade commit 45437ef

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎app/src/lib/tasks/groups.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ export function taskGroups(input: {
200200
});
201201
}
202202

203+
/** The task at the top of Needs you, as the list draws it, or undefined when nothing waits. */
204+
export function firstNeedingYou(
205+
tasks: readonly TaskRecord[] | undefined,
206+
inbox: ApprovalInboxData | undefined,
207+
): TaskRecord | undefined {
208+
return taskGroups({
209+
tasks: tasks ?? [],
210+
requests: inbox?.requests,
211+
questions: inbox?.questions,
212+
}).find((group) => group.id === "needs_input")?.items[0]?.task;
213+
}
214+
203215
/** Where one task stands and what it needs next, worked out as its row in the list would be. */
204216
export function taskItem(
205217
task: TaskRecord,

‎app/src/routes/_authed/_app/tasks.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IconPlus, IconSearch } from "@tabler/icons-react";
2+
import { useQuery } from "@tanstack/react-query";
23
import {
34
createFileRoute,
45
Outlet,
@@ -20,6 +21,9 @@ import {
2021
InputGroupAddon,
2122
InputGroupInput,
2223
} from "@/components/ui/input-group";
24+
import { approvalInboxOptions } from "@/lib/approvals";
25+
import { firstNeedingYou } from "@/lib/tasks/groups";
26+
import { taskListQueryOptions } from "@/lib/tasks/queries";
2327
import { cn } from "@/lib/utils";
2428

2529
export const Route = createFileRoute("/_authed/_app/tasks")({
@@ -51,6 +55,10 @@ const somethingOpen = () =>
5155
* closed decides it too: the two sit side by side once there are 56rem to share (a 360px list and a
5256
* conversation a comfortable width beside it).
5357
*
58+
* Arriving with no task open on a screen wide enough to show one beside the list, the first task
59+
* that needs the person opens, instead of an empty pane. Only on arriving: closing a task later
60+
* leaves the pane empty, as the person asked.
61+
*
5462
* While the open task's computer is open beside it, the list folds away to give the two the room.
5563
*
5664
* From the keyboard: ↓ and ↑, or j and k, move through the list, Enter opens a task, and Escape closes
@@ -69,6 +77,30 @@ function TasksLayout() {
6977
const [draft, setDraft] = useState<TaskDraft>(EMPTY_DRAFT);
7078
const [filter, setFilter] = useState("");
7179
const list = useRef<HTMLDivElement>(null);
80+
const pane = useRef<HTMLDivElement>(null);
81+
const tasks = useQuery(taskListQueryOptions());
82+
const inbox = useQuery(approvalInboxOptions());
83+
/** Whether arriving here has been dealt with: a task opened, or the pane left as it was. */
84+
const arrived = useRef(false);
85+
86+
useEffect(() => {
87+
if (arrived.current) return;
88+
if (taskId) {
89+
arrived.current = true;
90+
return;
91+
}
92+
if (!tasks.data) return;
93+
arrived.current = true;
94+
// The pane is drawn beside the list, not folded away on a narrow screen.
95+
if ((pane.current?.getBoundingClientRect().width ?? 0) <= 0) return;
96+
const first = firstNeedingYou(tasks.data, inbox.data);
97+
if (first)
98+
void navigate({
99+
to: "/tasks/$taskId",
100+
params: { taskId: first.id },
101+
replace: true,
102+
});
103+
}, [taskId, tasks.data, inbox.data, navigate]);
72104

73105
useEffect(() => {
74106
const onKey = (event: KeyboardEvent) => {
@@ -156,6 +188,7 @@ function TasksLayout() {
156188
taskId ? "flex" : "hidden",
157189
)}
158190
data-slot="task-pane"
191+
ref={pane}
159192
>
160193
<Outlet />
161194
</div>

‎app/tests/tasks-screen.test.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,39 @@ test("a row opens its task beside the list, and is lit while it is open", async
308308
).toBe("page");
309309
});
310310

311+
test("arriving on a wide screen opens the first task that needs you, once; a narrow one keeps the list", async () => {
312+
serve({ tasks: everyState });
313+
// Narrow, as happy-dom lays it out: the pane is folded away, so nothing opens.
314+
const narrow = draw();
315+
await narrow.view.findByText("Reply to Rosa");
316+
expect(await narrow.view.findByText("No task open")).toBeTruthy();
317+
expect(narrow.router.state.location.pathname).toBe("/tasks");
318+
cleanup();
319+
320+
const measure = HTMLElement.prototype.getBoundingClientRect;
321+
HTMLElement.prototype.getBoundingClientRect = function (this: HTMLElement) {
322+
return this.dataset.slot === "task-pane"
323+
? ({ width: 600, height: 800 } as DOMRect)
324+
: measure.call(this);
325+
};
326+
try {
327+
const { view, router, user } = draw();
328+
await waitFor(() =>
329+
expect(router.state.location.pathname).toBe("/tasks/ask"),
330+
);
331+
expect(
332+
await view.findByRole("heading", { name: "Open task ask" }),
333+
).toBeTruthy();
334+
// Closing it leaves the pane empty, as asked: it does not open again.
335+
await user.keyboard("{Escape}");
336+
await waitFor(() => expect(router.state.location.pathname).toBe("/tasks"));
337+
expect(await view.findByText("No task open")).toBeTruthy();
338+
expect(router.state.location.pathname).toBe("/tasks");
339+
} finally {
340+
HTMLElement.prototype.getBoundingClientRect = measure;
341+
}
342+
});
343+
311344
test("a done task opened from elsewhere keeps Done unfolded, so its row is in view", async () => {
312345
serve({ tasks: everyState });
313346
const { view } = draw("/tasks/done");

0 commit comments

Comments
 (0)