mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-04 13:56:23 +01:00
Sort Yaak Proxy's custom columns, and complete Add Column's field alone
A sort key can now name a header or query parameter column (`field:header.x-cache`). The database sorts it by the value the column shows, read from the stored headers or URL with the name bound as a parameter, and the app's in-memory sort places live flows the same way. Custom column headers sort on click and Option-click and their menus' sort items work; the sort is kept with the columns and removing a column drops it from the sort. The Add Column field completes `@` and the paths a column accepts, with no colon and no values, opens its menu on focus, normalises a pasted term, and says why a path cannot be added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5951b36ae
commit
e870604607
11 files changed
+629
-86
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import { Compartment, type Extension } from "@codemirror/state";
|
||||
import { startCompletion } from "@codemirror/autocomplete";
|
||||
import { Compartment, EditorState, type Extension } from "@codemirror/state";
|
||||
import { EditorView, ViewPlugin } from "@codemirror/view";
|
||||
import type { Schema } from "@yaakapp-internal/query";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -11,7 +13,12 @@ import { queryFilter } from "@yaakapp-internal/ui/editor";
|
||||
import { atom, useAtom, useSetAtom } from "jotai";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useRpcQuery } from "../hooks/useRpcQuery";
|
||||
import { addCustomColumn, customPath } from "../lib/columns";
|
||||
import {
|
||||
addCustomColumn,
|
||||
customPath,
|
||||
customPathProblem,
|
||||
fieldReferenceChanges,
|
||||
} from "../lib/columns";
|
||||
import { loadSeenValues } from "../lib/searchBox";
|
||||
import { columnPrefsAtom } from "../lib/store";
|
||||
|
||||
@@ -20,9 +27,31 @@ export const addColumnDialogAtom = atom(false);
|
||||
|
||||
const FORM_ID = "add-column";
|
||||
|
||||
/** Keeps the field a reference to a field, `@header.x-cache`, whatever was typed or pasted */
|
||||
const asFieldReference = EditorState.transactionFilter.of((tr) => {
|
||||
if (!tr.docChanged) return tr;
|
||||
const changes = fieldReferenceChanges(tr.newDoc.toString());
|
||||
return changes.length === 0 ? tr : [tr, { changes, sequential: true }];
|
||||
});
|
||||
|
||||
/** Opens completion on the fields while the field is focused and empty */
|
||||
const completeWhenEmpty = [
|
||||
ViewPlugin.define((view) => {
|
||||
if (view.hasFocus && view.state.doc.length === 0) {
|
||||
queueMicrotask(() => startCompletion(view));
|
||||
}
|
||||
return {};
|
||||
}),
|
||||
EditorView.updateListener.of((update) => {
|
||||
if (update.focusChanged && update.view.hasFocus && update.state.doc.length === 0) {
|
||||
startCompletion(update.view);
|
||||
}
|
||||
}),
|
||||
];
|
||||
|
||||
/**
|
||||
* Adds a column for a header or query parameter, named by its field as the search names it,
|
||||
* `header.x-request-id` or `query.page`, completed as the search box completes it
|
||||
* Adds a column for a header or query parameter, named by a reference to its field as the search
|
||||
* names it, `@header.x-request-id` or `@query.page`, completed from the fields and the names seen
|
||||
*/
|
||||
export function AddColumnDialog() {
|
||||
const [open, setOpen] = useAtom(addColumnDialogAtom);
|
||||
@@ -41,13 +70,17 @@ function AddColumnForm({ onClose }: { onClose: () => void }) {
|
||||
const filter = useMemo<Extension>(() => {
|
||||
if (schema == null) return [];
|
||||
const maps: Schema = { ...schema, fields: schema.fields.filter((f) => f.map) };
|
||||
return queryFilter({ schema: maps, loadValues: loadSeenValues });
|
||||
return [
|
||||
queryFilter({ schema: maps, loadValues: loadSeenValues, fieldsOnly: true }),
|
||||
completeWhenEmpty,
|
||||
];
|
||||
}, [schema]);
|
||||
const extensions = useMemo(() => [compartment.current.of(filter)], [filter]);
|
||||
const extensions = useMemo(() => [asFieldReference, compartment.current.of(filter)], [filter]);
|
||||
useEffect(() => {
|
||||
handleRef.current?.dispatch({ effects: compartment.current.reconfigure(filter) });
|
||||
}, [filter]);
|
||||
const path = customPath(text);
|
||||
const problem = customPathProblem(text);
|
||||
|
||||
const submit = () => {
|
||||
if (path == null) return;
|
||||
@@ -77,6 +110,7 @@ function AddColumnForm({ onClose }: { onClose: () => void }) {
|
||||
placeholder="@header.x-request-id"
|
||||
defaultValue=""
|
||||
onChange={setText}
|
||||
validate={problem == null}
|
||||
extensions={extensions}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter" && !e.defaultPrevented) {
|
||||
@@ -85,6 +119,7 @@ function AddColumnForm({ onClose }: { onClose: () => void }) {
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="h-5 pt-1 text-xs text-danger">{problem}</div>
|
||||
</form>
|
||||
<DialogFooter
|
||||
actions={[
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useVirtualizer } from "@tanstack/react-virtual";
|
||||
import { formatSize } from "@yaakapp-internal/lib/formatSize";
|
||||
import type { Flow, FlowSortKey, SortColumn } from "@yaakapp-internal/proxy-lib";
|
||||
import type { Flow, FlowSortKey } from "@yaakapp-internal/proxy-lib";
|
||||
import {
|
||||
ContextMenu,
|
||||
CONTROL_TOOLTIP_DELAY,
|
||||
@@ -365,7 +365,7 @@ export function FlowList({ style, className }: Props) {
|
||||
// A click sorts by the column alone; a third goes back to arrival order, following as Reset
|
||||
// Sort does. Option-click adds the column to the sort, reverses it, then takes it out.
|
||||
const handleSort = useCallback(
|
||||
(column: SortColumn, add: boolean) => {
|
||||
(column: ColumnId, add: boolean) => {
|
||||
const current = latest.current.sort;
|
||||
const next = add ? toggleSortKey(current, column) : nextSort(current, column);
|
||||
if (next.length === 0) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { FlowSortKey, SortColumn } from "@yaakapp-internal/proxy-lib";
|
||||
import type { FlowSortKey } from "@yaakapp-internal/proxy-lib";
|
||||
import {
|
||||
ContextMenu,
|
||||
Dropdown,
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
type ColumnDef,
|
||||
type ColumnId,
|
||||
type ColumnPrefs,
|
||||
type CustomColumnId,
|
||||
canHide,
|
||||
hideOtherColumns,
|
||||
isCustomColumn,
|
||||
@@ -38,7 +39,7 @@ interface Props {
|
||||
prefs: ColumnPrefs;
|
||||
sort: FlowSortKey[];
|
||||
/** A click on a label, or with `add`, an Option-click */
|
||||
onSort: (column: SortColumn, add: boolean) => void;
|
||||
onSort: (column: ColumnId, add: boolean) => void;
|
||||
/** Sets the sort from the menus; empty goes back to arrival order */
|
||||
onSetSort: (sort: FlowSortKey[]) => void;
|
||||
onResetSort: () => void;
|
||||
@@ -67,6 +68,12 @@ export const FlowListHeader = memo(function FlowListHeader({
|
||||
// Widths when a drag began, so each move sets the width from where it started.
|
||||
const startWidth = useRef(0);
|
||||
|
||||
// Taking out the sort's only key goes back to arrival order, as Reset Sort does
|
||||
const removeColumn = (id: CustomColumnId) => {
|
||||
if (sort.length > 0 && sort.every((k) => k.column === id)) onResetSort();
|
||||
setPrefs((p) => removeCustomColumn(p, id));
|
||||
};
|
||||
|
||||
const pickerItems: DropdownItem[] = [
|
||||
{
|
||||
type: "content",
|
||||
@@ -92,7 +99,7 @@ export const FlowListHeader = memo(function FlowListHeader({
|
||||
label: "Remove Column",
|
||||
icon: "trash" as const,
|
||||
color: "danger" as const,
|
||||
onSelect: () => setPrefs((p) => removeCustomColumn(p, c.id as `field:${string}`)),
|
||||
onSelect: () => removeColumn(c.id as CustomColumnId),
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -106,22 +113,13 @@ export const FlowListHeader = memo(function FlowListHeader({
|
||||
];
|
||||
|
||||
const columnItems = (c: ColumnDef): DropdownItem[] => {
|
||||
const column = c.sortable ? (c.id as SortColumn) : null;
|
||||
const sortBy = (descending: boolean) => {
|
||||
if (column != null) setSort([{ column, descending }]);
|
||||
};
|
||||
const addTo = (descending: boolean) => {
|
||||
if (column != null) setSort(addSortKey(sort, column, descending));
|
||||
};
|
||||
const sortBy = (descending: boolean) => setSort([{ column: c.id, descending }]);
|
||||
const addTo = (descending: boolean) => setSort(addSortKey(sort, c.id, descending));
|
||||
return [
|
||||
...(column == null
|
||||
? []
|
||||
: ([
|
||||
{ label: "Sort Ascending", onSelect: () => sortBy(false) },
|
||||
{ label: "Sort Descending", onSelect: () => sortBy(true) },
|
||||
{ label: "Add to Sort Ascending", onSelect: () => addTo(false) },
|
||||
{ label: "Add to Sort Descending", onSelect: () => addTo(true) },
|
||||
] satisfies DropdownItem[])),
|
||||
{ label: "Sort Ascending", onSelect: () => sortBy(false) },
|
||||
{ label: "Sort Descending", onSelect: () => sortBy(true) },
|
||||
{ label: "Add to Sort Ascending", onSelect: () => addTo(false) },
|
||||
{ label: "Add to Sort Descending", onSelect: () => addTo(true) },
|
||||
{ label: "Clear Sorting", disabled: sort.length === 0, onSelect: onResetSort },
|
||||
{ type: "separator" },
|
||||
{
|
||||
@@ -139,7 +137,7 @@ export const FlowListHeader = memo(function FlowListHeader({
|
||||
{
|
||||
label: "Remove Column",
|
||||
color: "danger" as const,
|
||||
onSelect: () => setPrefs((p) => removeCustomColumn(p, c.id as `field:${string}`)),
|
||||
onSelect: () => removeColumn(c.id as CustomColumnId),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
@@ -213,19 +211,15 @@ export const FlowListHeader = memo(function FlowListHeader({
|
||||
data-column={c.id}
|
||||
className="relative h-full flex items-center min-w-0 px-1.5"
|
||||
>
|
||||
{c.sortable ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => onSort(c.id as SortColumn, e.altKey)}
|
||||
// No tooltip: the header sits under the search field, and a tooltip here covered it
|
||||
aria-label={`Sort by ${c.label}`}
|
||||
className={classNames(labelClass, "hover:text-text-subtle")}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
) : (
|
||||
<span className={labelClass}>{label}</span>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => onSort(c.id, e.altKey)}
|
||||
// No tooltip: the header sits under the search field, and a tooltip here covered it
|
||||
aria-label={`Sort by ${c.label}`}
|
||||
className={classNames(labelClass, "hover:text-text-subtle")}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
{/* On the column's right edge, the boundary with the next, resizing this one */}
|
||||
<ResizeHandle
|
||||
side="right"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Flow, GetProxyStateResponse } from "@yaakapp-internal/proxy-lib";
|
||||
import type { Flow, FlowSortKey, GetProxyStateResponse } from "@yaakapp-internal/proxy-lib";
|
||||
import { sortFlows } from "../lib/columns";
|
||||
import { appIconPng, fixtures } from "./fixtures";
|
||||
|
||||
/**
|
||||
@@ -91,10 +92,14 @@ function answer(cmd: string, payload: unknown): unknown {
|
||||
return { actions: [] };
|
||||
case "list_flows": {
|
||||
const many = Number(new URLSearchParams(location.search).get("many") ?? 0);
|
||||
const flows = (many > 0 ? manyFlows(many) : fixtures.map((f) => f.flow))
|
||||
.map((f, i) => ({ ...f, number: 1401 + i }))
|
||||
.reverse();
|
||||
return { flows, total: flows.length, matching: null };
|
||||
const all = (many > 0 ? manyFlows(many) : fixtures.map((f) => f.flow)).map((f, i) => ({
|
||||
...f,
|
||||
number: 1401 + i,
|
||||
}));
|
||||
// In a sort's order, as the database lists it, all in one page
|
||||
const sort = (req.sort ?? []) as FlowSortKey[];
|
||||
const flows = sort.length > 0 ? sortFlows(all, sort) : all.reverse();
|
||||
return { flows, total: flows.length, matching: null, cursor: null };
|
||||
}
|
||||
case "list_seen":
|
||||
return { hosts: [], apps: [] };
|
||||
@@ -179,6 +184,14 @@ function manyFlows(count: number): Flow[] {
|
||||
responseSize: pending ? 0 : base.responseSize,
|
||||
elapsedMs: pending ? null : base.elapsedMs,
|
||||
clientCwd: base.clientBundleId == null ? (folders[i % folders.length] ?? null) : null,
|
||||
// A cache header with a few values and some without, for a column of it to sort
|
||||
responseHeaders:
|
||||
i % 5 === 4
|
||||
? base.responseHeaders
|
||||
: [
|
||||
...base.responseHeaders,
|
||||
{ name: "X-Cache", value: ["HIT", "miss", "Hit", "EXPIRED"][i % 4] ?? "" },
|
||||
],
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
import type { Flow, FlowSortKey } from "@yaakapp-internal/proxy-lib";
|
||||
import { describe, expect, test } from "vite-plus/test";
|
||||
import customSortFixture from "../../../crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json";
|
||||
import {
|
||||
addCustomColumn,
|
||||
addSortKey,
|
||||
cellFilterTerm,
|
||||
type ColumnPrefs,
|
||||
customPath,
|
||||
customPathProblem,
|
||||
customValue,
|
||||
defaultColumnPrefs,
|
||||
fieldReference,
|
||||
fieldReferenceChanges,
|
||||
gridTemplate,
|
||||
gridWidth,
|
||||
hideOtherColumns,
|
||||
@@ -154,6 +158,19 @@ describe("column preferences", () => {
|
||||
expect(prefs).toEqual(defaultColumnPrefs);
|
||||
});
|
||||
|
||||
test("a custom column's sort persists with it and goes when it does", () => {
|
||||
let prefs = addCustomColumn(defaultColumnPrefs, "header.x-cache");
|
||||
const sort: FlowSortKey[] = [
|
||||
{ column: "field:header.x-cache", descending: true },
|
||||
{ column: "status", descending: false },
|
||||
];
|
||||
prefs = { ...prefs, sort };
|
||||
expect(parseColumnPrefs(JSON.parse(JSON.stringify(prefs))).sort).toEqual(sort);
|
||||
// A key for a column that is no longer there is dropped
|
||||
expect(parseColumnPrefs({ ...prefs, custom: [] }).sort).toEqual([sort[1]]);
|
||||
expect(removeCustomColumn(prefs, "field:header.x-cache").sort).toEqual([sort[1]]);
|
||||
});
|
||||
|
||||
test("Operation shows itself once, unless the person set it", () => {
|
||||
const grpc = flow(1, { grpcService: "pkg.Greeter", grpcMethod: "SayHello" });
|
||||
const anonymous = flow(2, { graphqlOperation: "" });
|
||||
@@ -193,6 +210,33 @@ describe("sorting", () => {
|
||||
expect(addSortKey([], "time", true)).toEqual(one("time", true));
|
||||
});
|
||||
|
||||
test("a custom column sorts on click and Option-click as the others do", () => {
|
||||
const cache = "field:header.x-cache";
|
||||
expect(nextSort([], cache)).toEqual(one(cache, false));
|
||||
expect(nextSort(one(cache, false), cache)).toEqual(one(cache, true));
|
||||
expect(toggleSortKey(one("status", true), cache)).toEqual([
|
||||
...one("status", true),
|
||||
...one(cache, false),
|
||||
]);
|
||||
});
|
||||
|
||||
// Flows and the order each sort lists them in, shared with
|
||||
// `a_custom_column_sorts_by_its_value_and_pages` in the lib's `flows.rs`, which checks the
|
||||
// database lists them in the same order.
|
||||
test("a custom column sorts as the database does", () => {
|
||||
const fixture = customSortFixture as {
|
||||
flows: Partial<Flow>[];
|
||||
sorts: { sort: FlowSortKey[]; order: number[] }[];
|
||||
};
|
||||
const flows = fixture.flows.map((f, i) => flow(i, f));
|
||||
for (const { sort, order } of fixture.sorts) {
|
||||
expect(
|
||||
sortFlows(flows, sort).map((f) => f.number),
|
||||
JSON.stringify(sort),
|
||||
).toEqual(order);
|
||||
}
|
||||
});
|
||||
|
||||
// The same flows and orders as `a_sort_on_several_columns_pages_in_its_order` in the lib's
|
||||
// `flows.rs`, so the app places a flow where the database would.
|
||||
const flows = [
|
||||
@@ -261,6 +305,33 @@ describe("values", () => {
|
||||
expect(customPath("query.filter[status]")).toBeNull();
|
||||
});
|
||||
|
||||
test("says tersely why what was typed names no column", () => {
|
||||
expect(customPathProblem("")).toBeNull();
|
||||
expect(customPathProblem("@")).toBeNull();
|
||||
expect(customPathProblem("@req.header.user-agent")).toBeNull();
|
||||
expect(customPathProblem("@hea")).toBe("Incomplete");
|
||||
expect(customPathProblem("@res.header")).toBe("Incomplete");
|
||||
expect(customPathProblem("@header.")).toBe("Missing name");
|
||||
expect(customPathProblem("@query.a b")).toBe("Invalid name");
|
||||
expect(customPathProblem("@host")).toBe("Unknown field");
|
||||
});
|
||||
|
||||
test("keeps the Add Column field a reference to a field", () => {
|
||||
expect(fieldReference("")).toBe("");
|
||||
expect(fieldReference("@")).toBe("@");
|
||||
expect(fieldReference("h")).toBe("@h");
|
||||
expect(fieldReference("header.x-cache")).toBe("@header.x-cache");
|
||||
expect(fieldReference("@req.header.user-agent:")).toBe("@req.header.user-agent");
|
||||
expect(fieldReference(" @@Header.X-Cache: ")).toBe("@Header.X-Cache");
|
||||
expect(fieldReference(":")).toBe("");
|
||||
expect(fieldReference("@:")).toBe("@");
|
||||
expect(fieldReferenceChanges("@header.x")).toEqual([]);
|
||||
expect(fieldReferenceChanges("query.page:")).toEqual([
|
||||
{ from: 0, to: 0, insert: "@" },
|
||||
{ from: 10, to: 11, insert: "" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("a custom column shows the first value, the response's header before the request's", () => {
|
||||
const f = flow(1, {
|
||||
url: "https://a.com/?Page=2&page=3",
|
||||
|
||||
@@ -21,8 +21,6 @@ export interface ColumnDef {
|
||||
align: "left" | "right";
|
||||
/** Shown until the person hides it */
|
||||
defaultVisible: boolean;
|
||||
/** Sorted by in the database; custom columns are not */
|
||||
sortable: boolean;
|
||||
/** The header or query parameter a custom column shows: `header.x-request-id` */
|
||||
path?: string;
|
||||
}
|
||||
@@ -34,7 +32,7 @@ const builtin = (
|
||||
minWidth: number,
|
||||
align: "left" | "right",
|
||||
defaultVisible: boolean,
|
||||
): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible, sortable: true });
|
||||
): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible });
|
||||
|
||||
export const COLUMNS: ColumnDef[] = [
|
||||
builtin("number", "#", 46, 30, "left", true),
|
||||
@@ -72,6 +70,45 @@ export function customPath(typed: string): string | null {
|
||||
return map + key.toLowerCase();
|
||||
}
|
||||
|
||||
/** Why what was typed names no custom column, in a few words; null when it does or is empty */
|
||||
export function customPathProblem(typed: string): string | null {
|
||||
if (customPath(typed) != null) return null;
|
||||
const path = typed.trim().replace(/^@/, "").replace(/:$/, "").trim().toLowerCase();
|
||||
if (path === "") return null;
|
||||
const map = CUSTOM_MAPS.find((m) => path.startsWith(m));
|
||||
if (map != null) return path === map ? "Missing name" : "Invalid name";
|
||||
// On the way to a map: `@hea`, `@req.header`
|
||||
if (CUSTOM_MAPS.some((m) => m.startsWith(path))) return "Incomplete";
|
||||
return "Unknown field";
|
||||
}
|
||||
|
||||
/**
|
||||
* The edits that turn what the Add Column field holds into a reference to a field, `@` and its
|
||||
* path: an `@` in front when it lacks one, and no colon or space after, as a pasted
|
||||
* `@header.x-cache:` or a typed `header.x-cache` has. A lone `@` stays, to complete from.
|
||||
*/
|
||||
export function fieldReferenceChanges(
|
||||
text: string,
|
||||
): { from: number; to: number; insert: string }[] {
|
||||
const lead = /^[\s@]*/.exec(text)?.[0].length ?? 0;
|
||||
const trail = /[\s:]*$/.exec(text.slice(lead))?.[0].length ?? 0;
|
||||
const end = text.length - trail;
|
||||
const at = end > lead || text.includes("@") ? "@" : "";
|
||||
const changes = [];
|
||||
if (text.slice(0, lead) !== at) changes.push({ from: 0, to: lead, insert: at });
|
||||
if (trail > 0) changes.push({ from: end, to: text.length, insert: "" });
|
||||
return changes;
|
||||
}
|
||||
|
||||
/** What the Add Column field shows for `text`, `fieldReferenceChanges` made */
|
||||
export function fieldReference(text: string): string {
|
||||
let result = text;
|
||||
for (const { from, to, insert } of [...fieldReferenceChanges(text)].reverse()) {
|
||||
result = result.slice(0, from) + insert + result.slice(to);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function customColumnId(path: string): CustomColumnId {
|
||||
return `field:${path}`;
|
||||
}
|
||||
@@ -89,7 +126,6 @@ function customDef(id: CustomColumnId): ColumnDef {
|
||||
minWidth: 40,
|
||||
align: "left",
|
||||
defaultVisible: true,
|
||||
sortable: false,
|
||||
path,
|
||||
};
|
||||
}
|
||||
@@ -198,10 +234,13 @@ export function addCustomColumn(prefs: ColumnPrefs, path: string): ColumnPrefs {
|
||||
return { ...prefs, custom, visible: { ...prefs.visible, [id]: true } };
|
||||
}
|
||||
|
||||
/** Takes the column out, and out of the sort */
|
||||
export function removeCustomColumn(prefs: ColumnPrefs, id: CustomColumnId): ColumnPrefs {
|
||||
const { [id]: _v, ...visible } = prefs.visible;
|
||||
const { [id]: _w, ...widths } = prefs.widths;
|
||||
return { ...prefs, custom: prefs.custom.filter((c) => c !== id), visible, widths };
|
||||
const custom = prefs.custom.filter((c) => c !== id);
|
||||
const sort = prefs.sort.filter((k) => k.column !== id);
|
||||
return { ...prefs, custom, visible, widths, sort };
|
||||
}
|
||||
|
||||
export function resizeColumn(prefs: ColumnPrefs, id: ColumnId, width: number): ColumnPrefs {
|
||||
@@ -254,9 +293,11 @@ export function parseColumnPrefs(value: unknown): ColumnPrefs {
|
||||
if (key == null || typeof key !== "object") continue;
|
||||
const { column, descending } = key as Record<string, unknown>;
|
||||
if (typeof column !== "string" || typeof descending !== "boolean") continue;
|
||||
const def = DEFS.get(column as ColumnId);
|
||||
if (def == null || prefs.sort.some((k) => k.column === column)) continue;
|
||||
prefs.sort.push({ column: def.id as SortColumn, descending });
|
||||
// A built-in column, or one the person added that is still there
|
||||
const id = column as ColumnId;
|
||||
const known = isCustomColumn(id) ? prefs.custom.includes(id) : DEFS.has(id);
|
||||
if (!known || prefs.sort.some((k) => k.column === id)) continue;
|
||||
prefs.sort.push({ column: id, descending });
|
||||
}
|
||||
}
|
||||
return prefs;
|
||||
@@ -265,7 +306,7 @@ export function parseColumnPrefs(value: unknown): ColumnPrefs {
|
||||
// -- Sorting --
|
||||
|
||||
/** Numbers sort largest first on the first click, names from A */
|
||||
export function firstDirection(column: SortColumn): boolean {
|
||||
export function firstDirection(column: ColumnId): boolean {
|
||||
switch (column) {
|
||||
case "number":
|
||||
case "status":
|
||||
@@ -283,7 +324,7 @@ export function firstDirection(column: SortColumn): boolean {
|
||||
* A click on a column's label: sort by it alone, reverse the sort it has alone, then, from the
|
||||
* reversed sort, go back to the order flows arrive in
|
||||
*/
|
||||
export function nextSort(sort: FlowSortKey[], column: SortColumn): FlowSortKey[] {
|
||||
export function nextSort(sort: FlowSortKey[], column: ColumnId): FlowSortKey[] {
|
||||
const first = firstDirection(column);
|
||||
const only = sort.length === 1 ? sort[0] : null;
|
||||
if (only?.column !== column) return [{ column, descending: first }];
|
||||
@@ -295,7 +336,7 @@ export function nextSort(sort: FlowSortKey[], column: SortColumn): FlowSortKey[]
|
||||
* An Option-click on a column's label: adds it as the last key, ascending; again reverses it, and
|
||||
* a third time takes it out
|
||||
*/
|
||||
export function toggleSortKey(sort: FlowSortKey[], column: SortColumn): FlowSortKey[] {
|
||||
export function toggleSortKey(sort: FlowSortKey[], column: ColumnId): FlowSortKey[] {
|
||||
const key = sort.find((k) => k.column === column);
|
||||
if (key == null) return [...sort, { column, descending: false }];
|
||||
if (!key.descending)
|
||||
@@ -306,7 +347,7 @@ export function toggleSortKey(sort: FlowSortKey[], column: SortColumn): FlowSort
|
||||
/** Adds `column` as a key in `descending` order: the last key, or in its place if it is one */
|
||||
export function addSortKey(
|
||||
sort: FlowSortKey[],
|
||||
column: SortColumn,
|
||||
column: ColumnId,
|
||||
descending: boolean,
|
||||
): FlowSortKey[] {
|
||||
if (sort.some((k) => k.column === column)) {
|
||||
@@ -328,10 +369,12 @@ function emptyNumber(n: number | null | undefined): number {
|
||||
}
|
||||
|
||||
/**
|
||||
* The value `column` sorts `flow` by, as `SortColumn::sql` in the lib's `flows.rs` makes it in
|
||||
* the database, so a flow placed here lands where a page read from there would have it
|
||||
* The value `column` sorts `flow` by, as `SortBy::sql` in the lib's `flows.rs` makes it in the
|
||||
* database, so a flow placed here lands where a page read from there would have it. A custom
|
||||
* column's is the value it shows, as text.
|
||||
*/
|
||||
export function columnSortKey(flow: Flow, column: SortColumn): string | number {
|
||||
export function columnSortKey(flow: Flow, column: ColumnId): string | number {
|
||||
if (isCustomColumn(column)) return noCase(customValue(flow, column.slice("field:".length)));
|
||||
switch (column) {
|
||||
case "number":
|
||||
return flow.number;
|
||||
|
||||
Reference in new issue
Block a user