Skip to content

Commit 91a5aa2

Browse files
add degrees selection in test-page
1 parent c8cedb7 commit 91a5aa2

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

packages/app-builder/src/components/Graph/contexts/GraphSessionContext.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ export type GraphRecordRef = {
2626
recordId: string;
2727
};
2828

29+
export const GRAPH_DEGREE_OPTIONS = [1, 2, 3, 4, 5] as const;
30+
export type GraphDegree = (typeof GRAPH_DEGREE_OPTIONS)[number];
31+
export const DEFAULT_GRAPH_DEGREES: GraphDegree = 2;
32+
33+
export function isGraphDegree(value: number): value is GraphDegree {
34+
return (GRAPH_DEGREE_OPTIONS as readonly number[]).includes(value);
35+
}
36+
2937
export type GraphSessionContextValue = {
3038
recordType: string;
3139
setRecordType: (value: string) => void;
3240
recordId: string;
3341
setRecordId: (value: string) => void;
42+
degrees: GraphDegree;
43+
setDegrees: (value: GraphDegree) => void;
3444

3545
graphData: GraphData | null;
3646
/** Incremented on every fetch; keys the graph subtree so it remounts on new data. */
@@ -76,6 +86,8 @@ export function GraphSessionProvider({
7686

7787
const [recordType, setRecordType] = useState(seed?.recordType ?? '');
7888
const [recordId, setRecordId] = useState(seed?.recordId ?? '');
89+
const [degrees, setDegrees] = useState<GraphDegree>(DEFAULT_GRAPH_DEGREES);
90+
const [committedDegrees, setCommittedDegrees] = useState<GraphDegree>(DEFAULT_GRAPH_DEGREES);
7991
const [loadedRecord, setLoadedRecord] = useState<GraphRecordRef | null>(seed);
8092
const [selectedTableNames, setSelectedTableNames] = useState(initialTableNames);
8193
/** `null` means "every group", so groups loading in later are included without a sync effect. */
@@ -112,6 +124,7 @@ export function GraphSessionProvider({
112124
{
113125
recordType: loadedRecord?.recordType ?? '',
114126
recordId: loadedRecord?.recordId ?? '',
127+
degrees: committedDegrees,
115128
...committedFilterParams,
116129
},
117130
loadedRecord != null,
@@ -127,15 +140,26 @@ export function GraphSessionProvider({
127140
if (!recordType || !trimmedRecordId) return;
128141

129142
const sameFilters = graphFilterParamsEqual(draftFilterParams, committedFilterParams);
143+
const sameDegrees = degrees === committedDegrees;
130144
setCommittedFilterParams(draftFilterParams);
145+
setCommittedDegrees(degrees);
131146

132147
if (loadedRecord?.recordType === recordType && loadedRecord?.recordId === trimmedRecordId) {
133-
if (sameFilters) void refetch();
148+
if (sameFilters && sameDegrees) void refetch();
134149
return;
135150
}
136151

137152
setLoadedRecord({ recordType, recordId: trimmedRecordId });
138-
}, [committedFilterParams, draftFilterParams, loadedRecord, recordId, recordType, refetch]);
153+
}, [
154+
committedDegrees,
155+
committedFilterParams,
156+
degrees,
157+
draftFilterParams,
158+
loadedRecord,
159+
recordId,
160+
recordType,
161+
refetch,
162+
]);
139163

140164
const refreshGraph = useCallback(() => {
141165
setCommittedFilterParams(draftFilterParams);
@@ -180,6 +204,8 @@ export function GraphSessionProvider({
180204
setRecordType,
181205
recordId,
182206
setRecordId,
207+
degrees,
208+
setDegrees,
183209
graphData: data ?? null,
184210
graphGeneration: dataUpdatedAt,
185211
isGeneratingGraph: isFetching,
@@ -196,6 +222,7 @@ export function GraphSessionProvider({
196222
[
197223
recordType,
198224
recordId,
225+
degrees,
199226
data,
200227
dataUpdatedAt,
201228
isFetching,

packages/app-builder/src/routes/_app/_builder/test-graph/index.tsx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
11
import { Page } from '@app-builder/components';
2-
import { useGraphSession } from '@app-builder/components/Graph/contexts/GraphSessionContext';
2+
import {
3+
GRAPH_DEGREE_OPTIONS,
4+
type GraphDegree,
5+
isGraphDegree,
6+
useGraphSession,
7+
} from '@app-builder/components/Graph/contexts/GraphSessionContext';
38
import { GraphOptionSelect } from '@app-builder/components/Graph/GraphOptionSelect';
49
import { SessionGraphCanvas } from '@app-builder/components/Graph/SessionGraphCanvas';
510
import { useDataModel } from '@app-builder/services/data/data-model';
611
import { createFileRoute } from '@tanstack/react-router';
7-
import { Button, Card, Input } from 'ui-design-system';
12+
import { Button, Card, Input, ThresholdRange, type ThresholdRangeStep } from 'ui-design-system';
813
import { Icon } from 'ui-icons';
914

1015
export const Route = createFileRoute('/_app/_builder/test-graph/')({
1116
component: TestGraphRoute,
1217
});
1318

19+
const GRAPH_DEGREE_COLORS = {
20+
1: 'var(--color-green-primary)',
21+
2: 'var(--color-green-primary)',
22+
3: 'var(--color-yellow-primary)',
23+
4: 'var(--color-orange-primary)',
24+
5: 'var(--color-red-primary)',
25+
} as const satisfies Record<GraphDegree, string>;
26+
27+
const GRAPH_DEGREE_STEPS: ThresholdRangeStep[] = GRAPH_DEGREE_OPTIONS.map((value) => ({
28+
value,
29+
color: GRAPH_DEGREE_COLORS[value],
30+
}));
31+
1432
function StartRecordPicker({
1533
tableNames,
1634
recordType,
1735
recordId,
36+
degrees,
1837
onRecordTypeChange,
1938
onRecordIdChange,
39+
onDegreesChange,
2040
onLoad,
2141
isLoading,
2242
}: {
2343
tableNames: string[];
2444
recordType: string;
2545
recordId: string;
46+
degrees: GraphDegree;
2647
onRecordTypeChange: (value: string) => void;
2748
onRecordIdChange: (value: string) => void;
49+
onDegreesChange: (value: GraphDegree) => void;
2850
onLoad: () => void;
2951
isLoading: boolean;
3052
}) {
@@ -61,6 +83,18 @@ function StartRecordPicker({
6183
className="min-w-56"
6284
/>
6385
</div>
86+
<ThresholdRange
87+
title="Degrees"
88+
value={degrees}
89+
onChange={(value) => {
90+
if (isGraphDegree(value)) onDegreesChange(value);
91+
}}
92+
values={GRAPH_DEGREE_STEPS}
93+
initialColor={GRAPH_DEGREE_COLORS[1]}
94+
min={GRAPH_DEGREE_OPTIONS[0]}
95+
max={GRAPH_DEGREE_OPTIONS.at(-1)}
96+
className="w-56 pb-md"
97+
/>
6498
<Button variant="primary" disabled={!recordType || !recordId.trim() || isLoading} type="submit">
6599
{isLoading ? (
66100
<>
@@ -81,7 +115,8 @@ function StartRecordPicker({
81115

82116
function TestGraphRoute() {
83117
const dataModel = useDataModel();
84-
const { recordType, setRecordType, recordId, setRecordId, isGeneratingGraph, loadGraph } = useGraphSession();
118+
const { recordType, setRecordType, recordId, setRecordId, degrees, setDegrees, isGeneratingGraph, loadGraph } =
119+
useGraphSession();
85120

86121
const tableNames = dataModel.map((table) => table.name).sort((a, b) => a.localeCompare(b));
87122

@@ -92,8 +127,10 @@ function TestGraphRoute() {
92127
tableNames={tableNames}
93128
recordType={recordType}
94129
recordId={recordId}
130+
degrees={degrees}
95131
onRecordTypeChange={setRecordType}
96132
onRecordIdChange={setRecordId}
133+
onDegreesChange={setDegrees}
97134
onLoad={loadGraph}
98135
isLoading={isGeneratingGraph}
99136
/>

0 commit comments

Comments
 (0)