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

Fix smooth scrolling + add scroll on reply click #1326

Merged
merged 2 commits into from
Apr 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const NotificationListPresenter = ({ unseen, seen }: INotificationList) => {
<WindowedList
width={370}
data={listData}
rowRenderer={(item, index) => {
itemContent={(index, item) => {
if (item.type === 'title') {
const title = item.data as string;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const NotificationListPresenter = ({ unseen, seen }: INotificationList) => {
<WindowedList
width={370}
data={listData}
rowRenderer={(item, index) => {
itemContent={(index, item) => {
if (item.type === 'title') {
const title = item.data as string;
return (
Expand Down
8 changes: 4 additions & 4 deletions app/src/renderer/apps/Courier/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ChatMessageProps = {
ourColor: string;
isPrevGrouped: boolean;
isNextGrouped: boolean;
measure: () => void;
onReplyClick?: (msgId: string) => void;
};

export const ChatMessagePresenter = ({
Expand All @@ -26,7 +26,7 @@ export const ChatMessagePresenter = ({
ourColor,
isPrevGrouped,
isNextGrouped,
measure,
onReplyClick,
}: ChatMessageProps) => {
const { ship, friends, theme } = useServices();
const { selectedChat } = useChatStore();
Expand Down Expand Up @@ -195,7 +195,7 @@ export const ChatMessagePresenter = ({

return (
<Bubble
ref={messageRef}
innerRef={messageRef}
id={messageRowId}
isPrevGrouped={isPrevGrouped}
isNextGrouped={isNextGrouped}
Expand All @@ -213,9 +213,9 @@ export const ChatMessagePresenter = ({
authorColor={authorColor}
message={mergedContents}
sentAt={sentAt}
onMeasure={measure}
reactions={reactionList}
onReaction={canReact ? onReaction : undefined}
onReplyClick={onReplyClick}
/>
);
};
Expand Down
67 changes: 5 additions & 62 deletions app/src/renderer/apps/Courier/views/ChatLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { observer } from 'mobx-react';
import styled from 'styled-components';
import { AnimatePresence } from 'framer-motion';
import {
Box,
Flex,
WindowedList,
Text,
Reply,
measureImage,
Expand All @@ -18,12 +16,11 @@ import { ChatInputBox } from '../components/ChatInputBox';
import { ChatLogHeader } from '../components/ChatLogHeader';
import { ChatAvatar } from '../components/ChatAvatar';
import { IuseStorage } from 'renderer/logic/lib/useStorage';
import { ChatMessage } from '../components/ChatMessage';
import { PinnedContainer } from '../components/PinnedMessage';
import { useServices } from 'renderer/logic/store';
import { ChatMessageType, ChatModelType } from '../models';
import { useAccountStore } from 'renderer/apps/Account/store';
import { displayDate } from 'os/lib/time';
import { ChatLogList } from './ChatLogList';

const FullWidthAnimatePresence = styled(AnimatePresence)`
width: 100%;
Expand Down Expand Up @@ -198,66 +195,12 @@ export const ChatLogPresenter = ({ storage }: ChatLogProps) => {
/>
</FullWidthAnimatePresence>
)}
<WindowedList
key={`${path}-${selectedChat.lastFetch}-${messages.length}`}
startAtBottom
hideScrollbar
<ChatLogList
messages={messages}
selectedChat={selectedChat}
width={containerWidth}
height={height}
data={messages}
rowRenderer={(row, index, measure) => {
const isLast = selectedChat
? index === messages.length - 1
: false;

const isNextGrouped =
index < messages.length - 1 &&
row.sender === messages[index + 1].sender;

const isPrevGrouped =
index > 0 &&
row.sender === messages[index - 1].sender &&
Object.keys(messages[index - 1].contents[0])[0] !==
'status';

const topSpacing = isPrevGrouped ? '3px' : 2;
const bottomSpacing = isNextGrouped ? '3px' : 2;

const thisMsgDate = new Date(row.createdAt).toDateString();
const prevMsgDate =
messages[index - 1] &&
new Date(messages[index - 1].createdAt).toDateString();
const showDate: boolean =
index === 0 || thisMsgDate !== prevMsgDate;
return (
<Box
mx="1px"
pt={topSpacing}
pb={isLast ? bottomSpacing : 0}
>
{showDate && (
<Text.Custom
opacity={0.5}
fontSize="12px"
fontWeight={500}
textAlign="center"
mt={2}
mb={2}
>
{displayDate(row.createdAt)}
</Text.Custom>
)}
<ChatMessage
isPrevGrouped={isPrevGrouped}
isNextGrouped={isNextGrouped}
containerWidth={containerWidth}
message={row as ChatMessageType}
ourColor={ourColor}
measure={measure}
/>
</Box>
);
}}
ourColor={ourColor}
/>
</Flex>
)}
Expand Down
105 changes: 105 additions & 0 deletions app/src/renderer/apps/Courier/views/ChatLogList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useRef } from 'react';
import {
Box,
Text,
WindowedList,
WindowedListRef,
} from '@holium/design-system';
import { displayDate } from 'os/lib/time';
import { ChatMessage } from '../components/ChatMessage';
import { ChatMessageType, ChatModelType } from '../models';

type Props = {
width: number;
height: number;
messages: ChatMessageType[];
selectedChat: ChatModelType;
ourColor: string;
};

export const ChatLogList = ({
width,
height,
messages,
selectedChat,
ourColor,
}: Props) => {
const listRef = useRef<WindowedListRef>(null);

const scrollbarWidth = 12;

const renderChatRow = (index: number, row: ChatMessageType) => {
const isLast = selectedChat ? index === messages.length - 1 : false;

const isNextGrouped =
index < messages.length - 1 && row.sender === messages[index + 1].sender;

const isPrevGrouped =
index > 0 &&
row.sender === messages[index - 1].sender &&
Object.keys(messages[index - 1].contents[0])[0] !== 'status';

const topSpacing = isPrevGrouped ? '3px' : 2;
const bottomSpacing = isNextGrouped ? '3px' : 2;

const thisMsgDate = new Date(row.createdAt).toDateString();
const prevMsgDate =
messages[index - 1] &&
new Date(messages[index - 1].createdAt).toDateString();
const showDate = index === 0 || thisMsgDate !== prevMsgDate;

return (
<Box
key={row.id}
mx="1px"
pt={topSpacing}
pb={isLast ? bottomSpacing : 0}
>
{showDate && (
<Text.Custom
opacity={0.5}
fontSize="12px"
fontWeight={500}
textAlign="center"
mt={2}
mb={2}
>
{displayDate(row.createdAt)}
</Text.Custom>
)}
<ChatMessage
isPrevGrouped={isPrevGrouped}
isNextGrouped={isNextGrouped}
containerWidth={width}
message={row as ChatMessageType}
ourColor={ourColor}
onReplyClick={(replyId) => {
const replyIndex = messages.findIndex((msg) => msg.id === replyId);
if (replyIndex === -1) return;

console.log('reply index', replyIndex);

listRef.current?.scrollToIndex({
index: replyIndex,
align: 'start',
behavior: 'smooth',
});
}}
/>
</Box>
);
};

return (
<WindowedList
innerRef={listRef}
data={messages}
followOutput="auto"
width={width + scrollbarWidth}
height={height}
style={{ marginRight: -scrollbarWidth }}
initialTopMostItemIndex={messages.length - 1}
itemContent={renderChatRow}
/>
);
};
6 changes: 2 additions & 4 deletions app/src/renderer/apps/Messages/DMs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ const DMsPresenter = (props: IProps) => {
key={lastTimeSent}
width={364}
height={544}
rowHeight={57}
data={previews}
filter={searchFilter}
rowRenderer={(dm, index) => (
data={previews.filter(searchFilter)}
itemContent={(index, dm) => (
<Box
display="block"
key={`dm-${index}-${dm.lastTimeSent}-${dm.pending}`}
Expand Down
5 changes: 2 additions & 3 deletions app/src/renderer/apps/Messages/components/ChatLogView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const ChatLogView = ({
<WindowedList
width={364}
data={messages}
rowRenderer={(message, index, measure) => (
initialTopMostItemIndex={messages.length - 1}
itemContent={(index, message) => (
<ChatMessage
isSending={message.pending}
// Only show author if it's a group
Expand All @@ -56,10 +57,8 @@ export const ChatLogView = ({
ourColor={ship.color || '#569BE2'}
contents={message.contents}
timeSent={message.timeSent}
onImageLoad={measure}
/>
)}
startAtBottom
/>
</Flex>
),
Expand Down
4 changes: 0 additions & 4 deletions app/src/renderer/apps/Messages/components/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface IProps {
ourColor: string;
contents: GraphDMType['contents'];
timeSent: number;
onImageLoad?: () => void;
}

export const ChatMessage = ({
Expand All @@ -29,7 +28,6 @@ export const ChatMessage = ({
isSending,
primaryBubble,
timeSent,
onImageLoad,
}: IProps) => {
const messageTypes = useMemo(
() =>
Expand Down Expand Up @@ -111,7 +109,6 @@ export const ChatMessage = ({
textColor={theme.textColor}
bgColor={referenceColor}
content={content}
onImageLoad={onImageLoad}
/>
);
})}
Expand All @@ -135,7 +132,6 @@ export const ChatMessage = ({
contents,
isMention,
isSending,
onImageLoad,
ourColor,
primaryBubble,
referenceColor,
Expand Down
6 changes: 2 additions & 4 deletions app/src/renderer/apps/Rooms/Room/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ const RoomChatPresenter = () => {
<WindowedList
width={354}
height={listHeight}
data={chats}
sort={(a, b) => a.timeReceived - b.timeReceived}
rowRenderer={(chat, index) => (
data={chats.sort((a, b) => a.timeReceived - b.timeReceived)}
itemContent={(index, chat) => (
<RoomChatMessage
key={chat.index}
chat={chat}
Expand All @@ -93,7 +92,6 @@ const RoomChatPresenter = () => {
}
/>
)}
startAtBottom
/>
);
}, [chats]);
Expand Down
3 changes: 2 additions & 1 deletion app/src/renderer/apps/Spaces/FeaturedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const FeaturedListPresenter = () => {
key={`featured-spaces-${listData.length}`}
width={354}
data={listData}
rowRenderer={(data: any) => {
itemContent={(_, data) => {
const onJoin = async () => {
setJoining(true);
SpacesActions.joinSpace(data.path.substring(1))
Expand Down Expand Up @@ -75,6 +75,7 @@ const FeaturedListPresenter = () => {
height="32px"
width="32px"
src={data.picture}
alt={data.name}
/>
) : (
<EmptyGroup color={data.color || '#000000'} />
Expand Down
6 changes: 2 additions & 4 deletions app/src/renderer/apps/Spaces/SpacesList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useMemo } from 'react';
import { observer } from 'mobx-react';
import { WindowedList } from '@holium/design-system';
import { SpaceModelType } from 'os/services/spaces/models/spaces';

import { Flex, Text, ActionButton, Icons } from 'renderer/components';
import { SpaceRow } from './SpaceRow';
import { ShellActions } from 'renderer/logic/actions/shell';
import { useServices } from 'renderer/logic/store';
import { VisaRow } from './components/VisaRow';
import { rgba } from 'polished';
import { WindowedList } from '@holium/design-system';

export interface Space {
color?: string;
Expand Down Expand Up @@ -108,11 +107,10 @@ const SpacesListPresenter = ({
return (
<Flex flex={1} width="100%">
<WindowedList
rowHeight={56}
key={`${spaces.length}-${incoming.length}`}
width={354}
data={rows}
rowRenderer={({ space, visa }) => {
itemContent={(_, { space, visa }) => {
if (space) {
return (
<SpaceRow
Expand Down
Loading