Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 23 additions & 15 deletions frontend/src/libs/components/card/card.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Icon } from '#libs/components/components.js';
import { type IconColor } from '#libs/enums/enums.js';
import { getValidClassNames } from '#libs/helpers/helpers.js';
import { useCallback } from '#libs/hooks/hooks.js';
import { forwardRef, useCallback } from '#libs/hooks/hooks.js';
import { type IconName, type ValueOf } from '#libs/types/types.js';

import styles from './styles.module.scss';
Expand All @@ -19,18 +19,24 @@ type Properties = {
onIconClick?: () => void;
};

const Card: React.FC<Properties> = ({
title,
imageUrl,
onClick,
isActive = false,
iconName,
iconRight,
iconWidth,
iconHeight,
onIconClick,
iconColor,
}) => {
const Card: React.ForwardRefRenderFunction<
HTMLDivElement | null,
Properties
> = (
{
title,
imageUrl,
onClick,
isActive = false,
iconName,
iconRight,
iconWidth,
iconHeight,
onIconClick,
iconColor,
},
reference,
) => {
const hasNoImageOrIcon = !imageUrl && !iconName;
const hasImage = Boolean(imageUrl);
const hasIcon = Boolean(iconName);
Expand All @@ -45,7 +51,7 @@ const Card: React.FC<Properties> = ({
);

return (
<div className={styles['container']}>
<div ref={reference} className={styles['container']}>
<button
className={getValidClassNames(
styles['item'],
Expand Down Expand Up @@ -97,4 +103,6 @@ const Card: React.FC<Properties> = ({
);
};

export { Card };
const ForwardedCard = forwardRef(Card);

export { ForwardedCard as Card };
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SidebarBody,
SidebarHeader,
} from '#libs/components/components.js';
import { AppRoute, IconColor } from '#libs/enums/enums.js';
import { AppRoute, DataStatus, IconColor } from '#libs/enums/enums.js';
import { getUrlWithQueryString } from '#libs/helpers/helpers.js';
import {
useAppDispatch,
Expand Down Expand Up @@ -41,11 +41,14 @@ const ChatSidebar: React.FC<Properties> = ({
const { id } = useParams<{ id: string }>();
const dispatch = useAppDispatch();
const [chatToDelete, setChatToDelete] = useState<null | number>(null);
const [isChatsLoaded, setIsChatsLoaded] = useState(false);
const dialogReference = useRef<HTMLDialogElement>(null);
const selectedChatReference = useRef<HTMLDivElement | null>(null);

const { chats } = useAppSelector(({ chats }) => {
const { chats, chatsDataStatus } = useAppSelector(({ chats }) => {
return {
chats: chats.chats,
chatsDataStatus: chats.chatsDataStatus,
};
});

Expand All @@ -57,6 +60,16 @@ const ChatSidebar: React.FC<Properties> = ({
void dispatch(chatsActions.getAllChats(filter));
}, [dispatch, filter]);

useEffect(() => {
if (chatsDataStatus === DataStatus.FULFILLED) {
setIsChatsLoaded(true);
}
}, [chatsDataStatus, setIsChatsLoaded]);
Comment on lines +63 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a state for this? Can't we simply check like this?

const isChatsLoaded = chatsDataStatus === DataStatus.FULFILLED

Copy link
Collaborator Author

@dmytro-kucherenko dmytro-kucherenko Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HIMDanny We can't. Scroll to the selected card should be only on first chats loading, because then data status will change on every search request

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now I see, thanks for clarification

Copy link
Collaborator

@HIMDanny HIMDanny Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, we also can use ref for this to not cause a rerender after setIsChatLoaded(true)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HIMDanny What do you mean by saying use ref?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, now I am not sure whether it will work)


if (selectedChatReference.current && !isChatsLoaded) {
selectedChatReference.current.scrollIntoView({ behavior: 'instant' });
}

const handleSelectChat = useCallback(() => {
onSetIsSidebarShow(false);
}, [onSetIsSidebarShow]);
Expand Down Expand Up @@ -109,6 +122,7 @@ const ChatSidebar: React.FC<Properties> = ({
to={getUrlWithQueryString(chatLink, { query: filter })}
>
<Card
ref={String(chat.id) === id ? selectedChatReference : null}
title={chat.name}
imageUrl={chat.imageUrl ?? cardPlaceholder}
onClick={handleSelectChat}
Expand Down