Fix message loading and narrow notifications (#74)

This commit is contained in:
Szymon Szlachtowicz 2021-10-15 13:54:48 +02:00 committed by GitHub
parent 6bd351ed41
commit 1bc239fa71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 77 deletions

View File

@ -23,7 +23,6 @@ const fetchMetadata = async (link: string) => {
}; };
function DragDiv() { function DragDiv() {
console.log();
const [x, setX] = useState(0); const [x, setX] = useState(0);
const [y, setY] = useState(0); const [y, setY] = useState(0);
const [width, setWidth] = useState(window.innerWidth - 50); const [width, setWidth] = useState(window.innerWidth - 50);

View File

@ -3,28 +3,22 @@ import styled from "styled-components";
import { useNarrow } from "../contexts/narrowProvider"; import { useNarrow } from "../contexts/narrowProvider";
import { ChannelData, channels } from "../helpers/channelsMock"; import { ChannelData, channels } from "../helpers/channelsMock";
import { CommunityData } from "../helpers/communityMock";
import { Community } from "./Community";
import { MutedIcon } from "./Icons/MutedIcon"; import { MutedIcon } from "./Icons/MutedIcon";
import { textMediumStyles } from "./Text"; import { textMediumStyles } from "./Text";
interface ChannelsProps { interface ChannelsProps {
community: CommunityData;
notifications: { [id: string]: number }; notifications: { [id: string]: number };
clearNotifications: (id: string) => void; clearNotifications: (id: string) => void;
onCommunityClick: () => void; onCommunityClick: (val: ChannelData) => void;
setActiveChannel: (val: ChannelData) => void;
activeChannelId: number; activeChannelId: number;
} }
export function Channels({ export function Channels({
community,
notifications, notifications,
setActiveChannel, onCommunityClick,
clearNotifications, clearNotifications,
activeChannelId, activeChannelId,
onCommunityClick,
}: ChannelsProps) { }: ChannelsProps) {
useEffect(() => { useEffect(() => {
const channel = channels.find((channel) => channel.id === activeChannelId); const channel = channels.find((channel) => channel.id === activeChannelId);
@ -36,8 +30,6 @@ export function Channels({
}, [notifications, activeChannelId]); }, [notifications, activeChannelId]);
return ( return (
<ChannelsWrapper>
<StyledCommunity onClick={onCommunityClick} community={community} />
<ChannelList> <ChannelList>
{channels.map((channel) => ( {channels.map((channel) => (
<Channel <Channel
@ -51,12 +43,11 @@ export function Channels({
: undefined : undefined
} }
onClick={() => { onClick={() => {
setActiveChannel(channel); onCommunityClick(channel);
}} }}
/> />
))} ))}
</ChannelList> </ChannelList>
</ChannelsWrapper>
); );
} }
@ -127,21 +118,6 @@ export function Channel({
); );
} }
const ChannelsWrapper = styled.div`
width: 21%;
height: 100%;
min-width: 196px;
background-color: ${({ theme }) => theme.sectionBackgroundColor};
padding: 10px 0.6%;
display: flex;
flex-direction: column;
`;
const StyledCommunity = styled(Community)`
padding: 0 0 0 10px;
margin: 0 0 16px;
`;
const ChannelDescription = styled.p` const ChannelDescription = styled.p`
font-size: 12px; font-size: 12px;
line-height: 16px; line-height: 16px;

View File

@ -10,6 +10,7 @@ import { Theme } from "../styles/themes";
import { Channels } from "./Channels"; import { Channels } from "./Channels";
import { ChatBody } from "./Chat/ChatBody"; import { ChatBody } from "./Chat/ChatBody";
import { Community } from "./Community";
import { Members } from "./Members"; import { Members } from "./Members";
import { CommunityModal } from "./Modals/CommunityModal"; import { CommunityModal } from "./Modals/CommunityModal";
@ -44,14 +45,15 @@ export function Chat({ theme, community, fetchMetadata }: ChatProps) {
return ( return (
<ChatWrapper> <ChatWrapper>
{showChannels && !narrow && ( {showChannels && !narrow && (
<ChannelsWrapper>
<StyledCommunity onClick={showModal} community={community} />
<Channels <Channels
notifications={notifications} notifications={notifications}
clearNotifications={clearNotifications} clearNotifications={clearNotifications}
community={community} onCommunityClick={(e) => setActiveChannel(e)}
setActiveChannel={setActiveChannel}
activeChannelId={activeChannel.id} activeChannelId={activeChannel.id}
onCommunityClick={showModal}
/> />
</ChannelsWrapper>
)} )}
<ChatBody <ChatBody
theme={theme} theme={theme}
@ -70,6 +72,7 @@ export function Chat({ theme, community, fetchMetadata }: ChatProps) {
onCommunityClick={showModal} onCommunityClick={showModal}
fetchMetadata={fetchMetadata} fetchMetadata={fetchMetadata}
loadingMessages={loadingMessages} loadingMessages={loadingMessages}
clearNotifications={clearNotifications}
/> />
{showMembers && !narrow && ( {showMembers && !narrow && (
<Members community={community} setShowChannels={setShowChannels} /> <Members community={community} setShowChannels={setShowChannels} />
@ -92,3 +95,18 @@ const ChatWrapper = styled.div`
height: 100%; height: 100%;
display: flex; display: flex;
`; `;
const ChannelsWrapper = styled.div`
width: 21%;
height: 100%;
min-width: 196px;
background-color: ${({ theme }) => theme.sectionBackgroundColor};
padding: 10px 0.6%;
display: flex;
flex-direction: column;
`;
const StyledCommunity = styled(Community)`
padding: 0 0 0 10px;
margin: 0 0 16px;
`;

View File

@ -36,6 +36,7 @@ interface ChatBodyProps {
onCommunityClick: () => void; onCommunityClick: () => void;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>; fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
loadingMessages: boolean; loadingMessages: boolean;
clearNotifications: (id: string) => void;
} }
export function ChatBody({ export function ChatBody({
@ -55,6 +56,7 @@ export function ChatBody({
onCommunityClick, onCommunityClick,
fetchMetadata, fetchMetadata,
loadingMessages, loadingMessages,
clearNotifications,
}: ChatBodyProps) { }: ChatBodyProps) {
const narrow = useNarrow(); const narrow = useNarrow();
const [showChannelsList, setShowChannelsList] = useState(false); const [showChannelsList, setShowChannelsList] = useState(false);
@ -140,6 +142,7 @@ export function ChatBody({
setActiveChannel={setActiveChannel} setActiveChannel={setActiveChannel}
setShowChannels={setShowChannelsList} setShowChannels={setShowChannelsList}
activeChannelId={activeChannelId} activeChannelId={activeChannelId}
clearNotifications={clearNotifications}
/> />
)} )}
{showMembersList && narrow && ( {showMembersList && narrow && (

View File

@ -34,10 +34,14 @@ export function ChatMessages({
}, [messages, messages.length, scrollOnBot]); }, [messages, messages.length, scrollOnBot]);
useEffect(() => { useEffect(() => {
if ((ref?.current?.clientHeight ?? 0) < (ref?.current?.scrollHeight ?? 0)) { if (!loadingMessages) {
if (
(ref?.current?.clientHeight ?? 0) >= (ref?.current?.scrollHeight ?? 0)
) {
loadNextDay(); loadNextDay();
} }
}, [messages, messages.length]); }
}, [messages, messages.length, loadingMessages]);
useEffect(() => { useEffect(() => {
const setScroll = () => { const setScroll = () => {

View File

@ -1,8 +1,8 @@
import React from "react"; import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import { ChannelData, channels } from "../../helpers/channelsMock"; import { ChannelData } from "../../helpers/channelsMock";
import { Channel, ChannelList } from "../Channels"; import { Channels } from "../Channels";
import { NarrowTopbar } from "./NarrowTopbar"; import { NarrowTopbar } from "./NarrowTopbar";
@ -12,6 +12,7 @@ interface NarrowChannelsProps {
setActiveChannel: (val: ChannelData) => void; setActiveChannel: (val: ChannelData) => void;
activeChannelId: number; activeChannelId: number;
setShowChannels: (val: boolean) => void; setShowChannels: (val: boolean) => void;
clearNotifications: (id: string) => void;
} }
export function NarrowChannels({ export function NarrowChannels({
@ -20,29 +21,20 @@ export function NarrowChannels({
setActiveChannel, setActiveChannel,
activeChannelId, activeChannelId,
setShowChannels, setShowChannels,
clearNotifications,
}: NarrowChannelsProps) { }: NarrowChannelsProps) {
return ( return (
<ListWrapper> <ListWrapper>
<NarrowTopbar list="Channels" community={community} /> <NarrowTopbar list="Channels" community={community} />
<ChannelList> <Channels
{channels.map((channel) => ( notifications={notifications}
<Channel clearNotifications={clearNotifications}
key={channel.id} onCommunityClick={(e) => {
channel={channel} setActiveChannel(e);
isActive={channel.id === activeChannelId}
isMuted={channel.isMuted || false}
notification={
notifications[channel.name] > 0 && !channel.isMuted
? notifications[channel.name]
: undefined
}
onClick={() => {
setActiveChannel(channel);
setShowChannels(false); setShowChannels(false);
}} }}
activeChannelId={activeChannelId}
/> />
))}
</ChannelList>
</ListWrapper> </ListWrapper>
); );
} }