1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-07 01:41:39 +00:00

feat: storage tree-panel section state (#7282)

Co-authored-by: 杨国璇 <ygx@Hello-word.local>
This commit is contained in:
杨国璇
2024-12-30 19:01:33 +08:00
committed by GitHub
parent 324d981420
commit 50887d21dc
7 changed files with 40 additions and 12 deletions

View File

@@ -224,7 +224,7 @@ class DirColumnView extends React.Component {
updateCurrentDirent={this.props.updateCurrentDirent} updateCurrentDirent={this.props.updateCurrentDirent}
/> />
)} )}
{currentMode === LIST_MODE && {currentMode === LIST_MODE && (
<DirListView <DirListView
path={this.props.path} path={this.props.path}
repoID={this.props.repoID} repoID={this.props.repoID}
@@ -264,8 +264,8 @@ class DirColumnView extends React.Component {
getMenuContainerSize={this.getMenuContainerSize} getMenuContainerSize={this.getMenuContainerSize}
eventBus={this.props.eventBus} eventBus={this.props.eventBus}
/> />
} )}
{currentMode === GRID_MODE && {currentMode === GRID_MODE && (
<DirGridView <DirGridView
path={this.props.path} path={this.props.path}
repoID={this.props.repoID} repoID={this.props.repoID}
@@ -301,7 +301,7 @@ class DirColumnView extends React.Component {
getMenuContainerSize={this.getMenuContainerSize} getMenuContainerSize={this.getMenuContainerSize}
eventBus={this.props.eventBus} eventBus={this.props.eventBus}
/> />
} )}
</div> </div>
</div> </div>
); );

View File

