Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerControlsOwnership.ts

63 lines2.6 KB

1/**
2 * Where the permissions control lives, which blocks overflow, and which menu answers the
3 * composer.mode shortcut. Blocks are ordered [traits, mode]; the last block overflows first.
4 * The rule: Permissions have exactly one visible home and composer.mode exactly one target.
5 */
6export function resolveComposerControlsOwnership(input: {
7 /** Permissions hide while the mode is the default, or while the new-thread strip names it. */
8 readonly matchesDefault: boolean;
9 /** The model has an effort (traits) menu, which then always holds Permissions. */
10 readonly hasTraits: boolean;
11 readonly planModeUiEnabled: boolean;
12 readonly collapsed: boolean;
13 /** How many trailing blocks the width measurement moved into the overflow menu. */
14 readonly measuredHiddenBlockCount: number;
15}) {
16 const accessDisplay = !input.matchesDefault
17 ? input.collapsed
18 ? ("icon" as const)
19 : ("label" as const)
20 : // Without traits there is no effort menu to hold Permissions, so keep an icon.
21 input.hasTraits
22 ? ("hidden" as const)
23 : ("icon" as const);
24 const showModeBlock = !(accessDisplay === "hidden" && !input.planModeUiEnabled);
25 const blockIds = [
26 ...(input.hasTraits ? ["traits" as const] : []),
27 ...(showModeBlock ? ["mode" as const] : []),
28 ];
29 const hiddenFor = (count: number) => blockIds.slice(blockIds.length - count);
30 const overflowShowsPermissionsFor = (hidden: ReadonlyArray<string>) =>
31 hidden.includes(input.hasTraits ? "traits" : "mode");
32 const overflowHasContent = (hidden: ReadonlyArray<string>) =>
33 hidden.includes("traits") ||
34 (input.planModeUiEnabled && hidden.includes("mode")) ||
35 overflowShowsPermissionsFor(hidden);
36​
37 let hiddenBlockCount = Math.min(input.measuredHiddenBlockCount, blockIds.length);
38 // An overflow menu with nothing in it must not exist: overflow one more block instead.
39 while (
40 hiddenBlockCount > 0 &&
41 hiddenBlockCount < blockIds.length &&
42 !overflowHasContent(hiddenFor(hiddenBlockCount))
43 ) {
44 hiddenBlockCount += 1;
45 }
46 const hiddenBlockIds = hiddenFor(hiddenBlockCount);
47 const modeHidden = hiddenBlockIds.includes("mode");
48 const traitsHidden = hiddenBlockIds.includes("traits");
49 return {
50 accessDisplay,
51 showModeBlock,
52 hiddenBlockCount,
53 hiddenBlockIds,
54 traitsHidden,
55 modeHidden,
56 /**
57 * The effort chip answers composer.mode while the chip is hidden or overflowed, until the
58 * effort chip overflows too; then the overflow menu (which holds Permissions) answers it.
59 */
60 effortOwnsModeShortcut: (input.matchesDefault || modeHidden) && !traitsHidden,
61 overflowShowsPermissions: overflowShowsPermissionsFor(hiddenBlockIds),
62 };