Skip to content

Commit

Permalink
Define SCROLL_BUFFER and apply it to the Channel&OpenChannel message …
Browse files Browse the repository at this point in the history
…list
  • Loading branch information
HoonBaek committed Mar 29, 2023
1 parent f24d056 commit 16d7c42
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/smart-components/Channel/components/MessageList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { isAboutSame } from '../../context/utils';
import { getMessagePartsInfo } from './getMessagePartsInfo';
import UnreadCount from '../UnreadCount';
import FrozenNotification from '../FrozenNotification';
import { MESSAGE_SCROLL_BUFFER } from '../../context/const';
import { SCROLL_BUFFER } from '../../../../utils/consts';

export interface MessageListProps {
className?: string;
Expand Down Expand Up @@ -77,7 +77,7 @@ const MessageList: React.FC<MessageListProps> = ({
});
}

if (isAboutSame(clientHeight + scrollTop, scrollHeight, MESSAGE_SCROLL_BUFFER)) {
if (isAboutSame(clientHeight + scrollTop, scrollHeight, SCROLL_BUFFER)) {
onScrollDownCallback(([messages]) => {
if (messages) {
try {
Expand All @@ -96,7 +96,7 @@ const MessageList: React.FC<MessageListProps> = ({
setScrollBottom(current.scrollHeight - current.scrollTop - current.offsetHeight)
}

if (!disableMarkAsRead && isAboutSame(clientHeight + scrollTop, scrollHeight, MESSAGE_SCROLL_BUFFER)) {
if (!disableMarkAsRead && isAboutSame(clientHeight + scrollTop, scrollHeight, SCROLL_BUFFER)) {
// Mark as read if scroll is at end
setTimeout(() => {
messagesDispatcher({
Expand Down Expand Up @@ -125,7 +125,7 @@ const MessageList: React.FC<MessageListProps> = ({
const current = scrollRef?.current;
if (current) {
const bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
if (scrollBottom < bottom && scrollBottom <= MESSAGE_SCROLL_BUFFER) {
if (scrollBottom < bottom && scrollBottom <= SCROLL_BUFFER) {
current.scrollTop += bottom - scrollBottom;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/smart-components/Channel/context/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const MESSAGE_SCROLL_BUFFER = 10;

export const PREV_RESULT_SIZE = 30;
export const NEXT_RESULT_SIZE = 15;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './openchannel-message-list.scss';

import React, { ReactElement, useRef, useState, useMemo, useLayoutEffect, useEffect } from 'react';
import { FileMessage, UserMessage } from '@sendbird/chat/message';
import isSameDay from 'date-fns/isSameDay';

import Icon, { IconTypes, IconColors } from '../../../../ui/Icon';
Expand All @@ -10,7 +11,7 @@ import { compareMessagesForGrouping } from '../../context/utils';
import { useOpenChannelContext } from '../../context/OpenChannelProvider';
import OpenChannelMessage from '../OpenChannelMessage';
import { RenderMessageProps } from '../../../../types';
import { FileMessage, UserMessage } from '@sendbird/chat/message';
import { SCROLL_BUFFER } from '../../../../utils/consts';

export type OpenchannelMessageListProps = {
renderMessage?: (props: RenderMessageProps) => React.ElementType<RenderMessageProps>;
Expand Down Expand Up @@ -53,7 +54,7 @@ function OpenchannelMessageList(
if (!hasMore) {
return;
}
if (scrollTop === 0) {
if (scrollTop < SCROLL_BUFFER) {
onScroll(() => {
// noop
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useOpenChannelListContext } from '../../context/OpenChannelListProvider
import OpenChannelListActionTypes from '../../context/dux/actionTypes';
import CreateOpenChannel from '../../../CreateOpenChannel';
import { LocalizationContext } from '../../../../lib/LocalizationContext';
import { SCROLL_BUFFER } from '../../../../utils/consts';

interface RenderOpenChannelPreviewProps {
channel: OpenChannel;
Expand Down Expand Up @@ -57,7 +58,7 @@ function OpenChannelListUI({
scrollHeight,
} = element;
const isAboutSame = (a, b, px) => (Math.abs(a - b) <= px);
if (isAboutSame(clientHeight + scrollTop, scrollHeight, 10)) {
if (isAboutSame(clientHeight + scrollTop, scrollHeight, SCROLL_BUFFER)) {
fetchNextChannels((messages) => {
if (messages) {
try {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/consts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const SCROLL_BUFFER = 10;

// voice message record
export const VOICE_RECORDER_CLICK_BUFFER_TIME = 250;
export const VOICE_RECORDER_DEFAULT_MIN = 1000;
Expand All @@ -24,3 +26,4 @@ export const VOICE_MESSAGE_MIME_TYPE = 'audio/mp3;sbu_type=voice';
export const META_ARRAY_VOICE_DURATION_KEY = 'KEY_VOICE_MESSAGE_DURATION';
export const META_ARRAY_MESSAGE_TYPE_KEY = 'KEY_INTERNAL_MESSAGE_TYPE';
export const META_ARRAY_MESSAGE_TYPE_VALUE__VOICE = 'voice/mp3';
``

Check failure on line 29 in src/utils/consts.ts

View workflow job for this annotation

GitHub Actions / build (16)

Expected an assignment or function call and instead saw an expression

0 comments on commit 16d7c42

Please sign in to comment.