Skip to content

Commit ce0570b

Browse files
committed
Use DM callAPI refactoring
1 parent e95f3e5 commit ce0570b

File tree

5 files changed

+67
-31
lines changed

5 files changed

+67
-31
lines changed

web/libs/datamanager/src/components/MainView/ScatterView/ScatterView.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ export const ScatterView: FC<ScatterViewProps> = observer(
154154
}, [data, settings.classField]);
155155

156156
// load base points via API
157-
const projectId = (view as any)?.root?.SDK?.projectId;
158-
const { basePoints, loading: baseLoading, reload } = useScatterBaseData(projectId, settings);
157+
const datamanager = (view as any)?.root?.SDK;
158+
const projectId = datamanager?.projectId;
159+
const { basePoints, loading: baseLoading, reload } = useScatterBaseData(projectId, settings, datamanager);
159160

160161
const numericPoints: TaskPoint[] = useMemo(() => {
161162
return [...basePoints, ...numericPointsFiltered];
@@ -411,7 +412,7 @@ export const ScatterView: FC<ScatterViewProps> = observer(
411412
</Block>
412413

413414
<DeckGL
414-
key={`scatter-${deckKey}`}
415+
key={`s catter-${deckKey}`}
415416
layers={layers}
416417
views={new OrthographicView({ id: "ortho-view" })}
417418
initialViewState={initialViewState}

web/libs/datamanager/src/components/MainView/ScatterView/hooks/useScatterBaseData.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ const scatterCache = new Map<string, TaskPoint[]>();
3636
*
3737
* @param projectId - Current project ID (undefined before project loaded)
3838
* @param settings - Scatter view settings with classField for categorization
39+
* @param datamanager - Optional datamanager object
3940
* @returns Object with basePoints array, loading state, and reload function
4041
*/
4142
export function useScatterBaseData(
4243
projectId: number | undefined,
4344
settings: ScatterSettings,
45+
datamanager?: { apiCall: (...args: any[]) => Promise<any> },
4446
): UseScatterBaseDataResult {
4547
const [basePoints, setBasePoints] = useState<TaskPoint[]>([]);
4648
const [loading, setLoading] = useState(false);
@@ -79,6 +81,7 @@ export function useScatterBaseData(
7981
classField: settings.classField,
8082
abortSignal: ctrl.signal,
8183
page: currentPage,
84+
datamanager,
8285
});
8386

8487
// Add this page's points to our accumulated array
@@ -106,7 +109,7 @@ export function useScatterBaseData(
106109

107110
// Start the fetching process
108111
fetchAllPages();
109-
}, [projectId, settings.classField]);
112+
}, [projectId, settings.classField, datamanager]);
110113

