1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-01 15:23:05 +00:00
seahub/frontend/src/metadata/metadata-view/hooks/metadata.js

80 lines
2.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable react/prop-types */
import React, { useContext, useEffect, useRef, useState, useCallback } from 'react';
import { toaster } from '@seafile/sf-metadata-ui-component';
import { gettext } from '../../../utils/constants';
import { getErrorMsg } from '../_basic';
import Context from '../context';
import Store from '../store';
import { EVENT_BUS_TYPE } from '../constants';
const MetadataContext = React.createContext(null);
export const MetadataProvider = ({
children,
...params
}) => {
const [isLoading, setLoading] = useState(true);
const [metadata, setMetadata] = useState({ rows: [], columns: [] });
const storeRef = useRef(null);
const tableChanged = useCallback(() => {
setMetadata(storeRef.current.data);
}, []);
const handleTableError = useCallback((error) => {
const errorMsg = getErrorMsg(error);
toaster.danger(gettext(errorMsg));
}, []);
const updateMetadata = useCallback((data) => {
setMetadata(data);
}, []);
// init
useEffect(() => {
// 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();
storeRef.current.loadData().then(() => {
setMetadata(storeRef.current.data);
setLoading(false);
}).catch(error => {
const errorMsg = getErrorMsg(error);
toaster.danger(gettext(errorMsg));
});
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);
return () => {
window.sfMetadataContext.destroy();
unsubscribeServerTableChanged();
unsubscribeTableChanged();
unsubscribeHandleTableError();
unsubscribeUpdateRows();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<MetadataContext.Provider value={{ isLoading, metadata, store: storeRef.current }}>
{children}
</MetadataContext.Provider>
);
};
export const useMetadata = () => {
const context = useContext(MetadataContext);
if (!context) {
throw new Error('\'MetadataContext\' is null');
}
const { isLoading, metadata, store } = context;
return { isLoading, metadata, store };
};