Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 36x 36x 36x 36x 4631x 3558x 3558x 3558x 2277x 149x 149x 1147x 1718x 1718x 1718x 1048x 3x 3x 107x 107x 51x 257x 257x 257x 257x 257x 257x 257x 1434x 1434x 1434x 1028x 143x 143x 143x | /**
* useFeedbackTarget.tsx — how a component becomes addressable (`@@T-463`,
* `@@T-487`, `@@D-667`).
*
* Three contexts, each read once per component and never subscribed to the
* store: `FeedbackActiveContext` says whether the mode is on (published once by
* `App`), `FeedbackPageContext` carries the model context a page publishes, and
* `FeedbackPathContext` carries the path the enclosing containers compose.
*
* **A component that is not being annotated must cost nothing.** Every hook here
* answers `undefined` while the mode is off — no identifier composed, no
* attributes allocated, no provider rendered — so the call sites can write
* `{...node?.attributes}` and `{...target?.(…)}`: an optional call does not even
* evaluate its arguments, which is what keeps a `t(label)` out of the off path.
* `FeedbackRegion` hands its child back untouched in that case rather than adding
* a provider, and a parent that renders the element itself composes the step with
* `appendSegment` instead of using it at all.
*/
import type {FeedbackTargetKind} from '@feasibleone/blong';
import {
cloneElement,
createContext,
isValidElement,
use,
useCallback,
useMemo,
useRef,
type ReactElement,
type ReactNode,
} from 'react';
import {
appendSegment,
feedbackAttributes,
pageIdOf,
segmentOf,
type IFeedbackModelContext,
} from './ids.js';
/** Whether the mode is on; published once, by `App` (`@@D-667`). */
export const FeedbackActiveContext = createContext(false);
/** What a page tells its children about itself; `null` outside a model page. */
export const FeedbackPageContext = createContext<IFeedbackModelContext | null>(null);
/** The path of the enclosing container, already composed. */
export const FeedbackPathContext = createContext<string | null>(null);
/** One shared empty result, for the paths that must answer with attributes anyway. */
const INERT: Record<string, string> = {};
/** The mode's own switch, read without subscribing to the store. */
export function useFeedbackActive(): boolean {
return use(FeedbackActiveContext);
}
/**
* The path a page starts from, or `undefined` while the mode is off.
*
* The identifier is only composed when something will carry it, so an inactive
* tree pays two context reads and nothing else.
*/
function usePagePath(active: boolean): string | undefined {
const page = use(FeedbackPageContext);
const inherited = use(FeedbackPathContext);
return useMemo(
() => (active ? (inherited ?? pageIdOf(page)) : undefined),
[active, inherited, page],
);
}
/** The path in scope, for a component that needs to extend it by hand. */
export function useFeedbackPath(): string | undefined {
const active = useFeedbackActive();
return usePagePath(active);
}
export interface IFeedbackNode {
/** The element's own identifier. */
id: string;
/** The path its children extend. */
path: string;
/** Attributes to spread on the element. */
attributes: Record<string, string>;
}
/**
* Publish a composed path to the subtree.
*
* Rendered whether or not the mode is on — with no path when it is off. A call site
* that returned its children bare instead would move the whole subtree under a
* different parent the moment the mode is switched on, and React remounts a subtree
* whose parent changes: a table lost its search and its page, a form lost its
* fields, at exactly the moment the reader meant to comment on them (`@@D-744`).
* Nothing is visible either way: a provider renders no element, and `null` is the
* value a consumer already reads outside one.
*/
export function FeedbackPath({path, children}: {path?: string; children: ReactNode}) {
return <FeedbackPathContext value={path ?? null}>{children}</FeedbackPathContext>;
}
/**
* Address one element and extend the path for its children.
*
* Take the node, spread `attributes`, and wrap the children in
* `<FeedbackPath path={node.path}>` — the whole protocol. `undefined` while the
* mode is off, so the call site spreads nothing and nests nothing.
*/
export function useFeedbackNode(
kind: FeedbackTargetKind,
name?: string,
label?: string,
): IFeedbackNode | undefined {
const active = useFeedbackActive();
const parent = usePagePath(active);
return useMemo(() => {
if (!active || !parent) return undefined;
const id = appendSegment(parent, kind, name);
return {id, path: id, attributes: feedbackAttributes(id, kind, label)};
}, [active, parent, kind, name, label]);
}
/**
* The page root: its identifier and the path its children extend.
*
* Read from the context the component owns rather than one it inherits — an
* `Editor` publishes the context it was given, so it cannot read it from the tree
* above itself.
*/
export function useFeedbackPage(
context: IFeedbackModelContext | null | undefined,
): IFeedbackNode | undefined {
const active = useFeedbackActive();
return useMemo(() => {
Eif (!active) return undefined;
const id = pageIdOf(context);
return {id, path: id, attributes: feedbackAttributes(id, 'page')};
}, [active, context]);
}
/** The identifier of one segment, for a place that builds a path by hand. */
export function segmentId(kind: FeedbackTargetKind, name?: string): string {
return segmentOf(kind, name);
}
/**
* A factory for components that render targets in a loop — a table whose rows are
* columns, a list of cards. Hooks cannot be called inside a `map`, so the context
* is read once here and the returned function composes an identifier per item.
*
* `undefined` while the mode is off: the call sites write `target?.(…)`, and an
* optional call evaluates none of its arguments.
*/
export function useFeedbackTargetFactory(
base?: string,
):
| ((kind: FeedbackTargetKind, name?: string, label?: string) => Record<string, string>)
| undefined {
const active = useFeedbackActive();
const parent = usePagePath(active);
const path = base ?? parent;
const pathRef = useRef(path);
pathRef.current = path;
const compose = useCallback((kind: FeedbackTargetKind, name?: string, label?: string) => {
const current = pathRef.current;
return current
? feedbackAttributes(appendSegment(current, kind, name), kind, label)
: INERT;
}, []);
// `undefined` rather than a function that answers nothing: `factory?.(…)
// stops at the call and never evaluates the arguments.
return active ? compose : undefined;
}
/** Attributes for the page root, when the path is not needed. */
export function useFeedbackPageTarget(
context: IFeedbackModelContext | null | undefined,
): Record<string, string> | undefined {
return useFeedbackPage(context)?.attributes;
}
/**
* Attributes to spread on an element that is addressable but has no addressable
* children — a label, a column header, a button.
*
* `undefined` while the mode is off, so the caller spreads nothing at all.
*
* @param kind the kind of thing the element is
* @param name the model's own name for it (`cardName`, a dotted field path, a
* toolbar action, a menu method); never an index
* @param label the translated visible label, so a reader without the source has
* a chance of finding the element the identifier names
*/
export function useFeedbackTarget(
kind: FeedbackTargetKind,
name?: string,
label?: string,
): Record<string, string> | undefined {
const active = useFeedbackActive();
const parent = usePagePath(active);
return useMemo(
() =>
active && parent
? feedbackAttributes(appendSegment(parent, kind, name), kind, label)
: undefined,
[active, parent, kind, name, label],
);
}
/**
* Address a single element the parent cannot annotate itself.
*
* The parent cannot call a hook for a path its own provider has not published
* yet, and some children own their markup — a PrimeReact table renders its
* columns inside itself, so the table has to be the one carrying the step. This
* clones that one element with the attributes and publishes the path above it.
*
* For anything the parent does render itself, compose the step by hand instead
* (`appendSegment(page.path, 'toolbar')` + `<FeedbackPath>`) — that costs no
* component at all. While the mode is off the child comes back untouched: no
* provider, no clone, no wrapper.
*/
export function FeedbackRegion({
kind,
name,
label,
children,
}: {
kind: FeedbackTargetKind;
name?: string;
label?: string;
children: ReactNode;
}) {
const node = useFeedbackNode(kind, name, label);
// The point of the region is that it adds no element of its own: the caller's
// child *is* the target, so the attributes are merged into it rather than into
// a wrapper that would change every layout it touches.
const body =
!node || !isValidElement(children)
? children
: // eslint-disable-next-line @eslint-react/no-clone-element
cloneElement(children as ReactElement<Record<string, string>>, node.attributes);
// The provider is rendered whether or not the mode is on, and the tree keeps its
// shape across the toggle. Returning `children` bare would move everything a
// region annotates underneath a different parent at the moment the mode is
// switched on, and React remounts a subtree when its parent changes: a table lost
// its search, its filters, its sort and its page, and re-read the list under the
// reader — which is the state they were about to comment on (`@@D-744`).
// A context provider renders no element, so an inactive tree still gains nothing
// it can see; `null` is the value `useFeedbackPath` already answers outside one.
return <FeedbackPathContext value={node?.path ?? null}>{body}</FeedbackPathContext>;
}
|