mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-24 12:58:34 +00:00
optimize kanban (#7054)
* optimize kanban * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code * feat: optimize code --------- Co-authored-by: zhouwenxuan <aries@Mac.local> Co-authored-by: 杨国璇 <ygx@Hello-word.local>
This commit is contained in:
@@ -7,10 +7,10 @@ import DetailItem from '../../../components/dirent-detail/detail-item';
|
||||
import { Utils } from '../../../utils/utils';
|
||||
import metadataAPI from '../../api';
|
||||
import Column from '../../model/metadata/column';
|
||||
import { getCellValueByColumn, getOptionName, getColumnOptionNamesByIds, getColumnOptionNameById, getFileNameFromRecord } from '../../utils/cell';
|
||||
import { getCellValueByColumn, getOptionName, getColumnOptionNamesByIds, getColumnOptionNameById, getFileNameFromRecord, geRecordIdFromRecord, getFileObjIdFromRecord } from '../../utils/cell';
|
||||
import { normalizeFields } from './utils';
|
||||
import { gettext } from '../../../utils/constants';
|
||||
import { CellType, PREDEFINED_COLUMN_KEYS, PRIVATE_COLUMN_KEY } from '../../constants';
|
||||
import { CellType, EVENT_BUS_TYPE, PREDEFINED_COLUMN_KEYS, PRIVATE_COLUMN_KEY } from '../../constants';
|
||||
import { getColumnOptions, getColumnOriginName } from '../../utils/column';
|
||||
import { SYSTEM_FOLDERS } from './constants';
|
||||
import Location from './location';
|
||||
@@ -22,46 +22,24 @@ const MetadataDetails = ({ repoID, filePath, repoInfo, direntType, updateRecord
|
||||
const [metadata, setMetadata] = useState({ record: {}, fields: [] });
|
||||
const permission = useMemo(() => repoInfo.permission !== 'admin' && repoInfo.permission !== 'rw' ? 'r' : 'rw', [repoInfo]);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
if (SYSTEM_FOLDERS.find(folderPath => filePath.startsWith(folderPath))) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const dirName = Utils.getDirName(filePath);
|
||||
const fileName = Utils.getFileName(filePath);
|
||||
let parentDir = direntType === 'file' ? dirName : dirName.slice(0, dirName.length - fileName.length - 1);
|
||||
if (!parentDir.startsWith('/')) {
|
||||
parentDir = '/' + parentDir;
|
||||
}
|
||||
metadataAPI.getMetadataRecordInfo(repoID, parentDir, fileName).then(res => {
|
||||
const { results, metadata } = res.data;
|
||||
const record = Array.isArray(results) && results.length > 0 ? results[0] : {};
|
||||
const fields = normalizeFields(metadata).map(field => new Column(field));
|
||||
updateRecord && updateRecord(record);
|
||||
setMetadata({ record, fields });
|
||||
setLoading(false);
|
||||
}).catch(error => {
|
||||
const errMessage = Utils.getErrorMsg(error);
|
||||
toaster.danger(errMessage);
|
||||
setLoading(false);
|
||||
});
|
||||
}, [repoID, filePath, direntType, updateRecord]);
|
||||
|
||||
const onChange = useCallback((fieldKey, newValue) => {
|
||||
const { record, fields } = metadata;
|
||||
const field = fields.find(f => f.key === fieldKey);
|
||||
const fileName = getColumnOriginName(field);
|
||||
const recordId = geRecordIdFromRecord(record);
|
||||
const fileObjId = getFileObjIdFromRecord(record);
|
||||
let update = { [fileName]: newValue };
|
||||
if (field.type === CellType.SINGLE_SELECT) {
|
||||
update = { [fileName]: getColumnOptionNameById(field, newValue) };
|
||||
} else if (field.type === CellType.MULTIPLE_SELECT) {
|
||||
update = { [fileName]: newValue ? getColumnOptionNamesByIds(field, newValue) : [] };
|
||||
}
|
||||
metadataAPI.modifyRecord(repoID, record._id, update, record._obj_id).then(res => {
|
||||
metadataAPI.modifyRecord(repoID, recordId, update, fileObjId).then(res => {
|
||||
const newMetadata = { ...metadata, record: { ...record, ...update } };
|
||||
setMetadata(newMetadata);
|
||||
if (window?.sfMetadataContext?.eventBus) {
|
||||
window.sfMetadataContext.eventBus.dispatch(EVENT_BUS_TYPE.LOCAL_RECORD_CHANGED, recordId, update);
|
||||
}
|
||||
}).catch(error => {
|
||||
const errorMsg = Utils.getErrorMsg(error);
|
||||
toaster.danger(errorMsg);
|
||||
@@ -98,6 +76,48 @@ const MetadataDetails = ({ repoID, filePath, repoInfo, direntType, updateRecord
|
||||
});
|
||||
}, [repoID, metadata]);
|
||||
|
||||
const localRecordChanged = useCallback((recordId, updates) => {
|
||||
if (geRecordIdFromRecord(metadata?.record) !== recordId) return;
|
||||
const newMetadata = { ...metadata, record: { ...metadata.record, ...updates } };
|
||||
setMetadata(newMetadata);
|
||||
}, [metadata]);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
if (SYSTEM_FOLDERS.find(folderPath => filePath.startsWith(folderPath))) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const dirName = Utils.getDirName(filePath);
|
||||
const fileName = Utils.getFileName(filePath);
|
||||
let parentDir = direntType === 'file' ? dirName : dirName.slice(0, dirName.length - fileName.length - 1);
|
||||
if (!parentDir.startsWith('/')) {
|
||||
parentDir = '/' + parentDir;
|
||||
}
|
||||
metadataAPI.getMetadataRecordInfo(repoID, parentDir, fileName).then(res => {
|
||||
const { results, metadata } = res.data;
|
||||
const record = Array.isArray(results) && results.length > 0 ? results[0] : {};
|
||||
const fields = normalizeFields(metadata).map(field => new Column(field));
|
||||
updateRecord && updateRecord(record);
|
||||
setMetadata({ record, fields });
|
||||
setLoading(false);
|
||||
}).catch(error => {
|
||||
const errMessage = Utils.getErrorMsg(error);
|
||||
toaster.danger(errMessage);
|
||||
setLoading(false);
|
||||
});
|
||||
}, [repoID, filePath, direntType, updateRecord]);
|
||||
|
||||
useEffect(() => {
|
||||
const eventBus = window?.sfMetadataContext?.eventBus;
|
||||
if (!eventBus) return;
|
||||
const unsubscribeLocalRecordChanged = eventBus.subscribe(EVENT_BUS_TYPE.LOCAL_RECORD_DETAIL_CHANGED, localRecordChanged);
|
||||
return () => {
|
||||
unsubscribeLocalRecordChanged();
|
||||
};
|
||||
}, [localRecordChanged]);
|
||||
|
||||
if (isLoading) return null;
|
||||
const { fields, record } = metadata;
|
||||
if (!record._id) return null;
|
||||
|
Reference in New Issue
Block a user