cinny/src/app/organisms/navigation/DrawerBreadcrumb.jsx

134 lines
4.4 KiB
React
Raw Normal View History

import React, { useState, useEffect, useRef } from 'react';
2021-09-05 13:26:34 +00:00
import PropTypes from 'prop-types';
2021-09-03 12:28:01 +00:00
import './DrawerBreadcrumb.scss';
import { twemojify } from '../../../util/twemojify';
2021-09-03 12:28:01 +00:00
import initMatrix from '../../../client/initMatrix';
import cons from '../../../client/state/cons';
import { selectSpace } from '../../../client/action/navigation';
import navigation from '../../../client/state/navigation';
import { abbreviateNumber } from '../../../util/common';
2021-09-03 12:28:01 +00:00
import Text from '../../atoms/text/Text';
import RawIcon from '../../atoms/system-icons/RawIcon';
import Button from '../../atoms/button/Button';
import ScrollView from '../../atoms/scroll/ScrollView';
import NotificationBadge from '../../atoms/badge/NotificationBadge';
2021-09-03 12:28:01 +00:00
import ChevronRightIC from '../../../../public/res/ic/outlined/chevron-right.svg';
2021-09-05 13:26:34 +00:00
function DrawerBreadcrumb({ spaceId }) {
const [, forceUpdate] = useState({});
2021-09-03 12:28:01 +00:00
const scrollRef = useRef(null);
const { roomList, notifications } = initMatrix;
2021-09-03 12:28:01 +00:00
const mx = initMatrix.matrixClient;
const spacePath = navigation.selectedSpacePath;
function onNotiChanged(roomId, total, prevTotal) {
if (total === prevTotal) return;
if (navigation.selectedSpacePath.includes(roomId)) {
forceUpdate({});
}
if (navigation.selectedSpacePath[0] === cons.tabs.HOME) {
if (!roomList.isOrphan(roomId)) return;
if (roomList.directs.has(roomId)) return;
forceUpdate({});
}
}
2021-09-05 13:26:34 +00:00
useEffect(() => {
2021-09-03 12:28:01 +00:00
requestAnimationFrame(() => {
if (scrollRef?.current === null) return;
scrollRef.current.scrollLeft = scrollRef.current.scrollWidth;
});
notifications.on(cons.events.notifications.NOTI_CHANGED, onNotiChanged);
return () => {
notifications.removeListener(cons.events.notifications.NOTI_CHANGED, onNotiChanged);
};
2021-09-05 13:26:34 +00:00
}, [spaceId]);
2021-09-03 12:28:01 +00:00
2021-09-05 13:26:34 +00:00
if (spacePath.length === 1) return null;
2021-09-03 12:28:01 +00:00
function getHomeNotiExcept(childId) {
const orphans = roomList.getOrphans();
const childIndex = orphans.indexOf(childId);
if (childId !== -1) orphans.splice(childIndex, 1);
let noti = null;
orphans.forEach((roomId) => {
if (!notifications.hasNoti(roomId)) return;
if (noti === null) noti = { total: 0, highlight: 0 };
const childNoti = notifications.getNoti(roomId);
noti.total += childNoti.total;
noti.highlight += childNoti.highlight;
});
return noti;
}
function getNotiExcept(roomId, childId) {
if (!notifications.hasNoti(roomId)) return null;
const noti = notifications.getNoti(roomId);
if (!notifications.hasNoti(childId)) return noti;
if (noti.from === null) return noti;
if (noti.from.has(childId) && noti.from.size === 1) return null;
const childNoti = notifications.getNoti(childId);
return {
total: noti.total - childNoti.total,
highlight: noti.highlight - childNoti.highlight,
};
}
2021-09-03 12:28:01 +00:00
return (
<div className="breadcrumb__wrapper">
<ScrollView ref={scrollRef} horizontal vertical={false} invisible>
<div className="breadcrumb">
{
2021-09-05 13:26:34 +00:00
spacePath.map((id, index) => {
const noti = (id !== cons.tabs.HOME && index < spacePath.length)
? getNotiExcept(id, (index === spacePath.length - 1) ? null : spacePath[index + 1])
: getHomeNotiExcept((index === spacePath.length - 1) ? null : spacePath[index + 1]);
2021-09-05 13:26:34 +00:00
return (
<React.Fragment
key={id}
2021-09-03 12:28:01 +00:00
>
{ index !== 0 && <RawIcon size="extra-small" src={ChevronRightIC} />}
2021-09-05 13:26:34 +00:00
<Button
className={index === spacePath.length - 1 ? 'breadcrumb__btn--selected' : ''}
onClick={() => selectSpace(id)}
>
<Text variant="b2">{id === cons.tabs.HOME ? 'Home' : twemojify(mx.getRoom(id).name)}</Text>
{ noti !== null && (
<NotificationBadge
alert={noti.highlight !== 0}
content={noti.total > 0 ? abbreviateNumber(noti.total) : null}
/>
)}
2021-09-05 13:26:34 +00:00
</Button>
</React.Fragment>
);
})
2021-09-03 12:28:01 +00:00
}
<div style={{ width: 'var(--sp-extra-tight)', height: '100%' }} />
</div>
</ScrollView>
</div>
);
}
2021-09-05 13:26:34 +00:00
DrawerBreadcrumb.defaultProps = {
spaceId: null,
};
DrawerBreadcrumb.propTypes = {
spaceId: PropTypes.string,
};
2021-09-03 12:28:01 +00:00
export default DrawerBreadcrumb;