Skip to content

Commit

Permalink
fix: cleanup PR and merge copy pasted component
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilb committed Mar 22, 2024
1 parent b76ab7e commit 379ef29
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
9 changes: 4 additions & 5 deletions ts/components/SessionScrollButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';

const SessionScrollButtonDiv = styled.div`
Expand All @@ -17,11 +18,9 @@ const SessionScrollButtonDiv = styled.div`
}
`;

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

return (
<SessionScrollButtonDiv>
Expand All @@ -31,7 +30,7 @@ export const SessionScrollButton = (props: {
isHidden={!show}
onClick={props.onClickScrollBottom}
dataTestId="scroll-to-bottom-button"
unreadCount={props.unreadCount}
unreadCount={unreadCount}
/>
</SessionScrollButtonDiv>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class SessionMessagesListContainerInner extends React.Component<Props> {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onClickScrollBottom={this.props.scrollToNow}
key="scroll-down-button"
unreadCount={conversation.unreadCount || 0}
/>
</StyledMessagesContainer>
);
Expand Down
6 changes: 3 additions & 3 deletions ts/components/icon/SessionIconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +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 { SessionNotificationCount, SessionUnreadCount } from './SessionNotificationCount';
import { SessionIcon, SessionIconProps } from '.';
import { SessionNotificationCount, SessionUnreadCount } from './SessionNotificationCount';

interface SProps extends SessionIconProps {
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
Expand Down Expand Up @@ -60,7 +60,7 @@ const SessionIconButtonInner = React.forwardRef<HTMLDivElement, SProps>((props,
dataTestIdIcon,
style,
tabIndex,
unreadCount
unreadCount,
} = props;
const clickHandler = (e: React.MouseEvent<HTMLDivElement>) => {
if (props.onClick) {
Expand Down
66 changes: 34 additions & 32 deletions ts/components/icon/SessionNotificationCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import styled from 'styled-components';
type Props = {
count?: number;
};
const StyledCountContainer = styled.div<{ shouldRender: boolean; unreadCount?: number }>`
const StyledCountContainer = styled.div<{ centeredOnTop: boolean }>`
position: absolute;
font-size: 18px;
line-height: 1.2;
top: ${props => (props.unreadCount ? '-10px' : '27px')};
left: ${props => (props.unreadCount ? '50%' : '28px')};
transform: ${props => (props.unreadCount ? 'translateX(-50%)' : 'none')};
padding: ${props => (props.unreadCount ? '3px 3px' : '1px 4px')};
opacity: ${props => (props.shouldRender ? 1 : 0)};
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')};
display: flex;
align-items: center;
justify-content: center;
Expand All @@ -23,43 +22,46 @@ const StyledCountContainer = styled.div<{ shouldRender: boolean; unreadCount?: n
transition: var(--default-duration);
text-align: center;
color: var(--unread-messages-alert-text-color);
white-space: ${props => (props.unreadCount ? 'nowrap' : 'normal')};
white-space: ${props => (props.centeredOnTop ? 'nowrap' : 'normal')};
`;

const StyledCount = styled.div<{ unreadCount?: number }>`
const StyledCount = styled.div<{ centeredOnTop: boolean }>`
position: relative;
font-size: ${props => (props.unreadCount ? 'var(--font-size-xs)' : '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);

if (overflow) {
return (
<StyledCountContainer shouldRender={shouldRender}>
<StyledCount>
{99}
<span>+</span>
</StyledCount>
</StyledCountContainer>
);
}
const OverflowingAt = (props: { overflowingAt: number }) => {
return (
<StyledCountContainer shouldRender={shouldRender}>
<StyledCount>{count}</StyledCount>
</StyledCountContainer>
<>
{props.overflowingAt}
<span>+</span>
</>
);
};

export const SessionUnreadCount = (props: Props) => {
const { count } = props;
const shouldRender = Boolean(count && count > 0);
const NotificationOrUnreadCount = ({
centeredOnTop,
overflowingAt,
count,
}: Props & { overflowingAt: number; centeredOnTop: boolean }) => {
if (!count) {
return null;
}
const overflowing = count > overflowingAt;

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

export const SessionNotificationCount = (props: Props) => {
return <NotificationOrUnreadCount centeredOnTop={false} overflowingAt={99} count={props.count} />;
};

export const SessionUnreadCount = (props: Props) => {
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

0 comments on commit 379ef29

Please sign in to comment.