Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unread message count indicator per conversation #2996

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ts/components/SessionScrollButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useSelector } from 'react-redux';
import styled from 'styled-components';
import { getShowScrollButton } from '../state/selectors/conversations';

import { useSelectedUnreadCount } from '../state/selectors/selectedConversation';
import { SessionIconButton } from './icon';
import { Noop } from '../types/Util';

const SessionScrollButtonDiv = styled.div`
position: fixed;
Expand All @@ -18,8 +18,9 @@ const SessionScrollButtonDiv = styled.div`
}
`;

export const SessionScrollButton = (props: { onClickScrollBottom: Noop }) => {
export const SessionScrollButton = (props: { onClickScrollBottom: () => void }) => {
const show = useSelector(getShowScrollButton);
const unreadCount = useSelectedUnreadCount();

return (
<SessionScrollButtonDiv>
Expand All @@ -29,6 +30,7 @@ export const SessionScrollButton = (props: { onClickScrollBottom: Noop }) => {
isHidden={!show}
onClick={props.onClickScrollBottom}
dataTestId="scroll-to-bottom-button"
unreadCount={unreadCount}
/>
</SessionScrollButtonDiv>
);
Expand Down
1 change: 1 addition & 0 deletions ts/components/icon/SessionIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type SessionIconProps = {
noScale?: boolean;
backgroundColor?: string;
dataTestId?: string;
unreadCount?: number;
};

const getIconDimensionFromIconSize = (iconSize: SessionIconSize | number) => {
Expand Down
7 changes: 4 additions & 3 deletions ts/components/icon/SessionIconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { KeyboardEvent } from 'react';
import classNames from 'classnames';
import _ from 'lodash';
import React, { KeyboardEvent } from 'react';
import styled from 'styled-components';

import { SessionIcon, SessionIconProps } from '.';
import { SessionNotificationCount } from './SessionNotificationCount';
import { SessionNotificationCount, SessionUnreadCount } from './SessionNotificationCount';

interface SProps extends SessionIconProps {
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
Expand Down Expand Up @@ -61,6 +60,7 @@ const SessionIconButtonInner = React.forwardRef<HTMLDivElement, SProps>((props,
dataTestIdIcon,
style,
tabIndex,
unreadCount,
} = props;
const clickHandler = (e: React.MouseEvent<HTMLDivElement>) => {
if (props.onClick) {
Expand Down Expand Up @@ -103,6 +103,7 @@ const SessionIconButtonInner = React.forwardRef<HTMLDivElement, SProps>((props,
dataTestId={dataTestIdIcon}
/>
{Boolean(notificationCount) && <SessionNotificationCount count={notificationCount} />}
{Boolean(unreadCount) && <SessionUnreadCount count={unreadCount} />}
</StyledSessionIconButton>
);
});
Expand Down
59 changes: 35 additions & 24 deletions ts/components/icon/SessionNotificationCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import React from 'react';
import styled from 'styled-components';

type Props = {
overflowingAt: number;
centeredOnTop: boolean;
count?: number;
};

const StyledCountContainer = styled.div<{ shouldRender: boolean }>`
const StyledCountContainer = styled.div<{ centeredOnTop: boolean }>`
position: absolute;
font-size: 18px;
line-height: 1.2;
top: 27px;
left: 28px;
padding: 1px 4px;
opacity: 1;
top: ${props => (props.centeredOnTop ? '-10px' : '27px')};
left: ${props => (props.centeredOnTop ? '50%' : '28px')};
transform: ${props => (props.centeredOnTop ? 'translateX(-50%)' : 'none')};
padding: ${props => (props.centeredOnTop ? '3px 3px' : '1px 4px')};
Bilb marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
align-items: center;
justify-content: center;
Expand All @@ -21,34 +22,44 @@ const StyledCountContainer = styled.div<{ shouldRender: boolean }>`
font-weight: 700;
background: var(--unread-messages-alert-background-color);
transition: var(--default-duration);
opacity: ${props => (props.shouldRender ? 1 : 0)};
text-align: center;
color: var(--unread-messages-alert-text-color);
white-space: ${props => (props.centeredOnTop ? 'nowrap' : 'normal')};
`;

const StyledCount = styled.div`
const StyledCount = styled.div<{ centeredOnTop: boolean }>`
position: relative;
font-size: 0.6rem;
font-size: ${props => (props.centeredOnTop ? 'var(--font-size-xs)' : '0.6rem')};
`;

export const SessionNotificationCount = (props: Props) => {
const { count } = props;
const overflow = Boolean(count && count > 99);
const shouldRender = Boolean(count && count > 0);
const OverflowingAt = (props: { overflowingAt: number }) => {
return (
<>
{props.overflowingAt}
<span>+</span>
</>
);
};

if (overflow) {
return (
<StyledCountContainer shouldRender={shouldRender}>
<StyledCount>
{99}
<span>+</span>
</StyledCount>
</StyledCountContainer>
);
const NotificationOrUnreadCount = ({ centeredOnTop, overflowingAt, count }: Props) => {
if (!count) {
return null;
}
const overflowing = count > overflowingAt;

return (
<StyledCountContainer shouldRender={shouldRender}>
<StyledCount>{count}</StyledCount>
<StyledCountContainer centeredOnTop={centeredOnTop}>
<StyledCount centeredOnTop={centeredOnTop}>
{overflowing ? <OverflowingAt overflowingAt={overflowingAt} /> : count}
</StyledCount>
</StyledCountContainer>
);
};

export const SessionNotificationCount = (props: Pick<Props, 'count'>) => {
return <NotificationOrUnreadCount centeredOnTop={false} overflowingAt={99} count={props.count} />;
yougotwill marked this conversation as resolved.
Show resolved Hide resolved
};

export const SessionUnreadCount = (props: Pick<Props, 'count'>) => {
return <NotificationOrUnreadCount centeredOnTop={true} overflowingAt={999} count={props.count} />;
};
8 changes: 8 additions & 0 deletions ts/state/selectors/selectedConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const getIsSelectedActive = (state: StateType): boolean => {
return Boolean(getSelectedConversation(state)?.activeAt) || false;
};

const getSelectedUnreadCount = (state: StateType) => {
return getSelectedConversation(state)?.unreadCount || 0;
};

const getIsSelectedNoteToSelf = (state: StateType): boolean => {
return getSelectedConversation(state)?.isMe || false;
};
Expand Down Expand Up @@ -302,6 +306,10 @@ export function useSelectedIsActive() {
return useSelector(getIsSelectedActive);
}

export function useSelectedUnreadCount() {
return useSelector(getSelectedUnreadCount);
}

export function useSelectedIsNoteToSelf() {
return useSelector(getIsSelectedNoteToSelf);
}
Expand Down
Loading