8000 Header destinations and the Callout's verdicts by kiran-brahma · Pull Request #82 · kiran-brahma/writing-tool · GitHub
[go: up one dir, main page]

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
48 changes: 39 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { AiSettingsView } from "./settings/AiSettingsView";
import { useColorScheme } from "./settings/useColorScheme";
import { WorkbenchView } from "./workbench/WorkbenchView";
import { DocumentMenu } from "./DocumentMenu";
import { HelpMenu } from "./HelpMenu";
import {
DESTINATIONS,
destinationsAt,
isCurrentDestination,
type DestinationId,
} from "./navigation";
Expand Down Expand Up @@ -418,21 +419,49 @@ export default function App() {

return (
<div className="flex min-h-screen flex-col bg-ground text-ink">
<header className="flex items-center justify-between gap-3 sm:gap-4 border-b border-rule-soft px-3 sm:px-6 py-2.5 whitespace-nowrap overflow-x-auto min-w-0 bg-ground text-ink">
{/*
The header's three zones: brand, document actions, navigation. It wraps
rather than scrolling sideways, so nothing clips the Document menu or
the help control's list when they open.
*/}
<header className="flex flex-wrap items-center justify-between gap-x-3 gap-y-2 sm:gap-x-4 border-b border-rule-soft px-3 sm:px-6 py-2.5 whitespace-nowrap bg-ground text-ink">
<div className="shrink-0">
<h1 className="text-base font-semibold tracking-tight text-ink">Obelus</h1>
<p className="hidden sm:block text-xs text-faint-ink">It marks; it never holds the pen.</p>
</div>
<div className="flex items-center gap-2 sm:gap-4 shrink-0">
<div className="flex flex-wrap items-center gap-x-2 gap-y-2 sm:gap-x-4">
<DocumentMenu
wordCount={document?.wordCount ?? 0}
>
>
canExport={document !== null}
/>
<div className="h-4 w-px bg-rule" aria-hidden="true" />
<nav aria-label="Main navigation" className="flex items-center gap-0.5 sm:gap-1 shrink-0">
{DESTINATIONS.map(({ id, label }) => {
<div className="hidden sm:block h-4 w-px bg-rule" aria-hidden="true" />
<nav aria-label="Main navigation" className="flex items-center gap-1 sm:gap-2">
{/* Stories 244–245: Editor and Library are the tabs; the tools follow, quieter. */}
<div className="flex items-center gap-0.5 sm:gap-1">
{destinationsAt("tab").map(({ id, label }) => {
const isCurrent = isCurrentDestination(id, view);
return (
<button
key={id}
id={`nav-dest-${id}`}
type="button"
aria-current={isCurrent ? "page" : undefined}
=> navigateTo(id)}
className={[
"rounded px-2.5 sm:px-3 py-1 text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus",
isCurrent
? "bg-ink font-semibold text-on-ink focus-visible:ring-offset-2"
: "font-medium text-quiet-ink hover:bg-sunk-strong hover:text-ink",
].join(" ")}
>
{label}
</button>
);
})}
</div>
{destinationsAt("link").map(({ id, label }) => {
const isCurrent = isCurrentDestination(id, view);
return (
<button
Expand All @@ -442,16 +471,17 @@ export default function App() {
aria-current={isCurrent ? "page" : undefined}
=> navigateTo(id)}
className={[
"rounded px-2 sm:px-2.5 py-1 text-xs font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus",
"rounded px-1.5 sm:px-2 py-1 text-xs underline-offset-4 transition-colors hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus",
isCurrent
? "bg-ink font-semibold text-on-ink shadow-xs focus-visible:ring-offset-2"
: "text-muted-ink hover:bg-sunk-strong/70 hover:text-ink",
? "font-semibold text-ink underline decoration-rule-strong"
: "font-medium text-faint-ink",
8000 ].join(" ")}
>
{label}
</button>
);
})}
<HelpMenu view={view} />
</nav>
</div>
</header>
Expand Down
104 changes: 104 additions & 0 deletions src/HelpMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { useEffect, useRef, useState, type FocusEvent } from "react";
import {
destinationsAt,
isCurrentDestination,
isHelpCurrent,
type DestinationId,
} from "./navigation";

export interface HelpMenuProps {
view: DestinationId;
onNavigate: (destination: DestinationId) => void;
}

/**
* Story 246: How this works and Privacy under one help control, so they are
* always reachable without taking header space. A disclosure rather than a
* menu: its two destinations follow the button in the tab order, Escape closes
* it and returns focus to the button, and a press or focus outside closes it.
*/
export function HelpMenu({ view, onNavigate }: HelpMenuProps) {
const [open, setOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
const current = isHelpCurrent(view);

// Any change of destination closes it, including `?` opening the shortcuts.
useEffect(() => setOpen(false), [view]);

useEffect(() => {
if (!open) return;
const KeyboardEvent) => {
if (event.key !== "Escape") return;
setOpen(false);
buttonRef.current?.focus();
};
const PointerEvent) => {
if (!(event.target instanceof Node)) return;
if (containerRef.current?.contains(event.target)) return;
setOpen(false);
};
window.addEventListener("keydown", onKeyDown);
window.addEventListener("pointerdown", onPointerDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
window.removeEventListener("pointerdown", onPointerDown);
};
}, [open]);

const FocusEvent<HTMLDivElement>) => {
const next = event.relatedTarget;
if (next instanceof Node && containerRef.current?.contains(next)) return;
if (next === null) return; // focus left the page, or a press is handled above
setOpen(false);
};

return (
<div ref={containerRef} className="relative shrink-0">
<button
ref={buttonRef}
type="button"
aria-expanded={open}
aria-controls="help-destinations"
=> setOpen((previous) => !previous)}
className={[
"flex items-center gap-1 rounded px-2 py-1 text-xs transition-colors hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus",
current ? "font-semibold text-ink" : "font-medium text-faint-ink",
].join(" ")}
>
<span>Help</span>
<span aria-hidden="true" className="text-ghost-ink">
▾
</span>
</button>

<ul
id="help-destinations"
hidden={!open}
className="absolute right-0 top-full z-30 mt-1.5 w-40 rounded border border-rule-soft bg-paper py-1 shadow-md"
>
{destinationsAt("help").map(({ id, label }) => {
const isCurrent = isCurrentDestination(id, view);
return (
<li key={id}>
<button
type="button"
aria-current={isCurrent ? "page" : undefined}
=> {
setOpen(false);
onNavigate(id);
}}
className={[
"flex w-full items-center px-3 py-1.5 text-left text-xs transition-colors hover:bg-ground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-focus",
isCurrent ? "font-semibold text-ink" : "text-quiet-ink",
].join(" ")}
>
{label}
</button>
</li>
);
})}
</ul>
</div>
);
}
37 changes: 31 additions & 6 deletions src/editor/FindingCallout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ function CalloutEntry({ finding, onAddress, onDecline }: CalloutEntryProps) {
</p>
)}
{rewrites.length > 0 && <QuarantinedRewrite violations={rewrites} />}
<p className="mt-2 flex gap-3 text-xs">
<CalloutAction => onAddress(finding.id)}>Addressed</CalloutAction>
<CalloutAction => onDecline(finding.id, "advice")}>Decline</CalloutAction>
{/*
Stories 248–250: the verdicts ranked by how often they are given, and no
key hints — the queue keys act on the Current Finding, not on this one
(ADR 0011).
*/}
<p className="mt-2.5 flex flex-wrap items-center gap-2 text-xs">
<CalloutAction rank="primary" => onAddress(finding.id)}>
Addressed
</CalloutAction>
<CalloutAction rank="secondary" => onDecline(finding.id, "advice")}>
Decline
</CalloutAction>
{violations.length > 0 && (
<CalloutAction => onDecline(finding.id, "violation")}>
<CalloutAction rank="quiet" => onDecline(finding.id, "violation")}>
Decline as violation
</CalloutAction>
)}
Expand All @@ -124,12 +133,28 @@ function CalloutEntry({ finding, onAddress, onDecline }: CalloutEntryProps) {
);
}

