Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/viewer/src/explore/ExploreContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { act, render } from '@testing-library/react';
import { afterEach, describe, expect, it } from 'vitest';
import { commerceApiLayout } from '../samples/commerce-api.layout';
import { ExploreProvider, useExplore } from './ExploreContext';
import type { Emphasis } from './emphasis';
import type { ExploreAction } from './state';

const draft = commerceApiLayout.draft;
const first = draft.nodes[0]!;

afterEach(() => history.replaceState(null, '', location.pathname));

function mount() {
const seen: Emphasis[] = [];
let send!: (a: ExploreAction) => void;
function Probe() {
const { emphasis, dispatch } = useExplore();
seen.push(emphasis);
send = dispatch;
return null;
}
render(
<ExploreProvider draft={draft} syncHash={false}>
<Probe />
</ExploreProvider>,
);
return { latest: () => seen[seen.length - 1]!, dispatch: (a: ExploreAction) => act(() => send(a)) };
}

describe('ExploreProvider emphasis', () => {
it('keeps the same emphasis through play and speed changes', () => {
const { latest, dispatch } = mount();
const before = latest();
dispatch({ type: 'togglePlay' });
dispatch({ type: 'cycleSpeed' });
expect(latest()).toBe(before);
});

it('rebuilds emphasis when the selection changes', () => {
const { latest, dispatch } = mount();
const before = latest();
dispatch({ type: 'select', id: first.id });
expect(latest()).not.toBe(before);
expect(latest().nodes.get(first.id)).toBe('focus');
});

it('rebuilds emphasis when a type is hidden', () => {
const { latest, dispatch } = mount();
const before = latest();
dispatch({ type: 'toggleType', nodeType: first.type });
expect(latest()).not.toBe(before);
expect(latest().nodes.get(first.id)).toBe('dim');
});
});
7 changes: 6 additions & 1 deletion packages/viewer/src/explore/ExploreContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export function ExploreProvider({
const known = useMemo(() => knownOf(draft), [draft]);
const [state, dispatch] = useReducer(explore, undefined, () => (syncHash ? parseHash(location.hash, known) : INITIAL));
const graph = useMemo(() => buildGraph(draft.nodes.map((n) => n.id), draft.edges, { timed: draft.kind === 'sequence' }), [draft]);
const em = useMemo(() => emphasis(draft, graph, state), [draft, graph, state]);
const { peek, view, query, trace, selected, route, routing, hiddenTypes } = state;
// Only what emphasis reads: a speed or play toggle must not rebuild it, or the playing flow restarts from 0.
const em = useMemo(
() => emphasis(draft, graph, { peek, view, query, trace, selected, route, routing, hiddenTypes }),
[draft, graph, peek, view, query, trace, selected, route, routing, hiddenTypes],
);
const stateRef = useRef(state);
stateRef.current = state;

Expand Down
5 changes: 4 additions & 1 deletion packages/viewer/src/explore/emphasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export function searchMatches(draft: DiagramDraft, query: string): DiagramNode[]
return hits.map((n, i) => ({ n, i })).sort((a, b) => prefix(a.n) - prefix(b.n) || a.i - b.i).map((x) => x.n);
}

export function emphasis(draft: DiagramDraft, graph: Graph, s: ExploreState): Emphasis {
/** The explorer state emphasis reads; playback fields (playing, speed) and reveal don't change what's lit. */
export type EmphasisInput = Pick<ExploreState, 'peek' | 'view' | 'query' | 'trace' | 'selected' | 'route' | 'routing' | 'hiddenTypes'>;

export function emphasis(draft: DiagramDraft, graph: Graph, s: EmphasisInput): Emphasis {
if (s.peek) return peekAt(draft, s.peek);
const view = s.view ? new Set(draft.views?.find((v) => v.id === s.view)?.nodes ?? []) : null;
const q = s.query?.trim() ? new Set(searchMatches(draft, s.query).map((n) => n.id)) : null;
Expand Down
Loading