111114
// Trigger initial load and handle cleanup on unmount
112115
useEffect(() => {

web/libs/datamanager/src/components/MainView/ScatterView/utils/api.ts

+32-14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface ScatterFetchOptions {
1919
pageSize?: number;
2020
/** Callback fired with progress information */
2121
onProgress?: (current: number, total: number) => void;
22+
/** DataManager instance providing apiCall (preferred over window.fetch) */
23+
datamanager?: { apiCall: (...args: any[]) => Promise<any> };
2224
}
2325

2426
export interface ScatterFetchResult {
@@ -49,32 +51,48 @@ export async function fetchScatterPoints(
4951
abortSignal,
5052
page = 1,
5153
pageSize = 10000,
52-
onProgress
54+
onProgress,
55+
datamanager,
5356
} = options;
5457

55-
const queryParams = new URLSearchParams({
56-
project: String(project),
58+
// Build params object once so we can reuse in either apiCall or fetch
59+
const paramsObject: Record<string, string | number> = {
60+
project,
5761
x: "x",
5862
y: "y",
5963
class: classField,
6064
text: "text",
6165
r: "r",
62-
page: String(page),
63-
page_size: String(pageSize)
64-
});
66+
page,
67+
page_size: pageSize,
68+
};
69+
70+
// Response placeholder
71+
let json: any;
72+
73+
if (datamanager?.apiCall) {
74+
// Prefer DataManager apiCall according to DataManager API usage conventions
75+
json = await datamanager.apiCall("scatterTasks", paramsObject, undefined, {
76+
allowToCancel: true,
77+
alwaysExpectJSON: true,
78+
});
79+
} else {
80+
// Fallback to plain fetch for environments where DataManager is not available (e.g. Storybook)
81+
const queryParams = new URLSearchParams(Object.entries(paramsObject).map(([k, v]) => [k, String(v)]));
82+
const resp = await fetch(`/api/scatter/tasks?${queryParams.toString()}`, {
83+
signal: abortSignal,
84+
});
6585

66-
const resp = await fetch(`/api/scatter/tasks?${queryParams.toString()}`, {
67-
signal: abortSignal,
68-
});
86+
if (!resp.ok) {
87+
throw new Error(`Scatter API error ${resp.status}`);
88+
}
6989

70-
if (!resp.ok) {
71-
throw new Error(`Scatter API error ${resp.status}`);
90+
json = await resp.json();
7291
}
7392

74-
const json = await resp.json();
7593
const tasks = json.tasks as any[];
7694
const total: number = json.total;
77-
const actualPageSize: number = json.page_size;
95+
const actualPageSize: number = json.page_size ?? pageSize;
7896

7997
// Map API response to TaskPoint format
8098
const points: TaskPoint[] = tasks.map((t) => ({
@@ -96,6 +114,6 @@ export async function fetchScatterPoints(
96114
total,
97115
page,
98116
pageSize: actualPageSize,
99-
hasMore: points.length === actualPageSize && (page * actualPageSize) < total
117+
hasMore: points.length === actualPageSize && page * actualPageSize < total,
100118
};
101119
}

web/libs/datamanager/src/components/MainView/ScatterView/utils/scatter-tokens.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@ export const CATEGORY_COLORS_OPACITY = 0.75;
77

88
/** Base colors without opacity applied */
99
const BASE_CATEGORY_COLORS: Color[] = [
10-
[255, 107, 107, 255], // red (#ff6b6b)
11-
[72, 219, 251, 255], // blue (#48dbfb)
12-
[254, 202, 87, 255], // yellow (#feca57)
13-
[162, 155, 254, 255], // lavender (#a29bfe)
14-
[255, 127, 80, 255], // coral (#ff7f50)
15-
[255, 105, 180, 255], // hotpink (#ff69b4)
16-
[186, 85, 211, 255], // mediumorchid (#ba55d3)
17-
[60, 179, 113, 255], // mediumseagreen (#3cb371)
18-
[0, 128, 128, 255], // teal (#008080)
19-
[106, 90, 205, 255], // slateblue (#6a5acd)
20-
[0, 255, 0, 255], // lime (#00ff00)
21-
[0, 206, 209, 255], // darkturquoise (#00ced1)
22-
[205, 133, 63, 255], // peru (#cd853f)
10+
[72, 219, 251, 255], // blue (#48dbfb)
11+
[163, 210, 169, 255], // intermediate blue-yellow
12+
[254, 202, 87, 255], // yellow (#feca57)
13+
[208, 178, 170, 255], // intermediate yellow-lavender
14+
[162, 155, 254, 255], // lavender (#a29bfe)
15+
[208, 130, 217, 255], // intermediate lavender-hotpink
16+
[255, 105, 180, 255], // hotpink (#ff69b4)
17+
[220, 95, 195, 255], // intermediate hotpink-mediumorchid
18+
[186, 85, 211, 255], // mediumorchid (#ba55d3)
19+
[123, 132, 162, 255], // intermediate mediumorchid-mediumseagreen
20+
[60, 179, 113, 255], // mediumseagreen (#3cb371)
21+
[30, 153, 120, 255], // intermediate mediumseagreen-teal
22+
[0, 128, 128, 255], // teal (#008080)
23+
[53, 109, 166, 255], // intermediate teal-slateblue
24+
[106, 90, 205, 255], // slateblue (#6a5acd)
25+
[53, 172, 102, 255], // intermediate slateblue-lime
26+
[0, 255, 0, 255], // lime (#00ff00)
27+
[0, 230, 104, 255], // intermediate lime-darkturquoise
28+
[0, 206, 209, 255], // darkturquoise (#00ced1)
29+
[36, 212, 230, 255], // additional turquoise variant
2330
];
2431

2532
/** Helper function to apply opacity to colors */

web/libs/datamanager/src/sdk/api-config.js

+7
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,12 @@ export const APIConfig = {
171171
path: "/../comments/:id",
172172
method: "delete",
173173
},
174+
175+
/** Scatter tasks for scatter plot */
176+
scatterTasks: {
177+
path: "/scatter/tasks",
178+
gateway: "/api",
179+
method: "get",
180+
},
174181
},
175182
};

0 commit comments

Comments
 (0)