MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / useDataLoader

Function useDataLoader

web/src/hooks/useDataLoader.js:6–69  ·  view source on GitHub ↗
(
  userState,
  inputs,
  handleInputChange,
  setModels,
  setGroups
)

Source from the content-addressed store, hash-verified

4import { API_ENDPOINTS } from '../constants/playground.constants';
5
6export const useDataLoader = (
7 userState,
8 inputs,
9 handleInputChange,
10 setModels,
11 setGroups
12) => {
13 const { t } = useTranslation();
14
15 const loadModels = useCallback(async () => {
16 try {
17 const res = await API.get(API_ENDPOINTS.USER_MODELS);
18 const { success, message, data } = res.data;
19
20 if (success) {
21 const { modelOptions, selectedModel } = processModelsData(data, inputs.model);
22 setModels(modelOptions);
23
24 if (selectedModel !== inputs.model) {
25 handleInputChange('model', selectedModel);
26 }
27 } else {
28 showError(t(message));
29 }
30 } catch (error) {
31 showError(t('加载模型失败'));
32 }
33 }, [inputs.model, handleInputChange, setModels, t]);
34
35 const loadGroups = useCallback(async () => {
36 try {
37 const res = await API.get(API_ENDPOINTS.USER_GROUPS);
38 const { success, message, data } = res.data;
39
40 if (success) {
41 const userGroup = userState?.user?.group || JSON.parse(localStorage.getItem('user'))?.group;
42 const groupOptions = processGroupsData(data, userGroup);
43 setGroups(groupOptions);
44
45 const hasCurrentGroup = groupOptions.some(option => option.value === inputs.group);
46 if (!hasCurrentGroup) {
47 handleInputChange('group', groupOptions[0]?.value || '');
48 }
49 } else {
50 showError(t(message));
51 }
52 } catch (error) {
53 showError(t('加载分组失败'));
54 }
55 }, [userState, inputs.group, handleInputChange, setGroups, t]);
56
57 // 自动加载数据
58 useEffect(() => {
59 if (userState?.user) {
60 loadModels();
61 loadGroups();
62 }
63 }, [userState?.user, loadModels, loadGroups]);

Callers 1

index.jsFile · 0.90

Calls 7

processModelsDataFunction · 0.90
processGroupsDataFunction · 0.90
showErrorFunction · 0.85
loadGroupsFunction · 0.85
handleInputChangeFunction · 0.50
tFunction · 0.50
loadModelsFunction · 0.50

Tested by

no test coverage detected