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,
|
2024-07-22 01:45:08 +00:00
|
|
|
repoID,
|
|
|
|
viewID,
|
2024-06-29 09:58:27 +00:00
|
|
|
...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
|
|
|
|
2024-08-09 10:01:58 +00:00
|
|
|
const reloadMetadata = useCallback(() => {
|
|
|
|
setLoading(true);
|
|
|
|
storeRef.current.reload(PER_LOAD_NUMBER).then(() => {
|
|
|
|
setMetadata(storeRef.current.data);
|
|
|
|
setLoading(false);
|
|
|
|
}).catch(error => {
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2024-06-29 09:58:27 +00:00
|
|
|
// init
|
|
|
|
useEffect(() => {
|
2024-07-22 01:45:08 +00:00
|
|
|
setLoading(true);
|
2024-07-03 09:04:30 +00:00
|
|
|
// init context
|
|
|
|
const context = new Context();
|
2024-07-26 09:15:52 +00:00
|
|
|
window.sfMetadata = {};
|
2024-07-03 09:04:30 +00:00
|
|
|
window.sfMetadataContext = context;
|
2024-07-31 04:11:00 +00:00
|
|
|
window.sfMetadataContext.init({ otherSettings: { ...params, repoID, viewID } });
|
2024-07-22 01:45:08 +00:00
|
|
|
storeRef.current = new Store({ context: window.sfMetadataContext, repoId: repoID, viewId: viewID });
|
2024-07-10 10:18:57 +00:00
|
|
|
window.sfMetadataStore = storeRef.current;
|
2024-07-03 09:04:30 +00:00
|
|
|
storeRef.current.initStartIndex();
|
2024-08-09 10:01:58 +00:00
|
|
|
storeRef.current.load(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-08-09 10:01:58 +00:00
|
|
|
const eventBus = window.sfMetadataContext.eventBus;
|
|
|
|
const unsubscribeServerTableChanged = eventBus.subscribe(EVENT_BUS_TYPE.SERVER_TABLE_CHANGED, tableChanged);
|
|
|
|
const unsubscribeTableChanged = eventBus.subscribe(EVENT_BUS_TYPE.LOCAL_TABLE_CHANGED, tableChanged);
|
|
|
|
const unsubscribeHandleTableError = eventBus.subscribe(EVENT_BUS_TYPE.TABLE_ERROR, handleTableError);
|
|
|
|
const unsubscribeUpdateRows = eventBus.subscribe(EVENT_BUS_TYPE.UPDATE_TABLE_ROWS, updateMetadata);
|
|
|
|
const unsubscribeReloadData = eventBus.subscribe(EVENT_BUS_TYPE.RELOAD_DATA, reloadMetadata);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
return () => {
|
2024-07-26 09:15:52 +00:00
|
|
|
window.sfMetadata = {};
|
2024-06-29 09:58:27 +00:00
|
|
|
window.sfMetadataContext.destroy();
|
2024-07-22 01:45:08 +00:00
|
|
|
window.sfMetadataStore.destroy();
|
2024-07-03 09:04:30 +00:00
|
|
|
unsubscribeServerTableChanged();
|
|
|
|
unsubscribeTableChanged();
|
|
|
|
unsubscribeHandleTableError();
|
|
|
|
unsubscribeUpdateRows();
|
2024-08-09 10:01:58 +00:00
|
|
|
unsubscribeReloadData();
|
2024-06-29 09:58:27 +00:00
|
|
|
};
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-07-26 09:15:52 +00:00
|
|
|
}, [repoID, viewID]);
|
2024-06-29 09:58:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|