@@ -367,6 +367,8 @@ class DirFiles extends React.Component {
return ( return (
<> <>
<TreeSection <TreeSection
repoID={repoID}
stateStorageKey="files"
title={gettext('Files')} title={gettext('Files')}
renderHeaderOperations={this.renderTreeSectionHeaderOperations} renderHeaderOperations={this.renderTreeSectionHeaderOperations}
> >

View File

@@ -21,7 +21,7 @@ const DirTags = ({ userPerm, repoID, currentPath, currentRepoInfo }) => {
if (!enableMetadata || !enableTags) return null; if (!enableMetadata || !enableTags) return null;
return ( return (
<TreeSection title={gettext('Tags')}> <TreeSection repoID={repoID} title={gettext('Tags')} stateStorageKey="tags">
{!isLoading && (<TagsTreeView userPerm={userPerm} repoID={repoID} currentPath={currentPath} />)} {!isLoading && (<TagsTreeView userPerm={userPerm} repoID={repoID} currentPath={currentPath} />)}
</TreeSection> </TreeSection>
); );

View File

@@ -51,6 +51,8 @@ const DirViews = ({ userPerm, repoID, currentPath, currentRepoInfo }) => {
return ( return (
<> <>
<TreeSection <TreeSection
repoID={repoID}
stateStorageKey="views"
title={gettext('Views')} title={gettext('Views')}
renderHeaderOperations={renderTreeSectionHeaderOperations} renderHeaderOperations={renderTreeSectionHeaderOperations}
> >

View File

@@ -1,19 +1,37 @@
import React, { useCallback, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classnames from 'classnames'; import classnames from 'classnames';
import { TREE_PANEL_SECTION_STATE_KEY } from '../../constants';
import './index.css'; import './index.css';
const TreeSection = ({ title, children, renderHeaderOperations, className }) => { const TreeSection = ({ repoID, stateStorageKey, title, children, renderHeaderOperations, className }) => {
const [showChildren, setShowChildren] = useState(true); const [showChildren, setShowChildren] = useState(true);
const [highlight, setHighlight] = useState(false); const [highlight, setHighlight] = useState(false);
const [freeze, setFreeze] = useState(false); const [freeze, setFreeze] = useState(false);
const storageKey = useMemo(() => `${TREE_PANEL_SECTION_STATE_KEY}_${repoID}`, [repoID]);
useEffect(() => {
if (!stateStorageKey) return;
const stateString = window.localStorage.getItem(storageKey, '{}');
const state = JSON.parse(stateString) || {};
const currentValue = state[stateStorageKey] === false ? false : true;
setShowChildren(currentValue);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const toggleShowChildren = useCallback((event) => { const toggleShowChildren = useCallback((event) => {
event.stopPropagation(); event.stopPropagation();
event.nativeEvent.stopImmediatePropagation(); event.nativeEvent.stopImmediatePropagation();
setShowChildren(!showChildren); const newValue = !showChildren;
}, [showChildren]); setShowChildren(newValue);
if (!stateStorageKey) return;
const stateString = window.localStorage.getItem(storageKey, '{}');
const stateOldValue = JSON.parse(stateString);
const newState = { ...stateOldValue, [stateStorageKey]: newValue };
window.localStorage.setItem(storageKey, JSON.stringify(newState));
}, [showChildren, storageKey, stateStorageKey]);
const onMouseEnter = useCallback(() => { const onMouseEnter = useCallback(() => {
if (freeze) return; if (freeze) return;
@@ -76,6 +94,8 @@ const TreeSection = ({ title, children, renderHeaderOperations, className }) =>
}; };
TreeSection.propTypes = { TreeSection.propTypes = {
repoID: PropTypes.string,
stateStorageKey: PropTypes.string,
title: PropTypes.any.isRequired, title: PropTypes.any.isRequired,
children: PropTypes.any, children: PropTypes.any,
className: PropTypes.string, className: PropTypes.string,

View File

@@ -45,3 +45,7 @@ export const SYSTEM_FOLDERS = [
]; ];
export const DIRENT_DETAIL_SHOW_KEY = 'sf_dirent_detail_show'; export const DIRENT_DETAIL_SHOW_KEY = 'sf_dirent_detail_show';
export const TREE_PANEL_STATE_KEY = 'sf_dir_view_tree_panel_open';
export const TREE_PANEL_SECTION_STATE_KEY = 'sf_dir_view_tree_panel_section_state';

View File

@@ -21,7 +21,7 @@ import FileUploader from '../../components/file-uploader/file-uploader';
import CopyMoveDirentProgressDialog from '../../components/dialog/copy-move-dirent-progress-dialog'; import CopyMoveDirentProgressDialog from '../../components/dialog/copy-move-dirent-progress-dialog';
import DeleteFolderDialog from '../../components/dialog/delete-folder-dialog'; import DeleteFolderDialog from '../../components/dialog/delete-folder-dialog';
import { EVENT_BUS_TYPE } from '../../components/common/event-bus-type'; import { EVENT_BUS_TYPE } from '../../components/common/event-bus-type';
import { PRIVATE_FILE_TYPE, DIRENT_DETAIL_SHOW_KEY } from '../../constants'; import { PRIVATE_FILE_TYPE, DIRENT_DETAIL_SHOW_KEY, TREE_PANEL_STATE_KEY } from '../../constants';
import { MetadataStatusProvider } from '../../hooks'; import { MetadataStatusProvider } from '../../hooks';
import { MetadataProvider, CollaboratorsProvider } from '../../metadata/hooks'; import { MetadataProvider, CollaboratorsProvider } from '../../metadata/hooks';
import { TagsProvider } from '../../tag/hooks'; import { TagsProvider } from '../../tag/hooks';
@@ -50,7 +50,7 @@ class LibContentView extends React.Component {
super(props); super(props);
let isTreePanelShown = true; let isTreePanelShown = true;
const storedTreePanelState = localStorage.getItem('sf_dir_view_tree_panel_open'); const storedTreePanelState = localStorage.getItem(TREE_PANEL_STATE_KEY);
if (storedTreePanelState != undefined) { if (storedTreePanelState != undefined) {
isTreePanelShown = storedTreePanelState === 'true'; isTreePanelShown = storedTreePanelState === 'true';
} }
@@ -2140,7 +2140,7 @@ class LibContentView extends React.Component {
if (this.state.isTreePanelShown) { if (this.state.isTreePanelShown) {
this.loadSidePanel(this.state.path); this.loadSidePanel(this.state.path);
} }
localStorage.setItem('sf_dir_view_tree_panel_open', String(this.state.isTreePanelShown)); localStorage.setItem(TREE_PANEL_STATE_KEY, String(this.state.isTreePanelShown));
}); });
}; };