function CalloutAction({ onClick, children }: { onClick: () => void; children: string }) {
/** Addressed is a filled button, Decline an outlined one, Decline as violation a text link. */
const CALLOUT_ACTION_RANK = {
primary:
"border border-ink bg-ink px-2.5 py-1 font-semibold text-on-ink hover:bg-soft-ink focus-visible:ring-offset-2",
secondary: "border border-rule px-2.5 py-1 text-quiet-ink hover:bg-sunk hover:text-ink",
quiet: "px-1 py-1 text-muted-ink underline underline-offset-2 hover:text-ink",
} as const;

function CalloutAction({
rank,
onClick,
children,
}: {
rank: keyof typeof CALLOUT_ACTION_RANK;
onClick: () => void;
children: string;
}) {
return (
<button
type="button"
>
className="rounded font-medium text-muted-ink underline-offset-2 hover:text-ink hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus"
className={`rounded font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus ${CALLOUT_ACTION_RANK[rank]}`}
>
{children}
</button>
Expand Down
47 changes: 41 additions & 6 deletions src/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
import { describe, expect, it } from "vitest";
import {
clicksBetween,
currentDestination,
DESTINATIONS,
destinationsAt,
isCurrentDestination,
isHelpCurrent,
type DestinationId,
} from "./navigation";

describe("destination model", () => {
it("covers all six destinations with the expected labels in persistent order", () => {
expect(DESTINATIONS).toHaveLength(6);
expect(DESTINATIONS).toEqual([
{ id: "editor", label: "Editor" },
{ id: "library", label: "Library" },
{ id: "workbench", label: "Pass workbench" },
{ id: "settings", label: "AI Settings" },
{ id: "help", label: "How this works" },
{ id: "privacy", label: "Privacy" },
{ id: "editor", label: "Editor", placement: "tab" },
{ id: "library", label: "Library", placement: "tab" },
{ id: "workbench", label: "Pass workbench", placement: "link" },
{ id: "settings", label: "AI Settings", placement: "link" },
{ id: "help", label: "How this works", placement: "help" },
{ id: "privacy", label: "Privacy", placement: "help" },
]);
});

it("makes Editor and Library the tabs, the tools quieter links, and help one control", () => {
expect(destinationsAt("tab").map((d) => d.id)).toEqual(["editor", "library"]);
expect(destinationsAt("link").map((d) => d.id)).toEqual(["workbench", "settings"]);
expect(destinationsAt("help").map((d) => d.id)).toEqual(["help", "privacy"]);
});

it("reaches every destination from every other in at most two clicks", () => {
for (const from of DESTINATIONS) {
for (const to of DESTINATIONS) {
const clicks = clicksBetween(from.id, to.id);
expect(clicks).toBeLessThanOrEqual(2);
expect(clicks === 0).toBe(from.id === to.id);
}
}
});

it("costs one click for a tab or a link and two for a destination under help", () => {
expect(clicksBetween("privacy", "editor")).toBe(1);
expect(clicksBetween("help", "library")).toBe(1);
expect(clicksBetween("editor", "workbench")).toBe(1);
expect(clicksBetween("library", "settings")).toBe(1);
expect(clicksBetween("editor", "help")).toBe(2);
expect(clicksBetween("help", "privacy")).toBe(2);
});

it("marks the help control current only while How this works or Privacy is open", () => {
expect(DESTINATIONS.filter((d) => isHelpCurrent(d.id)).map((d) => d.id)).toEqual([
"help",
"privacy",
]);
});

Expand Down
44 changes: 38 additions & 6 deletions src/navigation.ts
7036
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
* Navigation is persistent across every destination: clicking a destination
* moves straight to it, so no view needs to remember where it was entered from
* and the app needs no "Back to..." buttons.
*
* Spec v1.3 (stories 244–247) ranks them in the header: Editor and Library are
* the tabs, where the work is; the Pass workbench and AI Settings follow as
* quieter links; How this works and Privacy sit under one help control.
*/

export type DestinationId =
Expand All @@ -14,20 +18,29 @@ export type DestinationId =
| "help"
| "privacy";

/** Where a destination sits in the header's navigation zone. */
export type DestinationPlacement = "tab" | "link" | "help";

export interface Destination {
readonly id: DestinationId;
readonly label: string;
readonly placement: DestinationPlacement;
}

export const DESTINATIONS: readonly Destination[] = [
{ id: "editor", label: "Editor" },
{ id: "library", label: "Library" },
{ id: "workbench", label: "Pass workbench" },
{ id: "settings", label: "AI Settings" },
{ id: "help", label: "How this works" },
{ id: "privacy", label: "Privacy" },
{ id: "editor", label: "Editor", placement: "tab" },
{ id: "library", label: "Library", placement: "tab" },
{ id: "workbench", label: "Pass workbench", placement: "link" },
{ id: "settings", label: "AI Settings", placement: "link" },
{ id: "help", label: "How this works", placement: "help" },
{ id: "privacy", label: "Privacy", placement: "help" },
] as const;

/** The destinations at one placement, in persistent order. */
export function destinationsAt(placement: DestinationPlacement): Destination[] {
return DESTINATIONS.filter((destination) => destination.placement === placement);
}

/**
* Returns whether a destination is the current view.
*/
Expand All @@ -38,6 +51,25 @@ export function isCurrentDestination(
return destinationId === currentView;
}

/**
* Whether the help control holds the current view, so it reads as the Writer's
* place while How this works or Privacy is open.
*/
export function isHelpCurrent(currentView: DestinationId): boolean {
return currentDestination(currentView).placement === "help";
}

/**
* Story 247: the clicks the header needs to move from one destination to
* another. The header is on every destination, so the cost depends only on
* where the target sits: a tab or a link is one click, a destination under the
* help control two (open it, choose). Staying put costs nothing.
*/
export function clicksBetween(from: DestinationId, to: DestinationId): 0 | 1 | 2 {
if (from === to) return 0;
return currentDestination(to).placement === "help" ? 2 : 1;
}

/**
* Looks up the Destination record for a view.
*/
Expand Down
0