Skip to content

Commit

Permalink
Improve/sync title with view (#6789)
Browse files Browse the repository at this point in the history
* update title and favicon

* sync title when rename view
  • Loading branch information
Aries-0331 committed Sep 19, 2024
1 parent bd526ae commit 8f5f51c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
43 changes: 41 additions & 2 deletions frontend/src/metadata/metadata-tree-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ import { isEnter } from '../utils/hotkey';

import './index.css';

const updateFavicon = (iconName) => {
const favicon = document.getElementById('favicon');
if (favicon) {
switch (iconName) {
case 'image':
favicon.href = '/media/favicons/gallery.png';
break;
case 'table':
favicon.href = '/media/favicons/table.png';
break;
default:
favicon.href = '/media/favicons/favicon.png';
}
}
};

const MetadataTreeView = ({ userPerm, currentPath }) => {
const canAdd = useMemo(() => {
if (userPerm !== 'rw' && userPerm !== 'admin') return false;
Expand All @@ -33,11 +49,15 @@ const MetadataTreeView = ({ userPerm, currentPath }) => {
moveView
} = useMetadata();
const [newView, setNewView] = useState(null);

const [showAddViewPopover, setShowAddViewPopover] = useState(false);
const [showInput, setShowInput] = useState(false);
const inputRef = useRef(null);
const [inputValue, setInputValue] = useState('');
const [originalTitle, setOriginalTitle] = useState('');
const inputRef = useRef(null);

useEffect(() => {
setOriginalTitle(document.title);
}, []);

useEffect(() => {
const { origin, pathname, search } = window.location;
Expand All @@ -47,6 +67,8 @@ const MetadataTreeView = ({ userPerm, currentPath }) => {
const lastOpenedView = viewsMap[viewID] || '';
if (lastOpenedView) {
selectView(lastOpenedView);
document.title = `${lastOpenedView.name} - Seafile`;
updateFavicon(VIEW_TYPE_ICON[lastOpenedView.type] || 'table');
return;
}
const url = `${origin}${pathname}`;
Expand All @@ -57,10 +79,27 @@ const MetadataTreeView = ({ userPerm, currentPath }) => {
const firstView = firstViewObject ? viewsMap[firstViewObject._id] : '';
if (showFirstView && firstView) {
selectView(firstView);
document.title = `${firstView.name} - Seafile`;
updateFavicon(VIEW_TYPE_ICON[firstView.type] || 'table');
} else {
document.title = originalTitle;
updateFavicon('default');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const currentViewId = currentPath.split('/').pop();
const currentView = viewsMap[currentViewId];
if (currentView) {
document.title = `${currentView.name} - Seafile`;
updateFavicon(VIEW_TYPE_ICON[currentView.type] || 'table');
} else {
document.title = originalTitle;
updateFavicon('default');
}
}, [currentPath, viewsMap, originalTitle]);

const onUpdateView = useCallback((viewId, update, successCallback, failCallback) => {
updateView(viewId, update, () => {
setState(n => n + 1);
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/metadata/metadata-tree-view/view-item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ const ViewItem = ({
const renameView = useCallback((name, failCallback) => {
onUpdate({ name }, () => {
setRenamePopoverShow(false);
}, failCallback);
}, [onUpdate]);
document.title = `${name} - Seafile`;
}, (error) => {
failCallback(error);
document.title = `${view.name} - Seafile`;
});
}, [onUpdate, view.name]);

const onDragStart = useCallback((event) => {
if (!canDrop) return false;
Expand Down
Binary file added media/favicons/gallery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/favicons/table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8f5f51c

Please sign in to comment.