From 41d6dc9cd712e5211916469842c6f869ecbeaedd Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Sun, 15 Nov 2026 11:56:24 +0700 Subject: [PATCH] Reorder and turn off Usage cards, with matching sheet header and host picker controls (#9660) * feat(app): reorder and turn off usage cards Usage cards default to alphabetical order. Drag a card by its handle to reorder it; the handle shows on hover on web and is always visible on native and compact layouts. A card's switch turns it off: the card collapses to its header and its windows leave the sidebar summary. Order and off state are device-local usage preferences. Inside a native bottom sheet, a drag gesture can now keep the touch: the sheet's content pan waits for the list's drag pan (react-native-draggable-flatlist gains `dragGestureRef`), and the sheet scroll stops while a drag is active. * feat(app): one icon button for every sheet header The sheet header's back arrow, close X and actions now render through SheetHeaderIconButton, which takes the header island chrome CompactHeader uses: 26pt frame and 16pt glyph on pointer, 44pt and 20pt on touch, muted colour, hover fill and tooltip. Edge buttons hang into the row padding so their glyphs sit on the content rail; the row keeps its 64pt height and the trailing actions shrink before the title collapses. Migrates the diagnostic, import session, model browser, changelog, plugin logs and sheet pane actions, and moves the tool-call and tool-call overview sheets onto SheetHeaderView. The Usage refresh and options buttons size their glyph from the header scope. adaptive-modal-sheet and the migrated sheets leave the raw-pressable burn-down list. * feat(app): host picker trigger and Usage host placement HostFilter renders the shared ComboboxTrigger, as in Settings, and truncates long host names. Sessions, Schedules, plugin surfaces, the labels manager and Usage pick this up together. The Usage modal puts the host picker at the top of its body on every form factor. The header is title, Refresh all, Settings, X, so the title stays whole on a phone. --- .oxlintrc.json | 7 +- docs/design.md | 2 + packages/app/e2e/browser/usage-modal.spec.ts | 65 +++++- .../e2e/support/helpers/usage-sidebar-item.ts | 48 +++- .../changelog/internal/changelog-sheet.tsx | 30 +-- .../adaptive-modal-sheet.browser.test.tsx | 76 ++++++ .../src/components/adaptive-modal-sheet.tsx | 218 +++++++++--------- .../src/components/app-diagnostic-sheet.tsx | 91 ++------ .../components/drag-reorder/drag-hold.test.ts | 45 ++++ .../src/components/drag-reorder/drag-hold.ts | 51 ++++ .../drag-reorder/native-drop.test.ts | 14 ++ .../components/drag-reorder/native-drop.ts | 17 ++ .../drag-reorder/use-drag-reorder-state.ts | 7 +- .../src/components/draggable-list.native.tsx | 105 ++++++++- .../src/components/draggable-list.types.ts | 17 +- .../draggable-list.web.browser.test.tsx | 110 +++++++++ .../app/src/components/draggable-list.web.tsx | 61 ++++- .../app/src/components/host-status-dot.tsx | 6 +- .../app/src/components/hosts/host-filter.tsx | 111 ++++++--- .../components/import-session-sheet.test.tsx | 19 ++ .../src/components/import-session-sheet.tsx | 60 ++--- packages/app/src/components/model-browser.tsx | 47 +--- .../components/provider-diagnostic-sheet.tsx | 72 ++---- .../components/sortable-inline-list.web.tsx | 2 +- .../app/src/components/tool-call-sheet.tsx | 60 +---- .../src/components/ui/bottom-sheet-scope.tsx | 46 +++- .../src/components/ui/dismiss-backdrop.tsx | 6 + .../src/components/ui/icon-button-chrome.ts | 20 ++ .../index.test.tsx | 72 +++++- .../ui/isolated-bottom-sheet-modal/index.tsx | 29 ++- .../app/src/components/ui/scroll-view.tsx | 46 +++- .../ui/text-input/text-input.native.test.tsx | 4 +- packages/app/src/git/jump-to-file/index.tsx | 12 +- .../src/hooks/use-settings/storage.test.ts | 8 + packages/app/src/i18n/resources/ar.ts | 2 + packages/app/src/i18n/resources/en.ts | 2 + packages/app/src/i18n/resources/es.ts | 2 + packages/app/src/i18n/resources/fr.ts | 2 + packages/app/src/i18n/resources/ja.ts | 2 + packages/app/src/i18n/resources/ko.ts | 2 + packages/app/src/i18n/resources/pt-BR.ts | 2 + packages/app/src/i18n/resources/ru.ts | 2 + packages/app/src/i18n/resources/zh-CN.ts | 2 + .../app/src/screens/settings/plugins-page.tsx | 23 +- .../workspace/sheet-pane/sheet-pane.tsx | 2 +- .../detail-level/overview/sheet.tsx | 68 ++---- packages/app/src/usage/agent-usage.tsx | 6 +- packages/app/src/usage/card.tsx | 151 +++++++++++- packages/app/src/usage/cards.test.ts | 153 ++++++++++++ packages/app/src/usage/controls.tsx | 69 +++--- packages/app/src/usage/display.ts | 31 ++- packages/app/src/usage/format.ts | 7 +- packages/app/src/usage/list.tsx | 128 ++++++++-- packages/app/src/usage/options.tsx | 13 +- packages/app/src/usage/pinned.test.ts | 28 ++- packages/app/src/usage/pinned.ts | 24 +- packages/app/src/usage/preferences.test.ts | 12 +- packages/app/src/usage/preferences.ts | 100 +++++++- packages/app/src/usage/refresh-button.tsx | 13 +- packages/app/src/usage/usage-modal.tsx | 12 +- .../src/workspace-labels/manager-modal.tsx | 1 + ...eact-native-draggable-flatlist+4.0.3.patch | 47 ++-- .../codex-usage-source/server/usage.test.ts | 5 +- 63 files changed, 1831 insertions(+), 664 deletions(-) create mode 100644 packages/app/src/components/adaptive-modal-sheet.browser.test.tsx create mode 100644 packages/app/src/components/drag-reorder/drag-hold.test.ts create mode 100644 packages/app/src/components/drag-reorder/drag-hold.ts create mode 100644 packages/app/src/components/drag-reorder/native-drop.test.ts create mode 100644 packages/app/src/components/drag-reorder/native-drop.ts create mode 100644 packages/app/src/components/draggable-list.web.browser.test.tsx create mode 100644 packages/app/src/components/ui/dismiss-backdrop.tsx create mode 100644 packages/app/src/usage/cards.test.ts diff --git a/.oxlintrc.json b/.oxlintrc.json index ae335c0e4..fdbb91730 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -530,7 +530,7 @@ } }, // Press feedback is owned by the primitives in components/ui. Raw pressable burn-down: - // 94 files. They may only leave this list. + // 85 files. They may only leave this list. { "files": [ "packages/app/src/components/ui/**/*.{ts,tsx}", @@ -539,12 +539,9 @@ "packages/app/src/agent-skills/selection-sheet.tsx", "packages/app/src/agent-stream/chat-outline/rail.web.tsx", "packages/app/src/agent-stream/view.tsx", - "packages/app/src/changelog/internal/changelog-sheet.tsx", "packages/app/src/command-center/command-center.tsx", - "packages/app/src/components/adaptive-modal-sheet.tsx", "packages/app/src/components/add-project-flow.tsx", "packages/app/src/components/agent-list.tsx", - "packages/app/src/components/app-diagnostic-sheet.tsx", "packages/app/src/components/attachment-lightbox.tsx", "packages/app/src/components/attachment-pill.tsx", "packages/app/src/components/combined-model-selector.tsx", @@ -583,7 +580,6 @@ "packages/app/src/components/terminal-emulator-native-grid.native.tsx", "packages/app/src/components/terminal-pane.tsx", "packages/app/src/components/toast-host.tsx", - "packages/app/src/components/tool-call-sheet.tsx", "packages/app/src/components/welcome-screen.tsx", "packages/app/src/components/workspace-hover-card.tsx", "packages/app/src/components/zoomable-viewport/toolbar.tsx", @@ -619,7 +615,6 @@ "packages/app/src/screens/workspace/scripts/workspace-scripts-button.tsx", "packages/app/src/screens/workspace/workspace-desktop-tabs-row.tsx", "packages/app/src/screens/workspace/workspace-tab-presentation.tsx", - "packages/app/src/tool-calls/detail-level/overview/sheet.tsx", "packages/app/src/usage/sidebar-item.tsx", "packages/app/src/usage/window-bar.tsx", "packages/app/src/workspace-labels/manager-modal.tsx", diff --git a/docs/design.md b/docs/design.md index c9b1a9842..7cd1eb519 100644 --- a/docs/design.md +++ b/docs/design.md @@ -114,6 +114,8 @@ Five primitives. The pick is determined by option count, the need to search, and `` owns the presentation. Its content inset — the gutter that puts sheet content on the same rails as the sheet header — and compact bottom safe-area padding are the sheet's, not the caller's. A caller declares layout intent through `contentStyle` and never branches on form factor to add its own margins. If a sheet's first snap point is shorter than its header, content, and safe-area clearance, raise that snap point rather than moving the sheet container. +The sheet header (`SheetHeaderView`) is the only sheet title row. A sheet that brings its own Gorhom modal still renders it. Back, Close and every action passed through `header.actions` are `SheetHeaderIconButton`, so all of them get the header island's chrome, as `CompactHeader` does: one frame, glyph, colour and hover. The first and last buttons hang into the row's padding, so their glyphs sit on the content rail. + `confirmDialog` is for destructive yes/no and imperative confirmation. Promise-based: `await confirmDialog({ destructive: true, ... })`. Anything where a wrong click loses work. Three themes is `DropdownMenu`. Thirty hosts is `Combobox`. A label and a value is `AdaptiveModalSheet`. "Are you sure?" is `confirmDialog`. diff --git a/packages/app/e2e/browser/usage-modal.spec.ts b/packages/app/e2e/browser/usage-modal.spec.ts index cdf21e56d..fc25ff00c 100644 --- a/packages/app/e2e/browser/usage-modal.spec.ts +++ b/packages/app/e2e/browser/usage-modal.spec.ts @@ -33,7 +33,10 @@ import { pinRow, openUsageOptions, closeUsageOptions, + dragUsageCardAbove, + expectUsageCardOrder, expectUsageOptionsFitContent, + setUsageCardOn, showUsageAs, refreshAllUsage, togglePin, @@ -108,6 +111,14 @@ function weeklyReport(sourceId: string, usedPct: number): UsageReportEntry { }; } +function accountReport(sourceId: string, account: string, usedPct: number): UsageReportEntry { + return { + ...weeklyReport(sourceId, usedPct), + id: `${sourceId}:${account}`, + account: { label: account }, + }; +} + function installReversedUsageRefresh(page: Page) { const initial = [weeklyReport("opencode", 31), weeklyReport("openai", 12)]; const refreshed = [weeklyReport("openai", 22), weeklyReport("opencode", 41)]; @@ -120,15 +131,15 @@ async function openUsageSummaryAtWidth(page: Page, width: number) { await page.setViewportSize({ width, height: 900 }); await gotoAppShell(page); if (width === 390) await openCompactSidebar(page); - await expectPinnedUsage(page, ["31% Weekly", "12% Weekly"]); + await expectPinnedUsage(page, ["12% Weekly", "31% Weekly"]); await openUsageFromItem(page); } async function expectUsageProviderOrder(page: Page, summary: string[]) { const group = page.getByTestId(`usage-host-${getServerId()}`); await expect(group.getByText(/^(opencode|openai) plan$/)).toHaveText([ - "opencode plan", "openai plan", + "opencode plan", ]); await expectPinnedUsage(page, summary); } @@ -200,15 +211,61 @@ test.describe("usage modal", () => { }) => { const usage = await installReversedUsageRefresh(page); await openUsageSummaryAtWidth(page, width); - await expectUsageProviderOrder(page, ["31% Weekly", "12% Weekly"]); + // Reports arrive opencode first; cards and the summary stay alphabetical. + await expectUsageProviderOrder(page, ["12% Weekly", "31% Weekly"]); await qaScreenshot(page, `usage-order-${width}-before-refresh`); await refreshUsageAndWaitForReports(page, usage); - await expectUsageProviderOrder(page, ["41% Weekly", "22% Weekly"]); + await expectUsageProviderOrder(page, ["22% Weekly", "41% Weekly"]); await qaScreenshot(page, `usage-order-${width}-after-refresh`); }); } + test("keeps a card turned off and a dragged order after a reload", async ({ page }) => { + test.setTimeout(120_000); + await installUsageReportsFixture(page, { + lists: [ + [ + accountReport("beta", "work", 12), + weeklyReport("alpha", 31), + accountReport("beta", "home", 20), + ], + ], + }); + await gotoAppShell(page); + // Three windows leave no room for labels in the summary, so it reads percents. + await expectPinnedUsage(page, ["31%", "20%", "12%"]); + await openUsageFromItem(page); + await expectUsageCardOrder(page, ["alpha:a", "beta:home", "beta:work"]); + + // Without the pointer over the card, keyboard focus on its handle shows the handle. + await page.mouse.move(0, 0); + const alphaHandle = page + .getByTestId("usage-report-alpha:a") + .getByRole("button", { name: "Reorder alpha plan", exact: true }); + await expect(alphaHandle).toHaveCSS("opacity", "0"); + await alphaHandle.focus(); + await expect(alphaHandle).toHaveCSS("opacity", "1"); + await alphaHandle.blur(); + + // The off card collapses to its header, which still names the account. + await setUsageCardOn(page, "beta plan (work)", false); + const work = page.getByTestId("usage-report-beta:work"); + await expect(work.getByText("12% used")).toHaveCount(0); + await expect(work.getByText("work", { exact: true })).toBeVisible(); + await expectPinnedUsage(page, ["31% Weekly", "20% Weekly"]); + + await dragUsageCardAbove(page, "beta:home", "alpha:a"); + await expectUsageCardOrder(page, ["beta:home", "alpha:a", "beta:work"]); + await expectPinnedUsage(page, ["20% Weekly", "31% Weekly"]); + + await page.reload(); + await expectPinnedUsage(page, ["20% Weekly", "31% Weekly"]); + await openUsageFromItem(page); + await expectUsageCardOrder(page, ["beta:home", "alpha:a", "beta:work"]); + await expect(page.getByTestId("usage-report-beta:work").getByText("12% used")).toHaveCount(0); + }); + test("refreshes one report from its card", async ({ page }) => { test.setTimeout(120_000); const serverId = getServerId(); diff --git a/packages/app/e2e/support/helpers/usage-sidebar-item.ts b/packages/app/e2e/support/helpers/usage-sidebar-item.ts index 93f60c1b7..0c1365998 100644 --- a/packages/app/e2e/support/helpers/usage-sidebar-item.ts +++ b/packages/app/e2e/support/helpers/usage-sidebar-item.ts @@ -147,6 +147,46 @@ export function usageModal(page: Page): Locator { return visible(page, "usage-modal-body"); } +/** The Usage modal's cards, top to bottom, by report id. */ +export async function expectUsageCardOrder(page: Page, reportIds: string[]): Promise { + const cards = usageModal(page).locator('[data-testid^="usage-report-"]'); + await expect(cards).toHaveCount(reportIds.length); + await expect + .poll(() => + cards.evaluateAll((nodes) => + nodes.map((node) => node.getAttribute("data-testid")?.slice("usage-report-".length)), + ), + ) + .toEqual(reportIds); +} + +/** Turns a card on or off from its header switch, named by its source and account. */ +export async function setUsageCardOn(page: Page, name: string, on: boolean): Promise { + const cardSwitch = usageModal(page).getByRole("switch", { name: `Show ${name}`, exact: true }); + await cardSwitch.click(); + await expect(cardSwitch).toBeChecked({ checked: on }); +} + +/** Drags a card by its handle until it sits above another card, with the mouse. */ +export async function dragUsageCardAbove( + page: Page, + reportId: string, + targetReportId: string, +): Promise { + const card = usageModal(page).getByTestId(`usage-report-${reportId}`); + const target = usageModal(page).getByTestId(`usage-report-${targetReportId}`); + await card.hover(); + const handle = await card.getByTestId("usage-card-drag-handle").boundingBox(); + const targetBox = await target.boundingBox(); + if (!handle || !targetBox) throw new Error("Both cards must be on screen to drag."); + const x = handle.x + handle.width / 2; + await page.mouse.move(x, handle.y + handle.height / 2); + await page.mouse.down(); + await page.mouse.move(x, handle.y + handle.height / 2 - 10, { steps: 3 }); + await page.mouse.move(x, targetBox.y + 4, { steps: 12 }); + await page.mouse.up(); +} + /** Each pinned window as it reads: its percent and short label, "31% 5h". */ export async function expectPinnedUsage(page: Page, windows: string[]): Promise { const pinned = usageItem(page).getByTestId("sidebar-usage-pinned-window"); @@ -167,14 +207,18 @@ export function pinRow(scope: Locator, source: string, window: string): Locator /** * A report card whose window rows do not pin: no pin toggle, no pin glyph, and nothing focusable - * but buttons, so no row presses or highlights on hover. + * but the header's buttons and on/off switch, so no row presses or highlights on hover. */ export async function expectUnpinnableRows(card: Locator): Promise { await expect(card.getByRole("checkbox")).toHaveCount(0); await expect(card.locator('[data-testid^="usage-pin-"]')).toHaveCount(0); const focusable = await card .locator("[tabindex]") - .evaluateAll((nodes) => nodes.filter((node) => node.getAttribute("role") !== "button").length); + .evaluateAll( + (nodes) => + nodes.filter((node) => !["button", "switch"].includes(node.getAttribute("role") ?? "")) + .length, + ); expect(focusable).toBe(0); } diff --git a/packages/app/src/changelog/internal/changelog-sheet.tsx b/packages/app/src/changelog/internal/changelog-sheet.tsx index fd7bc8245..fb367af66 100644 --- a/packages/app/src/changelog/internal/changelog-sheet.tsx +++ b/packages/app/src/changelog/internal/changelog-sheet.tsx @@ -1,18 +1,18 @@ import { memo, useCallback, useMemo, useState } from "react"; -import { Pressable, Text, View, type PressableStateCallbackType } from "react-native"; +import { Text, View } from "react-native"; import { ExternalLink, Gift } from "lucide-react-native"; import { useTranslation } from "react-i18next"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; -import { AdaptiveModalSheet, type SheetHeader } from "@/components/adaptive-modal-sheet"; +import { + AdaptiveModalSheet, + SheetHeaderIconButton, + type SheetHeader, +} from "@/components/adaptive-modal-sheet"; import { MarkdownRenderer } from "@/components/markdown/renderer"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { LoadingSpinner } from "@/components/ui/loading-spinner"; -import { - iconButtonChromeGlyphSize, - iconButtonChromeStyle, -} from "@/components/ui/icon-button-chrome"; import { StatusBadge } from "@/components/ui/status-badge"; import { ICON_SIZE, type Theme } from "@/styles/theme"; import { resolveAppVersion } from "@/utils/app-version"; @@ -28,12 +28,9 @@ import { const WEBSITE_CHANGELOG_URL = "https://paseo.sh/changelog"; const ThemedGift = withUnistyles(Gift); -const ThemedExternalLink = withUnistyles(ExternalLink); const ThemedLoadingSpinner = withUnistyles(LoadingSpinner); const mutedColorMapping = (theme: Theme) => ({ color: theme.colors.foregroundMuted }); const sheetLeadingIcon = ; -const websiteButtonStyle = (state: PressableStateCallbackType & { hovered?: boolean }) => - iconButtonChromeStyle({ size: "large", state }); interface ChangelogSheetProps { visible: boolean; @@ -54,19 +51,12 @@ export function ChangelogSheet({ visible, onClose }: ChangelogSheetProps) { title: t("changelog.title"), leading: sheetLeadingIcon, actions: ( - - - + /> ), }), [handleOpenWebsite, t], diff --git a/packages/app/src/components/adaptive-modal-sheet.browser.test.tsx b/packages/app/src/components/adaptive-modal-sheet.browser.test.tsx new file mode 100644 index 000000000..29a670a98 --- /dev/null +++ b/packages/app/src/components/adaptive-modal-sheet.browser.test.tsx @@ -0,0 +1,76 @@ +import React, { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { RotateCw, Settings } from "lucide-react-native"; +import { afterEach, describe, expect, it } from "vitest"; +import { + InlineHeaderView, + SheetHeaderIconButton, + SheetHeaderView, + type SheetHeader, +} from "@/components/adaptive-modal-sheet"; + +let root: Root | null = null; +let container: HTMLDivElement | null = null; + +afterEach(() => { + act(() => root?.unmount()); + container?.remove(); + root = null; + container = null; +}); + +function noop() {} + +const LONG_TITLE = + "A sheet title long enough to fill the whole header row on a phone and then some more"; + +const header: SheetHeader = { + title: LONG_TITLE, + actions: ( + <> + + + + ), +}; + +function mount(node: React.ReactNode) { + container = document.createElement("div"); + container.style.width = "390px"; + document.body.appendChild(container); + root = createRoot(container); + act(() => root?.render(node)); +} + +function rectOf(testID: string): DOMRect { + const found = document.querySelector(`[data-testid="${testID}"]`); + if (!(found instanceof HTMLElement)) throw new Error(`Nothing rendered as ${testID}`); + return found.getBoundingClientRect(); +} + +/** Each button keeps its own width, follows the previous one and stays inside the row. */ +function expectButtonsInRow(testIDs: string[]) { + const row = container?.getBoundingClientRect(); + if (!row) throw new Error("Nothing mounted"); + const rects = testIDs.map(rectOf); + for (const [index, rect] of rects.entries()) { + expect(rect.width).toBeGreaterThan(0); + expect(rect.right).toBeLessThanOrEqual(row.right + 1); + const previous = rects[index - 1]; + if (previous) expect(rect.left).toBeGreaterThanOrEqual(previous.right); + } +} + +describe("sheet header with a long title", () => { + it("truncates the title and keeps the actions and Close in place", () => { + mount(); + + expectButtonsInRow(["refresh", "settings", "close"]); + }); + + it("does the same in the inline header", () => { + mount(); + + expectButtonsInRow(["refresh", "settings"]); + }); +}); diff --git a/packages/app/src/components/adaptive-modal-sheet.tsx b/packages/app/src/components/adaptive-modal-sheet.tsx index 3ff202095..31a605bdf 100644 --- a/packages/app/src/components/adaptive-modal-sheet.tsx +++ b/packages/app/src/components/adaptive-modal-sheet.tsx @@ -3,7 +3,7 @@ import { useCallback, useEffect, useMemo, useState } from "react"; import type { ComponentProps, ReactNode } from "react"; import { createPortal } from "react-dom"; import { useTranslation } from "react-i18next"; -import { Keyboard, Pressable, Text, View } from "react-native"; +import { Keyboard, Text, View } from "react-native"; import type { DimensionValue, StyleProp, ViewProps, ViewStyle } from "react-native"; import { StyleSheet, useUnistyles, withUnistyles } from "react-native-unistyles"; import { useIsCompactFormFactor } from "@/constants/layout"; @@ -29,6 +29,15 @@ import { useKeyboardVisibility } from "@/hooks/use-keyboard-visibility"; import { SafeAreaInsetsContext, useSafeAreaInsets } from "react-native-safe-area-context"; import { AdaptiveTextInput } from "@/components/adaptive-text-input"; import { useDismissibleOverlay } from "@/components/ui/overlay-dismissal"; +import { DismissBackdrop } from "@/components/ui/dismiss-backdrop"; +import { + IconButtonChromeProvider, + iconButtonChromeGlyphSize, + iconButtonChromeHang, + mutedIconColorMapping, +} from "@/components/ui/icon-button-chrome"; +import { LoadingSpinner } from "@/components/ui/loading-spinner"; +import { ToolbarButton } from "@/components/ui/pane-content-toolbar"; export { AdaptiveTextInput, type AdaptiveTextInputProps } from "@/components/adaptive-text-input"; // Horizontal indent token shared by the sheet header (title, back arrow, @@ -37,11 +46,6 @@ export { AdaptiveTextInput, type AdaptiveTextInputProps } from "@/components/ada // match this padding. export const SHEET_HORIZONTAL_PADDING_SCALE = 6; -// The header's close button grows outward from its glyph, so the glyph's -// trailing rail is the content inset plus this padding. Rows whose trailing -// glyph should line up with the X must reach the same rail. -export const SHEET_HEADER_CLOSE_PADDING_SCALE = 2; - /** The one height a sheet takes when it shows a whole pane rather than sizing to its content. */ export const FULL_SHEET_SNAP_POINTS = ["92%"]; @@ -63,6 +67,7 @@ export interface SheetHeaderBack { export interface SheetHeader { title: string; + titleTestID?: string; subtitle?: ReactNode; back?: SheetHeaderBack; leading?: ReactNode; @@ -71,7 +76,9 @@ export interface SheetHeader { } const SCROLL_CONTENT_GROW = { flexGrow: 1 }; -const ABSOLUTE_FILL_STYLE = { ...StyleSheet.absoluteFill }; +// Header buttons hang by how far their frame reaches past the glyph, so the edge glyphs sit on the +// content rail and a 44pt touch frame does not grow the row. +const HEADER_BUTTON_HANG = iconButtonChromeHang("large"); const NATIVE_DIALOG_SNAP_POINTS = ["100%"]; const styles = StyleSheet.create((theme) => ({ @@ -105,20 +112,24 @@ const styles = StyleSheet.create((theme) => ({ borderBottomWidth: 1, borderBottomColor: theme.colors.surface2, }, + // One height for every sheet header whatever its buttons; a hung 44pt touch frame fits inside it. headerRow: { + minHeight: 64, paddingHorizontal: theme.spacing[SHEET_HORIZONTAL_PADDING_SCALE], paddingVertical: theme.spacing[4], flexDirection: "row", alignItems: "center", gap: theme.spacing[2], }, - headerBackButton: { - borderRadius: theme.borderRadius.lg, + headerBack: { + marginVertical: HEADER_BUTTON_HANG, + marginLeft: HEADER_BUTTON_HANG, }, headerLeadingSlot: { alignItems: "center", justifyContent: "center", }, + // The title takes the room the buttons leave and truncates; the buttons never shrink. headerTitleGroup: { flex: 1, gap: theme.spacing[1], @@ -128,14 +139,13 @@ const styles = StyleSheet.create((theme) => ({ fontSize: theme.fontSize.base, fontWeight: theme.fontWeight.medium, }, - headerActions: { + headerTrailing: { flexDirection: "row", alignItems: "center", gap: theme.spacing[2], - }, - closeButton: { - padding: theme.spacing[SHEET_HEADER_CLOSE_PADDING_SCALE], - borderRadius: theme.borderRadius.lg, + flexShrink: 0, + marginVertical: HEADER_BUTTON_HANG, + marginRight: HEADER_BUTTON_HANG, }, // Without a header the close button floats over the card's top-right corner. floatingClose: { @@ -176,6 +186,7 @@ const styles = StyleSheet.create((theme) => ({ }, inlineTitle: { flex: 1, + minWidth: 0, fontSize: theme.fontSize.base, fontWeight: theme.fontWeight.medium, color: theme.colors.foreground, @@ -343,47 +354,83 @@ function SheetContent({ style, children }: { style: StyleProp; childr return {children}; } +const ThemedLoadingSpinner = withUnistyles(LoadingSpinner); +const ThemedHeaderGlyph = withUnistyles(function HeaderGlyph({ + icon: Icon, + size, + color, +}: { + icon: LucideIcon; + size: number; + color?: string; +}) { + return ; +}); + /** - * An icon action in a sheet header's trailing rail, drawn like the Close button so a header's - * actions line up with it at one size. Pass it through `SheetHeader.actions`. + * The one icon button of a sheet header: the back arrow, the close X, and every action passed + * through `SheetHeader.actions`. Inside a header it takes the header island's chrome, so all of them + * share one frame, glyph, colour and hover. `busy` swaps the glyph for a spinner and holds presses. */ export function SheetHeaderIconButton({ - icon: Icon, - accessibilityLabel, + icon, + label, onPress, + busy = false, + disabled = false, testID, }: { icon: LucideIcon; - accessibilityLabel: string; + /** The accessible name, also shown as the desktop tooltip. */ + label: string; onPress: () => void; + busy?: boolean; + disabled?: boolean; testID?: string; }) { - const { theme } = useUnistyles(); + const compact = useIsCompactFormFactor(); + const glyphSize = iconButtonChromeGlyphSize("large", compact); return ( - - {({ pressed }) => ( - + {busy ? ( + + ) : ( + )} - + + ); +} + +function SheetHeaderBackButton({ back }: { back: SheetHeaderBack }) { + const { t } = useTranslation(); + return ( + + + ); } export function SheetHeaderView({ header, onClose, - showCloseButton = true, testID, + closeTestID, }: { header: SheetHeader; onClose: () => void; - showCloseButton?: boolean; testID?: string; + closeTestID?: string; }) { const { theme } = useUnistyles(); const { t } = useTranslation(); @@ -391,8 +438,6 @@ export function SheetHeaderView({ () => [styles.title, { color: theme.colors.foreground }], [theme.colors.foreground], ); - const back = header.back; - const handleBackPress = back?.onPress; const search = header.search; const handleSearchChange = useCallback( (value: string) => { @@ -403,40 +448,27 @@ export function SheetHeaderView({ return ( - - {handleBackPress ? ( - - {({ pressed }) => ( - - )} - - ) : null} - {header.leading ? {header.leading} : null} - - - {header.title} - - {header.subtitle} + + + {header.back ? : null} + {header.leading ? {header.leading} : null} + + + {header.title} + + {header.subtitle} + + + {header.actions} + + - {header.actions ? {header.actions} : null} - {showCloseButton ? ( - - ) : null} - + {search ? ( @@ -462,39 +494,21 @@ export function SheetHeaderView({ export function InlineHeaderView({ header }: { header: SheetHeader }) { const { theme } = useUnistyles(); const { t } = useTranslation(); - const back = header.back; - const handleBackPress = back?.onPress; - const hasInlineRow = Boolean(handleBackPress || header.leading || header.actions); + const hasInlineRow = Boolean(header.back || header.leading || header.actions); if (!hasInlineRow && !header.search) return null; return ( {hasInlineRow ? ( - - {handleBackPress ? ( - - {({ pressed }) => ( - - )} - - ) : null} - {header.leading ? {header.leading} : null} - - {header.title} - - {header.actions ? {header.actions} : null} - + + + {header.back ? : null} + {header.leading ? {header.leading} : null} + + {header.title} + + {header.actions ? {header.actions} : null} + + ) : null} {header.search ? ( @@ -798,24 +812,18 @@ export function AdaptiveModalSheet({ {footerView} {header ? null : ( - - - + + + + + )} ); const desktopContent = ( - + {cardInner} diff --git a/packages/app/src/components/app-diagnostic-sheet.tsx b/packages/app/src/components/app-diagnostic-sheet.tsx index 9cf74a932..41c2a6b1a 100644 --- a/packages/app/src/components/app-diagnostic-sheet.tsx +++ b/packages/app/src/components/app-diagnostic-sheet.tsx @@ -1,11 +1,15 @@ import * as Clipboard from "expo-clipboard"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import { Platform, Pressable, Text, View, type PressableStateCallbackType } from "react-native"; +import { Platform, Text, View } from "react-native"; import { Copy, RotateCw } from "lucide-react-native"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; import { useTranslation } from "react-i18next"; -import { AdaptiveModalSheet, type SheetHeader } from "@/components/adaptive-modal-sheet"; +import { + AdaptiveModalSheet, + SheetHeaderIconButton, + type SheetHeader, +} from "@/components/adaptive-modal-sheet"; import { LoadingSpinner } from "@/components/ui/loading-spinner"; import { ScrollableCodeSurface, SurfaceCard } from "@/components/ui/scrollable-code-surface"; import { useToast } from "@/contexts/toast-context"; @@ -42,8 +46,6 @@ interface DiagnosticCollectionResult { } const SNAP_POINTS = ["55%", "88%"]; -const ThemedCopy = withUnistyles(Copy); -const ThemedRotateCw = withUnistyles(RotateCw); const ThemedLoadingSpinner = withUnistyles(LoadingSpinner); const foregroundMutedColorMapping = (theme: Theme) => ({ color: theme.colors.foregroundMuted, @@ -150,68 +152,31 @@ export function AppDiagnosticSheet({ .catch(() => toast.error(t("settings.diagnostics.app.copyFailed"))); }, [diagnostic, t, toast]); - const iconButtonStyle = useCallback( - ({ hovered, pressed }: PressableStateCallbackType & { hovered?: boolean }) => [ - styles.iconButton, - (Boolean(hovered) || pressed) && styles.iconButtonHovered, - ], - [], - ); - - const disabledIconButtonStyle = useCallback( - ({ hovered, pressed }: PressableStateCallbackType & { hovered?: boolean }) => [ - styles.iconButton, - (Boolean(hovered) || pressed) && Boolean(diagnostic) && styles.iconButtonHovered, - diagnostic ? null : styles.disabled, - ], - [diagnostic], - ); - const header = useMemo( () => ({ title: t("settings.diagnostics.app.title"), actions: ( - - + - - - + - {loading ? ( - - ) : ( - - )} - - + onPress={handleRefreshPress} + busy={loading} + /> + ), }), - [ - diagnostic, - disabledIconButtonStyle, - handleCopyPress, - handleRefreshPress, - iconButtonStyle, - loading, - t, - ], + [diagnostic, handleCopyPress, handleRefreshPress, loading, t], ); return ( @@ -342,24 +307,6 @@ const styles = StyleSheet.create((theme) => ({ fontSize: theme.fontSize.base, color: theme.colors.foregroundMuted, }, - headerActions: { - flexDirection: "row", - alignItems: "center", - gap: theme.spacing[1], - }, - iconButton: { - width: 28, - height: 28, - borderRadius: theme.borderRadius.full, - alignItems: "center", - justifyContent: "center", - }, - iconButtonHovered: { - backgroundColor: theme.colors.surface2, - }, - disabled: { - opacity: 0.5, - }, statusDot: { width: 8, height: 8, diff --git a/packages/app/src/components/drag-reorder/drag-hold.test.ts b/packages/app/src/components/drag-reorder/drag-hold.test.ts new file mode 100644 index 000000000..48f7cd42e --- /dev/null +++ b/packages/app/src/components/drag-reorder/drag-hold.test.ts @@ -0,0 +1,45 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { createDragHold } from "./drag-hold"; + +beforeEach(() => { + vi.useFakeTimers(); +}); + +afterEach(() => { + vi.useRealTimers(); +}); + +function hold() { + const onArm = vi.fn(); + return { onArm, hold: createDragHold({ delayMs: 180, slopPx: 4, onArm }) }; +} + +describe("createDragHold", () => { + it("arms once the finger has stayed within the slop for the delay", () => { + const { onArm, hold: dragHold } = hold(); + dragHold.begin({ x: 10, y: 10 }); + dragHold.move({ x: 12, y: 13 }); + vi.advanceTimersByTime(180); + + expect(onArm).toHaveBeenCalledTimes(1); + }); + + it("never arms when the finger moves past the slop first, as when a scroll starts", () => { + const { onArm, hold: dragHold } = hold(); + dragHold.begin({ x: 10, y: 10 }); + dragHold.move({ x: 10, y: 20 }); + vi.advanceTimersByTime(500); + + expect(onArm).not.toHaveBeenCalled(); + }); + + it("never arms when the finger lifts before the delay", () => { + const { onArm, hold: dragHold } = hold(); + dragHold.begin({ x: 10, y: 10 }); + vi.advanceTimersByTime(100); + dragHold.cancel(); + vi.advanceTimersByTime(500); + + expect(onArm).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/drag-reorder/drag-hold.ts b/packages/app/src/components/drag-reorder/drag-hold.ts new file mode 100644 index 000000000..04ed15231 --- /dev/null +++ b/packages/app/src/components/drag-reorder/drag-hold.ts @@ -0,0 +1,51 @@ +export interface TouchPoint { + x: number; + y: number; +} + +export interface DragHold { + begin: (point: TouchPoint) => void; + move: (point: TouchPoint) => void; + cancel: () => void; +} + +/** + * A hold that arms a drag once the finger has stayed put for `delayMs`. Moving further than + * `slopPx` from where the touch began cancels it, so a scroll that starts on a handle stays a + * scroll. The sidebar rows arm on the same delay and slop. + */ +export function createDragHold({ + delayMs, + slopPx, + onArm, +}: { + delayMs: number; + slopPx: number; + onArm: () => void; +}): DragHold { + let timer: ReturnType | null = null; + let start: TouchPoint | null = null; + + function cancel() { + if (timer) clearTimeout(timer); + timer = null; + start = null; + } + + return { + begin(point) { + cancel(); + start = point; + timer = setTimeout(() => { + timer = null; + start = null; + onArm(); + }, delayMs); + }, + move(point) { + if (!start) return; + if (Math.hypot(point.x - start.x, point.y - start.y) > slopPx) cancel(); + }, + cancel, + }; +} diff --git a/packages/app/src/components/drag-reorder/native-drop.test.ts b/packages/app/src/components/drag-reorder/native-drop.test.ts new file mode 100644 index 000000000..f05bf3c81 --- /dev/null +++ b/packages/app/src/components/drag-reorder/native-drop.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { movedItemKey } from "./native-drop"; + +const byValue = (item: string): string => item; + +describe("movedItemKey", () => { + it("names the item at its new slot", () => { + expect(movedItemKey({ data: ["b", "c", "a"], from: 0, to: 2 }, byValue)).toBe("a"); + }); + + it("names nothing when the item is released in the slot it started in", () => { + expect(movedItemKey({ data: ["a", "b", "c"], from: 1, to: 1 }, byValue)).toBeNull(); + }); +}); diff --git a/packages/app/src/components/drag-reorder/native-drop.ts b/packages/app/src/components/drag-reorder/native-drop.ts new file mode 100644 index 000000000..e155da779 --- /dev/null +++ b/packages/app/src/components/drag-reorder/native-drop.ts @@ -0,0 +1,17 @@ +export interface NativeDrop { + data: T[]; + from: number; + to: number; +} + +/** + * The key of the item a native drop moved, or null when the item was released in the slot it + * started in. The native list reports every release; web reports only drops that moved an item. + */ +export function movedItemKey( + { data, from, to }: NativeDrop, + keyExtractor: (item: T, index: number) => string, +): string | null { + if (from === to) return null; + return keyExtractor(data[to], to); +} diff --git a/packages/app/src/components/drag-reorder/use-drag-reorder-state.ts b/packages/app/src/components/drag-reorder/use-drag-reorder-state.ts index 400beeb28..ba64619e0 100644 --- a/packages/app/src/components/drag-reorder/use-drag-reorder-state.ts +++ b/packages/app/src/components/drag-reorder/use-drag-reorder-state.ts @@ -24,7 +24,7 @@ export function useDragReorderState({ }: { data: T[]; keyExtractor: (item: T, index: number) => string; - onDragEnd?: (items: T[]) => void; + onDragEnd?: (items: T[], movedKey: string) => void; onDragBegin?: () => void; disabled?: boolean; }): DragReorderState { @@ -50,13 +50,14 @@ export function useDragReorderState({ dispatch({ type: "clear" }); if (disabled) return; + const activeId = String(active.id); const reordered = reorderItemsOnDragEnd({ items, - activeId: String(active.id), + activeId, overId: over ? String(over.id) : null, keyExtractor, }); - if (reordered) onDragEnd?.(reordered); + if (reordered) onDragEnd?.(reordered, activeId); }, [data, disabled, keyExtractor, onDragEnd, state.dragItems], ); diff --git a/packages/app/src/components/draggable-list.native.tsx b/packages/app/src/components/draggable-list.native.tsx index 5d5f4cf5f..c92b04ed3 100644 --- a/packages/app/src/components/draggable-list.native.tsx +++ b/packages/app/src/components/draggable-list.native.tsx @@ -1,13 +1,87 @@ -import { RefreshControl } from "react-native"; -import { useCallback, useMemo, useState } from "react"; +import { RefreshControl, View, type GestureResponderEvent } from "react-native"; +import { + createContext, + useCallback, + useContext, + useEffect, + useMemo, + useRef, + useState, +} from "react"; +import * as Haptics from "expo-haptics"; import DraggableFlatList, { NestableDraggableFlatList, type RenderItemParams, } from "react-native-draggable-flatlist"; +import type { GestureType } from "react-native-gesture-handler"; import { useUnistyles } from "react-native-unistyles"; -import type { DraggableListProps, DraggableRenderItemInfo } from "./draggable-list.types"; +import { useContentDrag } from "@/components/ui/scroll-view"; +import { createDragHold } from "./drag-reorder/drag-hold"; +import { movedItemKey } from "./drag-reorder/native-drop"; +import type { + DraggableListHandleProps, + DraggableListProps, + DraggableRenderItemInfo, +} from "./draggable-list.types"; -export type { DraggableListProps, DraggableRenderItemInfo }; +export type { DraggableListHandleProps, DraggableListProps, DraggableRenderItemInfo }; + +/** The row's `drag()`, for its handle. */ +const RowDragContext = createContext<(() => void) | null>(null); + +// The list only accepts drag() once its pan gesture has begun for this touch, and that begin +// reaches JS after the touch does. Same hold and slop as the sidebar rows' drag arm. +const HANDLE_HOLD_MS = 180; +const HANDLE_HOLD_SLOP_PX = 4; + +function touchPoint(event: GestureResponderEvent) { + return { x: event.nativeEvent.pageX, y: event.nativeEvent.pageY }; +} + +/** + * On native the handle has no accessibility role or label: a screen reader cannot hold and drag, + * and announcing a control whose activation does nothing misleads. The label is for web, where + * the keyboard reorders through it. + */ +export function DraggableListHandle({ testID, style, children }: DraggableListHandleProps) { + const drag = useContext(RowDragContext); + const hold = useMemo( + () => + drag + ? createDragHold({ + delayMs: HANDLE_HOLD_MS, + slopPx: HANDLE_HOLD_SLOP_PX, + onArm: () => { + void Haptics.selectionAsync().catch(() => {}); + drag(); + }, + }) + : null, + [drag], + ); + useEffect(() => () => hold?.cancel(), [hold]); + const startHold = useCallback( + (event: GestureResponderEvent) => hold?.begin(touchPoint(event)), + [hold], + ); + const moveHold = useCallback( + (event: GestureResponderEvent) => hold?.move(touchPoint(event)), + [hold], + ); + const cancelHold = useCallback(() => hold?.cancel(), [hold]); + return ( + + {children} + + ); +} const SCROLL_ENABLED_FLEX_STYLE = { flex: 1 }; @@ -41,6 +115,12 @@ export function DraggableList({ }: DraggableListProps) { const { theme } = useUnistyles(); const [isDragging, setIsDragging] = useState(false); + // Inside a sheet the sheet would otherwise take the vertical move from an armed row. + const contentDrag = useContentDrag(); + const { attach: attachContentDrag } = contentDrag; + const dragGestureRef = useRef(undefined); + // Passive effects run after the list's gesture detector has attached and filled the ref. + useEffect(() => attachContentDrag?.(dragGestureRef), [attachContentDrag]); // Pass the ref directly to DraggableFlatList - it handles gesture // coordination internally for nestable lists. @@ -63,27 +143,31 @@ export function DraggableList({ drag, isActive, }; - return renderItem(info); + return {renderItem(info)}; }, [renderItem], ); const handleDragEnd = useCallback( - ({ data: newData }: { data: T[] }) => { + (drop: { data: T[]; from: number; to: number }) => { setIsDragging(false); - onDragEnd(newData); + contentDrag.setActive(false); + const moved = movedItemKey(drop, keyExtractor); + if (moved !== null) onDragEnd(drop.data, moved); }, - [onDragEnd], + [contentDrag, keyExtractor, onDragEnd], ); const handleDragBegin = useCallback(() => { setIsDragging(true); + contentDrag.setActive(true); onDragBeginProp?.(); - }, [onDragBeginProp]); + }, [contentDrag, onDragBeginProp]); const handleRelease = useCallback(() => { setIsDragging(false); - }, []); + contentDrag.setActive(false); + }, [contentDrag]); const showRefreshControl = Boolean(onRefresh) && (!isDragging || Boolean(refreshing)); const resolvedContainerStyle = @@ -128,6 +212,7 @@ export function DraggableList({ extraData={extraData} simultaneousHandlers={simultaneousHandlers} dragGestureHostPresented={gestureHostPresented} + dragGestureRef={attachContentDrag ? dragGestureRef : undefined} // Higher activation distance reduces accidental drag capture while nested // lists are inside a scroll container. activationDistance={20} diff --git a/packages/app/src/components/draggable-list.types.ts b/packages/app/src/components/draggable-list.types.ts index d541b5d73..67b628cc4 100644 --- a/packages/app/src/components/draggable-list.types.ts +++ b/packages/app/src/components/draggable-list.types.ts @@ -1,4 +1,4 @@ -import type { ReactElement, MutableRefObject } from "react"; +import type { ReactElement, ReactNode, MutableRefObject } from "react"; import type { StyleProp, ViewStyle } from "react-native"; import type { GestureType } from "react-native-gesture-handler"; @@ -12,6 +12,18 @@ export interface DraggableListDragHandleProps { setActivatorNodeRef?: (node: unknown) => void; } +/** + * The grip a row is dragged by, rendered anywhere inside `renderItem`. Pointer drags start from + * it on web (with `useDragHandle`); touch drags start after a short hold on native. + */ +export interface DraggableListHandleProps { + /** Names the keyboard-reorderable handle on web. Native leaves the handle out of accessibility. */ + accessibilityLabel: string; + testID?: string; + style?: StyleProp; + children: ReactNode; +} + export interface DraggableRenderItemInfo { item: T; index: number; @@ -24,7 +36,8 @@ export interface DraggableListProps { data: T[]; keyExtractor: (item: T, index: number) => string; renderItem: (info: DraggableRenderItemInfo) => ReactElement; - onDragEnd: (data: T[]) => void; + /** The new order, and the key of the item the user moved to reach it. */ + onDragEnd: (data: T[], movedKey: string) => void; style?: StyleProp; /** Outer container style (useful for nested, non-scrolling lists). */ containerStyle?: StyleProp; diff --git a/packages/app/src/components/draggable-list.web.browser.test.tsx b/packages/app/src/components/draggable-list.web.browser.test.tsx new file mode 100644 index 000000000..6839bb5b2 --- /dev/null +++ b/packages/app/src/components/draggable-list.web.browser.test.tsx @@ -0,0 +1,110 @@ +import React, { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + DraggableList, + DraggableListHandle, + type DraggableRenderItemInfo, +} from "./draggable-list.web"; + +Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); + +interface Item { + id: string; +} + +function keyExtractor(item: Item): string { + return item.id; +} + +function noop() {} + +let root: Root | null = null; +let container: HTMLDivElement | null = null; + +beforeEach(() => { + container = document.createElement("div"); + document.body.appendChild(container); + root = createRoot(container); +}); + +afterEach(() => { + act(() => root?.unmount()); + container?.remove(); + root = null; + container = null; +}); + +function renderRowWithHandle(info: DraggableRenderItemInfo) { + return ( +
+ + grip + + {info.item.id} +
+ ); +} + +function renderList(onDragBegin: () => void) { + act(() => { + root?.render( + , + ); + }); +} + +function byTestId(testID: string): HTMLElement { + const found = container?.querySelector(`[data-testid="${testID}"]`); + if (!(found instanceof HTMLElement)) throw new Error(`Nothing rendered as ${testID}`); + return found; +} + +/** Press on the element's centre, move the mouse down by `distance`, and release. */ +function pressAndMove(element: HTMLElement, distance: number) { + const { left, top, width, height } = element.getBoundingClientRect(); + const x = left + width / 2; + const y = top + height / 2; + act(() => { + element.dispatchEvent( + new MouseEvent("mousedown", { bubbles: true, button: 0, clientX: x, clientY: y }), + ); + }); + act(() => { + document.dispatchEvent( + new MouseEvent("mousemove", { bubbles: true, clientX: x, clientY: y + distance }), + ); + }); + act(() => { + document.dispatchEvent(new MouseEvent("mouseup", { bubbles: true })); + }); +} + +describe("DraggableListHandle on web", () => { + it("starts the row's drag from the handle, and only from the handle", () => { + const onDragBegin = vi.fn(); + renderList(onDragBegin); + + pressAndMove(byTestId("row-body"), 20); + expect(onDragBegin).not.toHaveBeenCalled(); + + pressAndMove(byTestId("row-handle"), 20); + expect(onDragBegin).toHaveBeenCalledTimes(1); + }); + + it("is a labelled button rather than dnd-kit's sortable", () => { + renderList(noop); + const handle = byTestId("row-handle"); + + expect(handle.getAttribute("role")).toBe("button"); + expect(handle.getAttribute("aria-label")).toBe("Move one"); + expect(handle.getAttribute("aria-roledescription")).toBeNull(); + }); +}); diff --git a/packages/app/src/components/draggable-list.web.tsx b/packages/app/src/components/draggable-list.web.tsx index 92a4d2287..5f35a3e28 100644 --- a/packages/app/src/components/draggable-list.web.tsx +++ b/packages/app/src/components/draggable-list.web.tsx @@ -1,4 +1,13 @@ -import { memo, useCallback, useMemo, useRef, type ReactElement } from "react"; +import { + createContext, + memo, + useCallback, + useContext, + useMemo, + useRef, + type ReactElement, + type Ref, +} from "react"; import { ScrollView, View } from "react-native"; import { DndContext, @@ -17,10 +26,52 @@ import { useSortable, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; -import type { DraggableListProps, DraggableRenderItemInfo } from "./draggable-list.types"; +import type { + DraggableListDragHandleProps, + DraggableListHandleProps, + DraggableListProps, + DraggableRenderItemInfo, +} from "./draggable-list.types"; import { getDragActivationConstraints, useDragReorderState } from "./drag-reorder"; -export type { DraggableListProps, DraggableRenderItemInfo }; +export type { DraggableListHandleProps, DraggableListProps, DraggableRenderItemInfo }; + +/** The row's dnd-kit activator, for its handle. Absent unless the list uses drag handles. */ +const RowHandleContext = createContext(undefined); + +// React Native's style types have no `cursor: "grab"`; react-native-web passes it through. +const GRAB_CURSOR = { cursor: "grab" } as object; + +export function DraggableListHandle({ + accessibilityLabel, + testID, + style, + children, +}: DraggableListHandleProps) { + const handle = useContext(RowHandleContext); + // The handle is a labelled button; dnd-kit's own role, focus stop and "sortable" description + // would replace that. + const { + role: _role, + tabIndex: _tabIndex, + "aria-roledescription": _roleDescription, + ...attributes + } = handle?.attributes ?? {}; + const handleStyle = useMemo(() => [GRAB_CURSOR, style], [style]); + return ( + } + accessibilityRole="button" + accessibilityLabel={accessibilityLabel} + style={handleStyle} + testID={testID} + > + {children} + + ); +} const restrictToVerticalAxis: Modifier = ({ transform }) => ({ ...transform, @@ -177,7 +228,9 @@ function SortableItemInner({ return (
- {renderItem(info)} + + {renderItem(info)} +
); } diff --git a/packages/app/src/components/host-status-dot.tsx b/packages/app/src/components/host-status-dot.tsx index 4f7d16dc4..2a6af8961 100644 --- a/packages/app/src/components/host-status-dot.tsx +++ b/packages/app/src/components/host-status-dot.tsx @@ -5,6 +5,8 @@ import { useHostRuntimeConnectionStatus, } from "@/runtime/host-runtime"; +export const HOST_STATUS_DOT_SIZE = 8; + export function HostStatusDot({ serverId }: { serverId: string }) { const status = useHostRuntimeConnectionStatus(serverId); @@ -19,8 +21,8 @@ function statusStyle(status: HostRuntimeConnectionStatus) { const styles = StyleSheet.create((theme) => ({ dot: { - width: 8, - height: 8, + width: HOST_STATUS_DOT_SIZE, + height: HOST_STATUS_DOT_SIZE, borderRadius: theme.borderRadius.full, }, dotOnline: { diff --git a/packages/app/src/components/hosts/host-filter.tsx b/packages/app/src/components/hosts/host-filter.tsx index d073dc943..9a810f2ca 100644 --- a/packages/app/src/components/hosts/host-filter.tsx +++ b/packages/app/src/components/hosts/host-filter.tsx @@ -1,8 +1,12 @@ import { useCallback, useMemo, useRef, useState, type ReactElement } from "react"; -import { Pressable, Text, View, type PressableStateCallbackType } from "react-native"; -import { ChevronDown, Server } from "lucide-react-native"; +import { Text, View } from "react-native"; +import { Server } from "lucide-react-native"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; -import type { Theme } from "@/styles/theme"; +import { HOST_STATUS_DOT_SIZE } from "@/components/host-status-dot"; +import { ComboboxTrigger } from "@/components/ui/combobox-trigger"; +import { mapLayout } from "@/components/ui/control-sizing"; +import { density } from "@/styles/density"; +import { ICON_SIZE, type Theme } from "@/styles/theme"; import { ALL_HOSTS_OPTION_ID, getHostPickerLabel, @@ -12,7 +16,8 @@ import { } from "@/components/hosts/host-picker"; const ThemedServer = withUnistyles(Server); -const ThemedChevronDown = withUnistyles(ChevronDown); +// The height a hung trigger keeps: one line of its base-size label. +const LABEL_LINE = 20; const mutedColorMapping = (theme: Theme) => ({ color: theme.colors.foregroundMuted }); export interface HostFilterProps { @@ -24,20 +29,28 @@ export interface HostFilterProps { * manager edits a single host's catalog, so "all" is not an answer it could carry out. */ includeAllHost?: boolean; + /** + * Hang the trigger into the surrounding padding, for a trigger that leads a column such as a + * modal body: its leading glyph's ink sits on the content rail and it adds only a label line to + * the column, so the column's own gap sets the rhythm. The frame still grows outward on hover. + */ + hang?: boolean; triggerTestID?: string; hostOptionTestID?: (serverId: string) => string; } /** - * The "All hosts / " host pill shared by History, Schedules, Usage, the label manager and + * The "All hosts / " host picker shared by History, Schedules, Usage, the label manager and * plugin screens: an anchored HostPicker, hidden by the caller when only one host exists. Without - * `includeAllHost` it picks the one host a surface shows. + * `includeAllHost` it picks the one host a surface shows. Its trigger is the Settings host + * trigger; a long host name truncates instead of pushing the row. */ export function HostFilter({ hosts, selectedHost, onSelectHost, includeAllHost = true, + hang = false, triggerTestID, hostOptionTestID, }: HostFilterProps): ReactElement { @@ -50,12 +63,21 @@ export function HostFilter({ ); const handleFilterOpen = useCallback(() => setIsFilterOpen(true), []); + const showsAllHosts = selectedHost === ALL_HOSTS_OPTION_ID; + const slotStyle = useMemo( + () => [ + styles.triggerSlot, + hang && styles.triggerSlotHang, + hang && (showsAllHosts ? styles.glyphHang : styles.dotHang), + ], + [hang, showsAllHosts], + ); - const filterTriggerStyle = useCallback( - ({ pressed, hovered = false }: PressableStateCallbackType & { hovered?: boolean }) => [ - styles.filterTrigger, - Boolean(hovered) && styles.filterTriggerHovered, - pressed && styles.filterTriggerPressed, + const triggerStyle = useCallback( + ({ pressed, hovered }: { pressed: boolean; hovered: boolean }) => [ + styles.trigger, + hovered && styles.triggerHovered, + pressed && styles.triggerPressed, ], [], ); @@ -74,54 +96,73 @@ export function HostFilter({ desktopPlacement="bottom-start" hostOptionTestID={hostOptionTestID} > - - + - {selectedHost === ALL_HOSTS_OPTION_ID ? ( - + {showsAllHosts ? ( + ) : ( )} - + {selectedHostLabel} - - + ); } const styles = StyleSheet.create((theme) => ({ - filterTriggerWrap: { - alignSelf: "flex-start", + // A row around the trigger keeps it as wide as its label in a column, and centred like any other + // item in a row. + triggerSlot: { + flexDirection: "row", + flexShrink: 1, + minWidth: 0, + maxWidth: "100%", }, - filterTrigger: { + // Hangs the frame's height down to a label line. + triggerSlotHang: { + marginVertical: mapLayout(density.tightRowHeight, (height) => (LABEL_LINE - height) / 2), + }, + // Hangs the padding, so the server glyph's box sits on the rail. + glyphHang: { + marginLeft: -theme.spacing[2], + }, + // Hangs the padding and the dot's inset in its icon slot, so the dot's ink sits on the rail. + dotHang: { + marginLeft: -(theme.spacing[2] + (ICON_SIZE.sm - HOST_STATUS_DOT_SIZE) / 2), + }, + // The Settings host trigger: a borderless row that fills on hover and press. + trigger: { + flexShrink: 1, + minWidth: 0, + maxWidth: "100%", flexDirection: "row", alignItems: "center", - gap: theme.spacing[1.5], - alignSelf: "flex-start", - paddingVertical: theme.spacing[1.5], - paddingHorizontal: theme.spacing[3], - borderRadius: theme.borderRadius.md, - backgroundColor: theme.colors.surface1, - borderWidth: theme.borderWidth[1], - borderColor: theme.colors.border, + minHeight: density.tightRowHeight, + paddingVertical: theme.spacing[1], + paddingHorizontal: theme.spacing[2], + borderRadius: theme.borderRadius.lg, }, - filterTriggerHovered: { + triggerHovered: { backgroundColor: theme.colors.surface2, }, - filterTriggerPressed: { - backgroundColor: theme.colors.surface3, + triggerPressed: { + backgroundColor: theme.colors.surface2, }, - filterTriggerText: { + label: { + flexShrink: 1, + minWidth: 0, color: theme.colors.foreground, fontSize: theme.fontSize.base, - fontWeight: theme.fontWeight.medium, + fontWeight: theme.fontWeight.normal, }, })); diff --git a/packages/app/src/components/import-session-sheet.test.tsx b/packages/app/src/components/import-session-sheet.test.tsx index 35c475e82..167de2a0e 100644 --- a/packages/app/src/components/import-session-sheet.test.tsx +++ b/packages/app/src/components/import-session-sheet.test.tsx @@ -164,6 +164,25 @@ vi.mock("@/components/adaptive-modal-sheet", () => ({ {children} ) : null, + SheetHeaderIconButton: ({ + label, + onPress, + busy, + testID, + }: { + label: string; + onPress: () => void; + busy?: boolean; + testID?: string; + }) => ( + + ), }), [logs.isFetching, plugin, refresh, t], diff --git a/packages/app/src/screens/workspace/sheet-pane/sheet-pane.tsx b/packages/app/src/screens/workspace/sheet-pane/sheet-pane.tsx index 7b93f8474..9b3fa10c4 100644 --- a/packages/app/src/screens/workspace/sheet-pane/sheet-pane.tsx +++ b/packages/app/src/screens/workspace/sheet-pane/sheet-pane.tsx @@ -94,7 +94,7 @@ export function SheetPane({ sheet, onClose, onBack, opener = null, isTop }: Shee actions: sheet.policy.openAsTab ? ( diff --git a/packages/app/src/tool-calls/detail-level/overview/sheet.tsx b/packages/app/src/tool-calls/detail-level/overview/sheet.tsx index 2888f1f42..c42b7de51 100644 --- a/packages/app/src/tool-calls/detail-level/overview/sheet.tsx +++ b/packages/app/src/tool-calls/detail-level/overview/sheet.tsx @@ -1,16 +1,16 @@ -import { useCallback, useEffect, useRef, type ReactNode } from "react"; -import { Pressable, Text, View } from "react-native"; +import { useCallback, useEffect, useMemo, useRef, type ReactNode } from "react"; +import { View } from "react-native"; import { BottomSheetScrollView, type BottomSheetScrollViewMethods } from "@gorhom/bottom-sheet"; -import { Wrench, X } from "lucide-react-native"; +import { Wrench } from "lucide-react-native"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; -import { useTranslation } from "react-i18next"; import { type ContextBridge, useIsolatedBottomSheetVisibility, } from "@/components/ui/isolated-bottom-sheet-modal"; +import { SheetHeaderView, type SheetHeader } from "@/components/adaptive-modal-sheet"; import { ToolCallSheetModal, useToolCallSheetContextBridge } from "@/components/tool-call-sheet"; import { useToolCallExpansionContextBridge } from "@/tool-calls/expansion-context"; -import type { Theme } from "@/styles/theme"; +import { ICON_SIZE, type Theme } from "@/styles/theme"; interface OverviewToolCallGroupSheetProps { visible: boolean; @@ -21,9 +21,8 @@ interface OverviewToolCallGroupSheetProps { const SNAP_POINTS = ["60%", "95%"]; const ThemedWrench = withUnistyles(Wrench); -const ThemedX = withUnistyles(X); const foregroundMapping = (theme: Theme) => ({ color: theme.colors.foreground }); -const foregroundMutedMapping = (theme: Theme) => ({ color: theme.colors.foregroundMuted }); +const headerIcon = ; export function OverviewToolCallGroupSheet({ visible, @@ -31,7 +30,6 @@ export function OverviewToolCallGroupSheet({ children, onClose, }: OverviewToolCallGroupSheetProps) { - const { t } = useTranslation(); const scrollRef = useRef(null); const sheetContextBridge = useToolCallSheetContextBridge(); const expansionContextBridge = useToolCallExpansionContextBridge(); @@ -39,6 +37,10 @@ export function OverviewToolCallGroupSheet({ (content) => sheetContextBridge(expansionContextBridge(content)), [expansionContextBridge, sheetContextBridge], ); + const header = useMemo( + () => ({ title: summary, titleTestID: "tool-call-group-sheet-summary", leading: headerIcon }), + [summary], + ); const { sheetRef, handleSheetChange, handleSheetDismiss } = useIsolatedBottomSheetVisibility({ visible, onClose, @@ -68,27 +70,11 @@ export function OverviewToolCallGroupSheet({ enablePanDownToClose > - - - - - {summary} - - - - - - + {children} @@ -102,30 +88,6 @@ const styles = StyleSheet.create((theme) => ({ flex: 1, backgroundColor: theme.colors.surface2, }, - header: { - flexDirection: "row", - alignItems: "center", - justifyContent: "space-between", - paddingHorizontal: theme.spacing[4], - paddingVertical: theme.spacing[3], - borderBottomWidth: theme.borderWidth[1], - borderBottomColor: theme.colors.border, - }, - headerLeft: { - flexDirection: "row", - alignItems: "center", - gap: theme.spacing[2], - flex: 1, - }, - headerTitle: { - fontSize: theme.fontSize.base, - fontWeight: theme.fontWeight.medium, - color: theme.colors.foreground, - flex: 1, - }, - closeButton: { - padding: theme.spacing[2], - }, content: { flex: 1, minHeight: 0, diff --git a/packages/app/src/usage/agent-usage.tsx b/packages/app/src/usage/agent-usage.tsx index bf10054db..98f1c9bf8 100644 --- a/packages/app/src/usage/agent-usage.tsx +++ b/packages/app/src/usage/agent-usage.tsx @@ -1,3 +1,4 @@ +import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { Text, View } from "react-native"; import { StyleSheet } from "react-native-unistyles"; @@ -8,7 +9,7 @@ import type { UsageReportEntry } from "./types"; /** * The usage of the account an agent runs under, for its context window details: one card per - * report as it streams in, without pins. Nothing on hosts that cannot report usage, or for an + * report in the user's card order, without pins. Nothing on hosts that cannot report usage, or for an * agent with no account to report. */ export function AgentUsage({ @@ -55,7 +56,8 @@ function AgentUsageCards({ refreshable: boolean; }) { const { display } = useUsageDisplay(reports); - return reports.map((entry) => ( + const arranged = useMemo(() => display.arrange(reports), [display, reports]); + return arranged.map((entry) => ( void; + /** The handle is always there; it shows while the card is hovered or where there is no hover. */ + handleVisible: boolean; +} const ThemedLoadingSpinner = withUnistyles(LoadingSpinner); export function UsageCard({ @@ -41,6 +53,7 @@ export function UsageCard({ display, pinnable, refreshable, + arrangement, compact = false, }: { serverId: string; @@ -51,6 +64,8 @@ export function UsageCard({ pinnable: boolean; /** Whether the header has a Refresh button. Without one the freshness shows on the card. */ refreshable: boolean; + /** Present where the user arranges cards. An off card collapses to its header. */ + arrangement?: UsageCardArrangement; compact?: boolean; }) { const { t } = useTranslation(); @@ -60,9 +75,9 @@ export function UsageCard({ const showsFreshnessInline = isNative || isCompact || !refreshable; const usage = entry.report; const status = statusText(usage, t); - const footer = entry.account.label ?? null; - const { windows, balances, details, planLabel, successfulAt, retained, messages } = - resolveUsageReport(entry, t); + const collapsed = arrangement?.on === false; + const report = resolveUsageReport(entry, t); + const { planLabel, successfulAt, retained } = report; const containerStyle = useMemo( () => [styles.container, compact ? styles.containerCompact : styles.containerPadded], @@ -79,20 +94,34 @@ export function UsageCard({ return ( - + + {arrangement ? ( + + ) : null} {entry.sourceLabel} - {planLabel ? : null} + {collapsed && entry.account.label ? ( + // The body that names the account is gone, and two off cards of one source must differ. + + {entry.account.label} + + ) : null} + {planLabel && !collapsed ? ( + + ) : null} - {status ? ( + {status && !collapsed ? ( {status} ) : null} - {refreshable ? ( + {refreshable && !collapsed ? ( ) : null} + {arrangement ? ( + + + + ) : null} + {collapsed ? null : ( + + )} + + ); +} + +function UsageCardBody({ + entry, + display, + pinnable, + report, + showsFreshnessInline, + refreshState, +}: { + entry: UsageReportEntry; + display: UsageDisplay; + pinnable: boolean; + report: ReturnType; + showsFreshnessInline: boolean; + refreshState: UsageRefresh; +}) { + const { t } = useTranslation(); + const { windows, balances, details, retained, successfulAt, messages } = report; + const footer = entry.account.label ?? null; + return ( + <> {messages.length > 0 ? ( {messages.join("\n")} @@ -164,7 +240,24 @@ export function UsageCard({ {t("providerUsage.refreshFailed")} ) : null} - + + ); +} + +/** + * Drags the card. It hangs in the card's left padding, so the logo, name and body share one rail + * and revealing it on hover moves nothing. + */ +function UsageCardDragHandle({ name, visible }: { name: string; visible: boolean }) { + const { t } = useTranslation(); + return ( + + + ); } @@ -279,6 +372,21 @@ function UsageFreshness({ ); } +const DRAG_GRIP_SIZE = 14; + +// The handle fills the card's left padding beside the header. +function dragHandleSlot(spacing: { 4: number }) { + return { + position: "absolute", + left: 0, + width: spacing[4], + top: 0, + bottom: 0, + alignItems: "center", + justifyContent: "center", + } as const; +} + // The Refresh glyph lands on the card's right rail and the header keeps its text height; // the button's larger hitbox overhangs both instead of pushing them. function iconHitboxOverhang(compact: boolean) { @@ -305,11 +413,28 @@ const styles = StyleSheet.create((theme) => ({ alignItems: "center", gap: theme.spacing[2], }, + // The row reaches into the card's left padding and above and below its text, so the handle + // there is inside it: Android delivers no touches to a child where it overflows its parent. + headerWithHandle: { + position: "relative", + flexDirection: "row", + alignItems: "center", + gap: theme.spacing[2], + marginLeft: -theme.spacing[4], + paddingLeft: theme.spacing[4], + marginVertical: -theme.spacing[2], + paddingVertical: theme.spacing[2], + }, name: { flexShrink: 1, color: theme.colors.foreground, fontSize: theme.fontSize.base, }, + collapsedAccount: { + flexShrink: 1, + color: theme.colors.foregroundMuted, + fontSize: theme.fontSize.sm, + }, headerSpacer: { flex: 1, }, @@ -379,6 +504,12 @@ const styles = StyleSheet.create((theme) => ({ color: theme.colors.popoverForeground, fontSize: theme.fontSize.sm, }, + dragHandle: dragHandleSlot(theme.spacing), + dragHandleHidden: { ...dragHandleSlot(theme.spacing), opacity: 0 }, + // The switch's reserved control height overhangs the header like the Refresh button's hitbox. + cardSwitch: { + marginVertical: -theme.spacing[2], + }, refreshButton: iconHitboxOverhang(false), refreshButtonCompact: iconHitboxOverhang(true), })); diff --git a/packages/app/src/usage/cards.test.ts b/packages/app/src/usage/cards.test.ts new file mode 100644 index 000000000..86e62b3d2 --- /dev/null +++ b/packages/app/src/usage/cards.test.ts @@ -0,0 +1,153 @@ +import { describe, expect, it } from "vitest"; +import { + arrangeUsageCards, + arrangeUsageReports, + DEFAULT_USAGE_PREFERENCES, + setUsageCardOn, + UsagePreferencesSchema, + usageReportsOn, + type UsagePreferences, +} from "./preferences"; +import { resolvePinnedUsage } from "./pinned"; +import type { UsageReportEntry } from "./types"; + +function report(sourceId: string, sourceLabel: string, account?: string): UsageReportEntry { + return { + id: `${sourceId}:${account ?? "default"}`, + sourceId, + sourceLabel, + account: account ? { label: account } : {}, + fetchedAt: "2026-01-01T00:00:00.000Z", + report: { + status: "available", + windows: [{ id: "weekly", label: "Weekly", summary: true, usedPct: 40 }], + }, + }; +} + +const codex = report("codex", "Codex"); +const claudeWork = report("claude", "Claude", "work"); +const claudePersonal = report("claude", "Claude", "personal"); +const copilot = report("copilot", "Copilot"); +const gemini = report("gemini", "Gemini"); + +function ids(reports: readonly UsageReportEntry[]): string[] { + return reports.map((entry) => entry.id); +} + +function arranged(cardOrder: string[]): UsagePreferences { + return { ...DEFAULT_USAGE_PREFERENCES, cardOrder }; +} + +describe("usage card order", () => { + it("shows cards by provider, then account, whatever order they arrive in", () => { + const expected = ["claude:personal", "claude:work", "codex:default", "copilot:default"]; + for (const arrival of [ + [codex, claudeWork, copilot, claudePersonal], + [copilot, claudePersonal, codex, claudeWork], + ]) { + expect(ids(arrangeUsageReports(arrival, DEFAULT_USAGE_PREFERENCES))).toEqual(expected); + } + }); + + it("puts the cards the user arranged first, in their order", () => { + const preferences = arranged(["copilot:default", "claude:work"]); + + expect( + ids(arrangeUsageReports([claudeWork, codex, claudePersonal, copilot], preferences)), + ).toEqual(["copilot:default", "claude:work", "claude:personal", "codex:default"]); + }); + + it("lands a report from a new login in its default slot after the arranged cards", () => { + const preferences = arrangeUsageCards(DEFAULT_USAGE_PREFERENCES, { + moved: "codex:default", + after: ["codex:default", "claude:work"], + }); + const newLogin = report("claude", "Claude", "agent"); + + expect(ids(arrangeUsageReports([newLogin, claudeWork, codex], preferences))).toEqual([ + "codex:default", + "claude:work", + "claude:agent", + ]); + }); + + it("keeps another host's arrangement when a drop on this host moves a card both show", () => { + // Host A shows Codex, Claude and Gemini; host B shows Copilot and the same Claude account. + const hostA = [codex, claudeWork, gemini]; + const hostB = [claudeWork, copilot]; + const arrangedOnA = arrangeUsageCards(DEFAULT_USAGE_PREFERENCES, { + moved: "codex:default", + after: ["codex:default", "claude:work", "gemini:default"], + }); + + const after = ["copilot:default", "claude:work"]; + const arrangedOnB = arrangeUsageCards(arrangedOnA, { moved: "copilot:default", after }); + + expect(ids(arrangeUsageReports(hostA, arrangedOnB))).toEqual([ + "codex:default", + "claude:work", + "gemini:default", + ]); + expect(ids(arrangeUsageReports(hostB, arrangedOnB))).toEqual(after); + }); + + it("moves only the dragged card when it swaps with its neighbour", () => { + // Gemini sits between Codex and Copilot, but only another host shows it. + const preferences = arranged(["codex:default", "gemini:default", "copilot:default"]); + const swapped = arrangeUsageCards(preferences, { + moved: "codex:default", + after: ["copilot:default", "codex:default"], + }); + + expect(ids(arrangeUsageReports([codex, copilot], swapped))).toEqual([ + "copilot:default", + "codex:default", + ]); + expect(ids(arrangeUsageReports([gemini, copilot], swapped))).toEqual([ + "gemini:default", + "copilot:default", + ]); + }); +}); + +describe("usage cards turned off", () => { + it("turns a card off and back on", () => { + const off = setUsageCardOn(DEFAULT_USAGE_PREFERENCES, "codex:default", false); + expect(off.cardsOff).toEqual(["codex:default"]); + expect(setUsageCardOn(off, "codex:default", true).cardsOff).toEqual([]); + }); + + it("leaves off cards out of the sidebar summary", () => { + const preferences = setUsageCardOn(DEFAULT_USAGE_PREFERENCES, "codex:default", false); + + expect(ids(usageReportsOn([codex, claudeWork], preferences))).toEqual(["claude:work"]); + expect( + resolvePinnedUsage([codex, claudeWork], preferences).map((source) => source.key), + ).toEqual(["claude:work"]); + }); + + it("orders the sidebar summary like the cards", () => { + const preferences = arranged(["codex:default"]); + + expect( + resolvePinnedUsage([claudeWork, codex], preferences).map((source) => source.key), + ).toEqual(["codex:default", "claude:work"]); + }); +}); + +describe("stored card preferences", () => { + it("reads settings saved before cards could be arranged as no arrangement", () => { + expect(UsagePreferencesSchema.parse({ displayAs: "used", serverId: null })).toEqual( + DEFAULT_USAGE_PREFERENCES, + ); + }); + + it("survives a save and reload, and ignores a malformed arrangement", () => { + const saved = setUsageCardOn(arranged(["codex:default"]), "claude:work", false); + expect(UsagePreferencesSchema.parse(JSON.parse(JSON.stringify(saved)))).toEqual(saved); + expect( + UsagePreferencesSchema.parse({ displayAs: "used", cardOrder: "bad", cardsOff: [1] }), + ).toEqual(DEFAULT_USAGE_PREFERENCES); + }); +}); diff --git a/packages/app/src/usage/controls.tsx b/packages/app/src/usage/controls.tsx index f0a8c73f5..ffe51271e 100644 --- a/packages/app/src/usage/controls.tsx +++ b/packages/app/src/usage/controls.tsx @@ -16,47 +16,41 @@ export interface UsageHostSelection { } /** - * The controls on the right of every usage title row: the host filter when there is more than one - * host, Refresh all, and the Settings cog. A host that cannot report usage keeps only the host filter. + * The controls on the right of every usage title row: Refresh all and the Settings cog. A host + * that cannot report usage has none. */ export function UsageControls({ view, serverId, onRefresh, - hostSelection, }: { view: UsageView; serverId: string; onRefresh: () => void; - hostSelection?: UsageHostSelection; }) { const busy = view.kind === "loading" || (view.kind === "ready" && view.isRefreshing); + if (view.kind === "unavailable") return null; return ( - {hasHostChoice(hostSelection) ? ( - - ) : null} - {view.kind === "unavailable" ? null : ( - <> - {view.kind === "off" ? null : } - - - )} + {view.kind === "off" ? null : } + ); } -function hasHostChoice( - hostSelection: UsageHostSelection | undefined, -): hostSelection is UsageHostSelection { - return hostSelection !== undefined && hostSelection.hosts.length > 1; +/** The host whose usage shows, when there is more than one to choose from. */ +function UsageHostFilter({ hostSelection }: { hostSelection: UsageHostSelection }) { + return ( + + ); } function usageHostOptionTestID(serverId: string): string { @@ -73,27 +67,28 @@ const styles = StyleSheet.create((theme) => ({ })); /** - * One host’s usage and title-row controls for the Usage modal. - * Returns null when there is nothing to control, so headers leave no empty island. + * One host’s usage for the Usage modal: its title-row controls, null when there is nothing to + * control so the header leaves no empty island, and the host filter for the top of the body, null + * with a single host. */ export function useHostUsageWithControls(hostSelection: UsageHostSelection): { view: UsageView; refresh: () => void; controls: ReactElement | null; + hostFilter: ReactElement | null; } { const { view, refresh } = useHostUsage(hostSelection.serverId); - const hasControls = hasHostChoice(hostSelection) || view.kind !== "unavailable"; const controls = useMemo( () => - hasControls ? ( - - ) : null, - [hasControls, hostSelection, refresh, view], + view.kind === "unavailable" ? null : ( + + ), + [hostSelection.serverId, refresh, view], ); - return { view, refresh, controls }; + const hostFilter = useMemo( + () => + hostSelection.hosts.length > 1 ? : null, + [hostSelection], + ); + return { view, refresh, controls, hostFilter }; } diff --git a/packages/app/src/usage/display.ts b/packages/app/src/usage/display.ts index c62ed78fd..f54c16883 100644 --- a/packages/app/src/usage/display.ts +++ b/packages/app/src/usage/display.ts @@ -1,21 +1,32 @@ import { useCallback, useMemo } from "react"; import { useAppSettings } from "@/hooks/use-settings"; import { + arrangeUsageCards, + arrangeUsageReports, + isUsageCardOff, isUsagePinned, + setUsageCardOn, setUsageDisplayAs, toggleUsagePin, + type UsageCardDrop, type UsageDisplayAs, type UsagePin, type UsagePreferences, } from "./preferences"; import type { UsageReportEntry } from "./types"; -/** What usage views need from the preferences: how to read percents, and the pins. */ +/** What usage views need from the preferences: how to read percents, the pins, and the cards. */ export interface UsageDisplay { displayAs: UsageDisplayAs; setDisplayAs: (displayAs: UsageDisplayAs) => void; isPinned: (pin: UsagePin) => boolean; togglePin: (pin: UsagePin) => void; + /** Reports in the order their cards show. */ + arrange: (reports: readonly UsageReportEntry[]) => UsageReportEntry[]; + /** Saves the order of the shown cards after the user drops one. */ + arrangeCards: (drop: UsageCardDrop) => void; + isCardOn: (reportId: string) => boolean; + setCardOn: (reportId: string, on: boolean) => void; } interface UsagePreferencesState { @@ -41,14 +52,30 @@ export function useUsageDisplay(reports: readonly UsageReportEntry[]): UsagePref }, [updateSettings, reports], ); + const arrangeCards = useCallback( + (drop: UsageCardDrop) => { + void updateSettings((current) => ({ usage: arrangeUsageCards(current.usage, drop) })); + }, + [updateSettings], + ); + const setCardOn = useCallback( + (reportId: string, on: boolean) => { + void updateSettings((current) => ({ usage: setUsageCardOn(current.usage, reportId, on) })); + }, + [updateSettings], + ); const display = useMemo( () => ({ displayAs: preferences.displayAs, setDisplayAs, isPinned: (pin) => isUsagePinned(preferences, pin, reports), togglePin, + arrange: (shown) => arrangeUsageReports(shown, preferences), + arrangeCards, + isCardOn: (reportId) => !isUsageCardOff(preferences, reportId), + setCardOn, }), - [preferences, reports, setDisplayAs, togglePin], + [arrangeCards, preferences, reports, setCardOn, setDisplayAs, togglePin], ); return { preferences, display }; } diff --git a/packages/app/src/usage/format.ts b/packages/app/src/usage/format.ts index 867976a58..16386810f 100644 --- a/packages/app/src/usage/format.ts +++ b/packages/app/src/usage/format.ts @@ -2,7 +2,7 @@ import type { TFunction } from "i18next"; import { i18n } from "@/i18n/i18next"; import { formatTimeOfDay } from "@/utils/time"; import type { UsageDisplayAs } from "./preferences"; -import type { UsageBalanceUnit } from "./types"; +import type { UsageBalanceUnit, UsageReportEntry } from "./types"; const providerUsageLabels = { session: { label: "Session", key: "providerUsage.labels.session" }, @@ -13,6 +13,11 @@ const providerUsageLabels = { monthly_credits: { label: "Monthly credits", key: "providerUsage.labels.monthlyCredits" }, } as const; +/** The source, with the account when the report names one: "Claude (work)". */ +export function formatUsageAccountName(entry: UsageReportEntry): string { + return entry.account.label ? `${entry.sourceLabel} (${entry.account.label})` : entry.sourceLabel; +} + export function formatProviderUsageLabel(id: string, label: string, t: TFunction = i18n.t): string { const reviewPrefix = "Code review · "; if (id.startsWith("code_review:") && label.startsWith(reviewPrefix)) { diff --git a/packages/app/src/usage/list.tsx b/packages/app/src/usage/list.tsx index 10e76ba57..e9218ae32 100644 --- a/packages/app/src/usage/list.tsx +++ b/packages/app/src/usage/list.tsx @@ -1,12 +1,24 @@ +import { useCallback, useMemo, useState } from "react"; import { View } from "react-native"; import { StyleSheet } from "react-native-unistyles"; +import { DraggableList } from "@/components/draggable-list"; +import type { DraggableRenderItemInfo } from "@/components/draggable-list.types"; +import { useIsCompactFormFactor } from "@/constants/layout"; +import { isNative } from "@/constants/platform"; import { settingsStyles } from "@/styles/settings"; import { UsageCard } from "./card"; import type { UsageDisplay } from "./display"; import { useUsageInSidebar } from "./in-sidebar"; import type { UsageReportEntry } from "./types"; -/** One card per report (source + account). Window rows pin while the sidebar summary is on. */ +function reportKey(entry: UsageReportEntry): string { + return entry.id; +} + +/** + * One card per report (source + account), in the user's order. Each card drags by its handle and + * turns off from its switch. Window rows pin while the sidebar summary is on. + */ export function UsageList({ serverId, reports, @@ -17,25 +29,113 @@ export function UsageList({ display: UsageDisplay; }) { const { inSidebar } = useUsageInSidebar(); + const { arrange, arrangeCards } = display; + const arranged = useMemo(() => arrange(reports), [arrange, reports]); + const handleDragEnd = useCallback( + (next: UsageReportEntry[], moved: string) => + arrangeCards({ moved, after: next.map(reportKey) }), + [arrangeCards], + ); + const renderItem = useCallback( + ({ item, isActive }: DraggableRenderItemInfo) => ( + + + + ), + [display, inSidebar, serverId], + ); return ( - - {reports.map((entry) => ( - - - - ))} + + ); +} + +/** + * The card inside its hover target: the drag handle shows while the pointer is over the card or + * keyboard focus is inside it, and always where there is no hover. + */ +function UsageListCard({ + serverId, + entry, + display, + pinnable, + dragging, +}: { + serverId: string; + entry: UsageReportEntry; + display: UsageDisplay; + pinnable: boolean; + dragging: boolean; +}) { + const isCompact = useIsCompactFormFactor(); + const [isHovered, setIsHovered] = useState(false); + const handlePointerEnter = useCallback(() => setIsHovered(true), []); + const handlePointerLeave = useCallback(() => setIsHovered(false), []); + // Focus events bubble from the card's controls, so tabbing onto the handle reveals it. + const [hasFocus, setHasFocus] = useState(false); + const handleFocus = useCallback(() => setHasFocus(true), []); + const handleBlur = useCallback(() => setHasFocus(false), []); + const { isCardOn, setCardOn } = display; + const on = isCardOn(entry.id); + const handleSetOn = useCallback( + (next: boolean) => setCardOn(entry.id, next), + [entry.id, setCardOn], + ); + const arrangement = useMemo( + () => ({ + on, + onSetOn: handleSetOn, + handleVisible: isHovered || hasFocus || dragging || isNative || isCompact, + }), + [dragging, handleSetOn, hasFocus, isCompact, isHovered, on], + ); + return ( + + + + ); } const styles = StyleSheet.create((theme) => ({ + // Each card carries the gap below it, so a native drag moves it with its slot; the list + // takes the last one back. list: { - gap: theme.spacing[3], + marginBottom: -theme.spacing[3], + }, + item: { + paddingBottom: theme.spacing[3], + }, + hoverTarget: { + position: "relative", }, })); diff --git a/packages/app/src/usage/options.tsx b/packages/app/src/usage/options.tsx index cc674cb90..9e12517da 100644 --- a/packages/app/src/usage/options.tsx +++ b/packages/app/src/usage/options.tsx @@ -3,8 +3,11 @@ import { View } from "react-native"; import { Settings } from "lucide-react-native"; import { withUnistyles } from "react-native-unistyles"; import { DropdownMenu, DropdownMenuContent } from "@/components/ui/dropdown-menu"; -import { extraMutedIconColorMapping } from "@/components/ui/icon-button-chrome"; -import { ToolbarButton, paneContentToolbarIconSize } from "@/components/ui/pane-content-toolbar"; +import { + mutedIconColorMapping, + useIconButtonChromeGlyphSize, +} from "@/components/ui/icon-button-chrome"; +import { ToolbarButton } from "@/components/ui/pane-content-toolbar"; import { useIsCompactFormFactor } from "@/constants/layout"; import { SettingsRow, SettingsSwitch } from "@/components/settings"; import { SegmentedControl, type SegmentedControlOption } from "@/components/ui/segmented-control"; @@ -32,6 +35,7 @@ export function UsageOptions({ serverId }: { serverId: string | null }) { }, ]; const compact = useIsCompactFormFactor(); + const glyphSize = useIconButtonChromeGlyphSize("small", compact); const { display } = useUsageDisplay([]); const { inSidebar, setInSidebar } = useUsageInSidebar(); return ( @@ -42,10 +46,7 @@ export function UsageOptions({ serverId }: { serverId: string | null }) { compact={compact} testID="usage-options-menu" > - + { @@ -114,7 +114,7 @@ describe("resolvePinnedUsage", () => { ]); }); - it("groups pinned windows by source in report order, not pin order", () => { + it("lists pinned windows in card order, not pin order", () => { const work = report({ sourceId: "claude", sourceLabel: "Claude", @@ -161,6 +161,28 @@ describe("resolvePinnedUsage", () => { }); }); + it("follows the arranged card order, even when it separates a source's accounts", () => { + const work = report({ + sourceId: "claude", + sourceLabel: "Claude", + account: "work", + windows: [{ id: "weekly", label: "Weekly", usedPct: 90 }], + }); + const sources = resolvePinnedUsage([claude, codex, work], { + ...preferences([ + { sourceId: "claude", windowId: "weekly" }, + { sourceId: "codex", windowId: "weekly" }, + ]), + cardOrder: ["claude:default", "codex:default", "claude:work"], + }); + + expect(sources.map((source) => source.key)).toEqual([ + "claude:default", + "codex:default", + "claude:work", + ]); + }); + it("names a window by its label when the source gives no short label", () => { const items = windows([codex], preferences([{ sourceId: "codex", windowId: "weekly" }])); diff --git a/packages/app/src/usage/pinned.ts b/packages/app/src/usage/pinned.ts index acce210a7..b725c3e08 100644 --- a/packages/app/src/usage/pinned.ts +++ b/packages/app/src/usage/pinned.ts @@ -1,8 +1,13 @@ import type { TFunction } from "i18next"; import { i18n } from "@/i18n/i18next"; -import { formatPct, formatDisplayPct, formatProviderUsageLabel } from "./format"; +import { + formatPct, + formatDisplayPct, + formatProviderUsageLabel, + formatUsageAccountName, +} from "./format"; import { displayPercent, resolveUsageSnapshot } from "./model"; -import { effectiveUsagePins, type UsagePreferences } from "./preferences"; +import { effectiveUsagePins, usageReportsOn, type UsagePreferences } from "./preferences"; import { windowTone } from "./tone"; import type { UsageReportEntry, UsageTone } from "./types"; @@ -55,19 +60,12 @@ export function choosePinnedUsageLayout(input: { } function describe(entry: UsageReportEntry, windowLabel: string): string { - const account = entry.account.label ? ` (${entry.account.label})` : ""; - return `${entry.sourceLabel}${account} ${windowLabel}`; -} - -/** Reports with every account of a source together, sources in the order they first appear. */ -function groupBySource(reports: readonly UsageReportEntry[]): UsageReportEntry[] { - const sourceIds = [...new Set(reports.map((entry) => entry.sourceId))]; - return sourceIds.flatMap((sourceId) => reports.filter((entry) => entry.sourceId === sourceId)); + return `${formatUsageAccountName(entry)} ${windowLabel}`; } /** - * The sidebar Usage item's summary: one group per account, grouped by source, each with its - * windows in its report's order. Accounts with nothing to show are left out. + * The sidebar Usage item's summary: one group per account in card order, each with its windows in + * its report's order. Accounts turned off or with nothing to show are left out. */ export function resolvePinnedUsage( reports: readonly UsageReportEntry[], @@ -75,7 +73,7 @@ export function resolvePinnedUsage( t: TFunction = i18n.t, ): PinnedUsageSource[] { const pins = effectiveUsagePins(preferences, reports); - return groupBySource(reports).flatMap((entry) => { + return usageReportsOn(reports, preferences).flatMap((entry) => { const snapshot = resolveUsageSnapshot(entry); if (!snapshot) return []; const windows = snapshot.report.windows diff --git a/packages/app/src/usage/preferences.test.ts b/packages/app/src/usage/preferences.test.ts index 43dccc466..064920fb5 100644 --- a/packages/app/src/usage/preferences.test.ts +++ b/packages/app/src/usage/preferences.test.ts @@ -31,7 +31,7 @@ describe("usage preferences", () => { it("unpins a pinned window and keeps the others in order", () => { const three = [codexWeekly, claudeFiveHour, { sourceId: "claude", windowId: "weekly" }]; const unpinned = toggleUsagePin({ - preferences: { displayAs: "used", pins: three, serverId: null }, + preferences: { ...DEFAULT_USAGE_PREFERENCES, pins: three }, pin: claudeFiveHour, reports: [], }); @@ -40,18 +40,18 @@ describe("usage preferences", () => { }); it("switches between used and remaining without touching pins", () => { - const preferences = { displayAs: "used" as const, pins: [codexWeekly], serverId: null }; + const preferences = { ...DEFAULT_USAGE_PREFERENCES, pins: [codexWeekly] }; expect(setUsageDisplayAs(preferences, "remaining")).toEqual({ + ...DEFAULT_USAGE_PREFERENCES, displayAs: "remaining", pins: [codexWeekly], - serverId: null, }); }); it("remembers the picked host without touching pins", () => { expect(setUsageHost({ ...DEFAULT_USAGE_PREFERENCES, pins: [codexWeekly] }, "server")).toEqual({ - displayAs: "used", + ...DEFAULT_USAGE_PREFERENCES, pins: [codexWeekly], serverId: "server", }); @@ -59,7 +59,7 @@ describe("usage preferences", () => { it("reads preferences saved before hosts could be picked as no pick", () => { expect(UsagePreferencesSchema.parse({ displayAs: "remaining", pinned: [codexWeekly] })).toEqual( - { displayAs: "remaining", pins: [codexWeekly], serverId: null }, + { ...DEFAULT_USAGE_PREFERENCES, displayAs: "remaining", pins: [codexWeekly] }, ); }); }); @@ -125,6 +125,6 @@ it("reads old selections without losing display or host and preserves an explici ] as const) { expect( UsagePreferencesSchema.parse({ displayAs: "remaining", serverId: "host", ...input }), - ).toEqual({ displayAs: "remaining", serverId: "host", pins }); + ).toEqual({ ...DEFAULT_USAGE_PREFERENCES, displayAs: "remaining", serverId: "host", pins }); } }); diff --git a/packages/app/src/usage/preferences.ts b/packages/app/src/usage/preferences.ts index b9c5c6fde..7ad7d5a3e 100644 --- a/packages/app/src/usage/preferences.ts +++ b/packages/app/src/usage/preferences.ts @@ -18,15 +18,22 @@ export interface UsagePreferences { pins: UsagePin[] | null; /** The host the user picked to show usage for; null until they pick one. */ serverId: string | null; + /** Report IDs the user arranged, in their order. Shared by every host. */ + cardOrder: string[]; + /** Report IDs the user turned off. Their cards collapse and leave the sidebar summary. */ + cardsOff: string[]; } export const DEFAULT_USAGE_PREFERENCES: UsagePreferences = { displayAs: "used", pins: null, serverId: null, + cardOrder: [], + cardsOff: [], }; const pinsSchema = z.array(z.object({ sourceId: z.string(), windowId: z.string() })); +const reportIdsSchema = z.array(z.string()).optional().catch(undefined); export const UsagePreferencesSchema = z .object({ @@ -36,10 +43,18 @@ export const UsagePreferencesSchema = z // Old [] meant defaults. Read it only when the new selection is absent. pinned: pinsSchema.optional().catch([]), serverId: z.string().nullable().catch(null), + cardOrder: reportIdsSchema, + cardsOff: reportIdsSchema, }) - .transform(({ displayAs, pins, pinned, serverId }): UsagePreferences => { + .transform(({ displayAs, pins, pinned, serverId, cardOrder, cardsOff }): UsagePreferences => { const legacyPins = pinned && pinned.length > 0 ? pinned : null; - return { displayAs, pins: pins === undefined ? legacyPins : pins, serverId }; + return { + displayAs, + pins: pins === undefined ? legacyPins : pins, + serverId, + cardOrder: cardOrder ?? [], + cardsOff: cardsOff ?? [], + }; }) .catch(DEFAULT_USAGE_PREFERENCES); @@ -104,3 +119,84 @@ export function setUsageDisplayAs( export function setUsageHost(preferences: UsagePreferences, serverId: string): UsagePreferences { return { ...preferences, serverId }; } + +/** Reports the user has not arranged: by provider, then account, then ID. */ +function compareByDefaultOrder(a: UsageReportEntry, b: UsageReportEntry): number { + return ( + a.sourceLabel.localeCompare(b.sourceLabel) || + (a.account.label ?? "").localeCompare(b.account.label ?? "") || + a.id.localeCompare(b.id) + ); +} + +/** + * Reports in the order their cards show: the ones the user arranged first, in their order, then + * the rest in the default order. Arrival order never matters, so a report that streams in late or + * appears for a new login lands in its own slot. + */ +export function arrangeUsageReports( + reports: readonly UsageReportEntry[], + preferences: UsagePreferences, +): UsageReportEntry[] { + const rank = new Map(preferences.cardOrder.map((id, index) => [id, index])); + return [...reports].sort((a, b) => { + const rankA = rank.get(a.id); + const rankB = rank.get(b.id); + if (rankA !== undefined && rankB !== undefined) return rankA - rankB; + if (rankA !== undefined) return -1; + if (rankB !== undefined) return 1; + return compareByDefaultOrder(a, b); + }); +} + +/** Reports whose cards are on, in card order: what the sidebar summary reads. */ +export function usageReportsOn( + reports: readonly UsageReportEntry[], + preferences: UsagePreferences, +): UsageReportEntry[] { + return arrangeUsageReports(reports, preferences).filter( + (entry) => !isUsageCardOff(preferences, entry.id), + ); +} + +/** A drop as the list reports it: the card the user moved and the shown cards' order after it. */ +export interface UsageCardDrop { + moved: string; + after: readonly string[]; +} + +/** + * The order after the user drops a card. Only the moved card changes place: it goes just before + * its new successor among the shown cards, or just after its new predecessor when it lands last. + * Shown cards not yet arranged are added at the end, in their shown order. Other arranged cards + * keep their places relative to each other. The order is shared by every host, so a drop that + * arranges a card another host also shows ranks it ahead of that host's unarranged cards. + */ +export function arrangeUsageCards( + preferences: UsagePreferences, + { moved, after }: UsageCardDrop, +): UsagePreferences { + const arranged = new Set([...preferences.cardOrder, moved]); + const rest = [ + ...preferences.cardOrder.filter((id) => id !== moved), + ...after.filter((id) => !arranged.has(id)), + ]; + const index = after.indexOf(moved); + const successor = after[index + 1]; + const at = successor === undefined ? rest.indexOf(after[index - 1]) + 1 : rest.indexOf(successor); + rest.splice(at, 0, moved); + return { ...preferences, cardOrder: rest }; +} + +export function isUsageCardOff(preferences: UsagePreferences, reportId: string): boolean { + return preferences.cardsOff.includes(reportId); +} + +export function setUsageCardOn( + preferences: UsagePreferences, + reportId: string, + on: boolean, +): UsagePreferences { + const others = preferences.cardsOff.filter((id) => id !== reportId); + return { ...preferences, cardsOff: on ? others : [...others, reportId] }; +} diff --git a/packages/app/src/usage/refresh-button.tsx b/packages/app/src/usage/refresh-button.tsx index 473e4442c..80a31389e 100644 --- a/packages/app/src/usage/refresh-button.tsx +++ b/packages/app/src/usage/refresh-button.tsx @@ -1,9 +1,12 @@ import { useTranslation } from "react-i18next"; import { RotateCw } from "lucide-react-native"; import { withUnistyles } from "react-native-unistyles"; -import { extraMutedIconColorMapping } from "@/components/ui/icon-button-chrome"; +import { + mutedIconColorMapping, + useIconButtonChromeGlyphSize, +} from "@/components/ui/icon-button-chrome"; import { LoadingSpinner } from "@/components/ui/loading-spinner"; -import { ToolbarButton, paneContentToolbarIconSize } from "@/components/ui/pane-content-toolbar"; +import { ToolbarButton } from "@/components/ui/pane-content-toolbar"; import { useIsCompactFormFactor } from "@/constants/layout"; const ThemedRotateCw = withUnistyles(RotateCw); @@ -16,7 +19,7 @@ const ThemedLoadingSpinner = withUnistyles(LoadingSpinner); export function UsageRefreshButton({ busy, onRefresh }: { busy: boolean; onRefresh: () => void }) { const { t } = useTranslation(); const compact = useIsCompactFormFactor(); - const iconSize = paneContentToolbarIconSize(compact); + const iconSize = useIconButtonChromeGlyphSize("small", compact); return ( {busy ? ( - + ) : ( - + )} ); diff --git a/packages/app/src/usage/usage-modal.tsx b/packages/app/src/usage/usage-modal.tsx index 4e049c0b5..f78b9ac3e 100644 --- a/packages/app/src/usage/usage-modal.tsx +++ b/packages/app/src/usage/usage-modal.tsx @@ -17,8 +17,8 @@ interface UsageModalProps { /** * Usage over the current screen: a dialog on wide layouts, a bottom sheet on compact ones. It - * shows the usage host's reports with pins, and the host filter, Refresh all, and Settings in its - * title row. + * shows the usage host's reports with pins, Refresh all and Settings in its title row, and the + * host filter at the top of its body. */ export function UsageModal(props: UsageModalProps) { const { t } = useTranslation(); @@ -56,10 +56,10 @@ function HostUsageModal({ () => ({ hosts, serverId, onSelect: onSelectHost }), [hosts, onSelectHost, serverId], ); - const { view, refresh, controls } = useHostUsageWithControls(hostSelection); + const { view, refresh, controls, hostFilter } = useHostUsageWithControls(hostSelection); const { display } = useUsageDisplay(view.kind === "ready" ? view.reports : []); return ( - + @@ -72,8 +72,9 @@ function UsageModalFrame({ onClose, onDismiss, actions, + hostFilter, children, -}: UsageModalProps & { actions?: ReactNode; children: ReactNode }) { +}: UsageModalProps & { actions?: ReactNode; hostFilter?: ReactNode; children: ReactNode }) { const { t } = useTranslation(); const title = t(builtinSidebarNavLabelKey("usage")); const header = useMemo(() => ({ title, actions }), [actions, title]); @@ -87,6 +88,7 @@ function UsageModalFrame({ contextBridge={null} testID="usage-modal" > + {hostFilter} {children}
); diff --git a/packages/app/src/workspace-labels/manager-modal.tsx b/packages/app/src/workspace-labels/manager-modal.tsx index 8b0905dd1..5ef3252e5 100644 --- a/packages/app/src/workspace-labels/manager-modal.tsx +++ b/packages/app/src/workspace-labels/manager-modal.tsx @@ -191,6 +191,7 @@ export function WorkspaceLabelManagerModal({ selectedHost={state.serverId} onSelectHost={selectHost} includeAllHost={false} + hang triggerTestID="workspace-label-manager-host" hostOptionTestID={managerHostOptionTestID} /> diff --git a/patches/react-native-draggable-flatlist+4.0.3.patch b/patches/react-native-draggable-flatlist+4.0.3.patch index e22f549c4..75f1f060a 100644 --- a/patches/react-native-draggable-flatlist+4.0.3.patch +++ b/patches/react-native-draggable-flatlist+4.0.3.patch @@ -1,8 +1,8 @@ diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts -index d3a4f1f..083bf19 100644 +index d3a4f1f..66e5124 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/components/DraggableFlatList.d.ts -@@ -16,12 +16,14 @@ declare const _default: (props: Omit, "keyExtractor" | "data +@@ -16,12 +16,15 @@ declare const _default: (props: Omit, "keyExtractor" | "data keyExtractor: (item: T, index: number) => string; onDragBegin?: ((index: number) => void) | undefined; onDragEnd?: ((params: import("../types").DragEndParams) => void) | undefined; @@ -14,14 +14,15 @@ index d3a4f1f..083bf19 100644 renderPlaceholder?: import("../types").RenderPlaceholder | undefined; simultaneousHandlers?: React.Ref | React.Ref[] | undefined; + dragGestureHostPresented?: boolean | undefined; ++ dragGestureRef?: React.MutableRefObject | undefined; outerScrollOffset?: Animated.SharedValue | undefined; onAnimValInit?: ((animVals: { activeCellOffset: import("react-native-reanimated").SharedValue; diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts -index d3c6cbb..36e9645 100644 +index d3c6cbb..a586a09 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/components/NestableDraggableFlatList.d.ts -@@ -15,12 +15,14 @@ export declare const NestableDraggableFlatList: (props: Omit(props: Omit string; onDragBegin?: ((index: number) => void) | undefined; onDragEnd?: ((params: import("../types").DragEndParams) => void) | undefined; @@ -33,14 +34,15 @@ index d3c6cbb..36e9645 100644 renderPlaceholder?: import("../types").RenderPlaceholder | undefined; simultaneousHandlers?: React.Ref | React.Ref[] | undefined; + dragGestureHostPresented?: boolean | undefined; ++ dragGestureRef?: React.MutableRefObject | undefined; outerScrollOffset?: Animated.SharedValue | undefined; onAnimValInit?: ((animVals: { activeCellOffset: import("react-native-reanimated").SharedValue; diff --git a/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts b/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts -index 2abb4a2..8ca967f 100644 +index 2abb4a2..c60dedf 100644 --- a/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts +++ b/node_modules/react-native-draggable-flatlist/lib/typescript/types.d.ts -@@ -23,12 +23,16 @@ export declare type DraggableFlatListProps = Modify, { +@@ -23,12 +23,21 @@ export declare type DraggableFlatListProps = Modify, { keyExtractor: (item: T, index: number) => string; onDragBegin?: (index: number) => void; onDragEnd?: (params: DragEndParams) => void; @@ -54,6 +56,11 @@ index 2abb4a2..8ca967f 100644 simultaneousHandlers?: React.Ref | React.Ref[]; + /** @internal Forces handler reattachment when a retained native host is presented. */ + dragGestureHostPresented?: boolean; ++ /** ++ * Ref for the drag pan, so an enclosing gesture can wait for it to fail. The pan then arbitrates ++ * manually, as with `simultaneousHandlers`: it fails on a move unless a row is armed. ++ */ ++ dragGestureRef?: React.MutableRefObject; outerScrollOffset?: Animated.SharedValue; onAnimValInit?: (animVals: ReturnType) => void; itemEnteringAnimation?: AnimateProps["entering"]; @@ -174,7 +181,7 @@ index e4e861a..f485139 100644 declare global { diff --git a/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx b/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx -index 7c88afc..ad75714 100644 +index 7c88afc..a97cb80 100644 --- a/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx +++ b/node_modules/react-native-draggable-flatlist/src/components/DraggableFlatList.tsx @@ -16,9 +16,11 @@ import { @@ -391,14 +398,16 @@ index 7c88afc..ad75714 100644 useAnimatedReaction( () => { return spacerIndexAnim.value; -@@ -271,11 +371,24 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -271,11 +371,26 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { ); const gestureDisabled = useSharedValue(false); + const touchStartX = useSharedValue(0); + const touchStartY = useSharedValue(0); + const manualPanActivated = useSharedValue(false); -+ const coordinatesDragWithExternalGesture = Boolean(props.simultaneousHandlers); ++ const coordinatesDragWithExternalGesture = Boolean( ++ props.simultaneousHandlers || props.dragGestureRef ++ ); - const panGesture = Gesture.Pan() + let panGesture = Gesture.Pan() @@ -417,7 +426,7 @@ index 7c88afc..ad75714 100644 panGestureState.value = evt.state; }) .onUpdate((evt) => { -@@ -286,8 +399,15 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -286,8 +401,15 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { : evt.translationY; touchTranslate.value = translation; }) @@ -434,7 +443,7 @@ index 7c88afc..ad75714 100644 // Set touch val to current translate val isTouchActiveNative.value = false; const translation = horizontalAnim.value -@@ -299,6 +419,7 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -299,6 +421,7 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { // Only call onDragEnd if actually dragging a cell if (activeIndexAnim.value === -1 || disabled.value) return; @@ -442,7 +451,7 @@ index 7c88afc..ad75714 100644 disabled.value = true; runOnJS(onRelease)(activeIndexAnim.value); const springTo = placeholderOffset.value - activeCellOffset.value; -@@ -309,22 +430,84 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -309,22 +432,85 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { runOnJS(onDragEnd)({ from: activeIndexAnim.value, to: spacerIndexAnim.value, @@ -524,13 +533,14 @@ index 7c88afc..ad75714 100644 if (dragHitSlop) panGesture.hitSlop(dragHitSlop); - if (activationDistanceProp) { ++ if (props.dragGestureRef) panGesture.withRef(props.dragGestureRef); + if (coordinatesDragWithExternalGesture) { + panGesture = panGesture.manualActivation(true); + } else if (activationDistanceProp) { const activeOffset = [-activationDistanceProp, activationDistanceProp]; if (props.horizontal) { panGesture.activeOffsetX(activeOffset); -@@ -332,6 +515,21 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -332,6 +518,21 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { panGesture.activeOffsetY(activeOffset); } } @@ -552,7 +562,7 @@ index 7c88afc..ad75714 100644 const onScroll = useStableCallback((scrollOffset: number) => { props.onScrollOffsetChange?.(scrollOffset); -@@ -372,9 +570,11 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { +@@ -372,9 +573,11 @@ function DraggableFlatListInner(props: DraggableFlatListProps) { keyExtractor={keyExtractor} horizontal={!!props.horizontal} layoutAnimationDisabled={layoutAnimationDisabled} @@ -636,10 +646,10 @@ index 75f7db7..e99faf3 100644 return ( diff --git a/node_modules/react-native-draggable-flatlist/src/types.ts b/node_modules/react-native-draggable-flatlist/src/types.ts -index d6755c8..1b244a6 100644 +index d6755c8..af70a01 100644 --- a/node_modules/react-native-draggable-flatlist/src/types.ts +++ b/node_modules/react-native-draggable-flatlist/src/types.ts -@@ -36,12 +36,16 @@ export type DraggableFlatListProps = Modify< +@@ -36,12 +36,21 @@ export type DraggableFlatListProps = Modify< keyExtractor: (item: T, index: number) => string; onDragBegin?: (index: number) => void; onDragEnd?: (params: DragEndParams) => void; @@ -653,6 +663,11 @@ index d6755c8..1b244a6 100644 simultaneousHandlers?: React.Ref | React.Ref[]; + /** @internal Forces handler reattachment when a retained native host is presented. */ + dragGestureHostPresented?: boolean; ++ /** ++ * Ref for the drag pan, so an enclosing gesture can wait for it to fail. The pan then arbitrates ++ * manually, as with `simultaneousHandlers`: it fails on a move unless a row is armed. ++ */ ++ dragGestureRef?: React.MutableRefObject; outerScrollOffset?: Animated.SharedValue; onAnimValInit?: (animVals: ReturnType) => void; itemEnteringAnimation?: AnimateProps["entering"]; diff --git a/plugins/codex-usage-source/server/usage.test.ts b/plugins/codex-usage-source/server/usage.test.ts index 4cdefa2bc..c793d8dd1 100644 --- a/plugins/codex-usage-source/server/usage.test.ts +++ b/plugins/codex-usage-source/server/usage.test.ts @@ -328,9 +328,10 @@ test("reports every named additional limit with its own duration identity", asyn test("a weekly pin means weekly after the provider moves it between slots", async () => { const { resolvePinnedUsage } = await import("../../../packages/app/src/usage/pinned.js"); + const { DEFAULT_USAGE_PREFERENCES } = + await import("../../../packages/app/src/usage/preferences.js"); const preferences = { - displayAs: "used" as const, - serverId: null, + ...DEFAULT_USAGE_PREFERENCES, pins: [{ sourceId: "codex", windowId: "weekly" }], }; for (const primaryWeekly of [false, true]) {