2024-06-29 09:58:27 +00:00
|
|
|
/* eslint-disable react/prop-types */
|
2024-07-03 09:04:30 +00:00
|
|
|
import React, { useContext, useEffect, useRef, useState, useCallback } from 'react';
|
2024-07-04 08:30:57 +00:00
|
|
|
import toaster from '../../../components/toast';
|
2024-06-29 09:58:27 +00:00
|
|
|
import Context from '../context';
|
2024-07-03 09:04:30 +00:00
|
|
|
import Store from '../store';
|
2024-07-04 08:30:57 +00:00
|
|
|
import { EVENT_BUS_TYPE, PER_LOAD_NUMBER } from '../constants';
|
|
|
|
import { Utils } from '../../../utils/utils';
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
const MetadataContext = React.createContext(null);
|
|
|
|
|
|
|
|
export const MetadataProvider = ({
|
|
|
|
children,
|
|
|
|
...params
|
|
|
|
}) => {
|
|
|
|
const [isLoading, setLoading] = useState(true);
|
2024-07-03 09:04:30 +00:00
|
|
|
const [metadata, setMetadata] = useState({ rows: [], columns: [] });
|
|
|
|
const storeRef = useRef(null);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
const tableChanged = useCallback(() => {
|
|
|
|
setMetadata(storeRef.current.data);
|
2024-06-29 09:58:27 +00:00
|
|
|
}, []);
|
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
const handleTableError = useCallback((error) => {
|
2024-07-04 08:30:57 +00:00
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
2024-06-29 09:58:27 +00:00
|
|
|
}, []);
|
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
const updateMetadata = useCallback((data) => {
|
|
|
|
setMetadata(data);
|
|
|
|
}, []);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
// init
|
|
|
|
useEffect(() => {
|
2024-07-03 09:04:30 +00:00
|
|
|
// init context
|
|
|
|
const context = new Context();
|
|
|
|
window.sfMetadataContext = context;
|
|
|
|
window.sfMetadataContext.init({ otherSettings: params });
|
|
|
|
const repoId = window.sfMetadataContext.getSetting('repoID');
|
|
|
|
storeRef.current = new Store({ context: window.sfMetadataContext, repoId });
|
|
|
|
storeRef.current.initStartIndex();
|
2024-07-04 08:30:57 +00:00
|
|
|
storeRef.current.loadData(PER_LOAD_NUMBER).then(() => {
|
2024-07-03 09:04:30 +00:00
|
|
|
setMetadata(storeRef.current.data);
|
|
|
|
setLoading(false);
|
|
|
|
}).catch(error => {
|
2024-07-04 08:30:57 +00:00
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
2024-07-03 09:04:30 +00:00
|
|
|
});
|
2024-06-29 09:58:27 +00:00
|
|
|
|
2024-07-03 09:04:30 +00:00
|
|
|
const unsubscribeServerTableChanged = window.sfMetadataContext.eventBus.subscribe(EVENT_BUS_TYPE.SERVER_TABLE_CHANGED, tableChanged);
|
|
|
|
const unsubscribeTableChanged = window.sfMetadataContext.eventBus.subscribe(EVENT_BUS_TYPE.LOCAL_TABLE_CHANGED, tableChanged);
|
|
|
|
const unsubscribeHandleTableError = window.sfMetadataContext.eventBus.subscribe(EVENT_BUS_TYPE.TABLE_ERROR, handleTableError);
|
|
|
|
const unsubscribeUpdateRows = window.sfMetadataContext.eventBus.subscribe(EVENT_BUS_TYPE.UPDATE_TABLE_ROWS, updateMetadata);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.sfMetadataContext.destroy();
|
2024-07-03 09:04:30 +00:00
|
|
|
unsubscribeServerTableChanged();
|
|
|
|
unsubscribeTableChanged();
|
|
|
|
unsubscribeHandleTableError();
|
|
|
|
unsubscribeUpdateRows();
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2024-07-03 09:04:30 +00:00
|
|
|
<MetadataContext.Provider value={{ isLoading, metadata, store: storeRef.current }}>
|
2024-06-29 09:58:27 +00:00
|
|
|
{children}
|
|
|
|
</MetadataContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useMetadata = () => {
|
|
|
|
const context = useContext(MetadataContext);
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('\'MetadataContext\' is null');
|
|
|
|
}
|
2024-07-03 09:04:30 +00:00
|
|
|
const { isLoading, metadata, store } = context;
|
|
|
|
return { isLoading, metadata, store };
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|