Add fetch metadata provider (#103)

This commit is contained in:
Szymon Szlachtowicz 2021-10-29 11:08:21 +02:00 committed by GitHub
parent 08e7ea2ab0
commit b8b3d23741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 32 deletions

View File

@ -5,7 +5,6 @@ import styled from "styled-components";
import { useMessengerContext } from "../contexts/messengerProvider"; import { useMessengerContext } from "../contexts/messengerProvider";
import { useNarrow } from "../contexts/narrowProvider"; import { useNarrow } from "../contexts/narrowProvider";
import { ChannelData } from "../models/ChannelData"; import { ChannelData } from "../models/ChannelData";
import { Metadata } from "../models/Metadata";
import { uintToImgUrl } from "../utils/uintToImgUrl"; import { uintToImgUrl } from "../utils/uintToImgUrl";
import { Channels } from "./Channels/Channels"; import { Channels } from "./Channels/Channels";
@ -18,7 +17,6 @@ import { CommunitySkeleton } from "./Skeleton/CommunitySkeleton";
interface ChatProps { interface ChatProps {
communityKey: string; communityKey: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
identity: Identity; identity: Identity;
setActiveChannel: (channel: ChannelData) => void; setActiveChannel: (channel: ChannelData) => void;
activeChannel: ChannelData; activeChannel: ChannelData;
@ -26,7 +24,6 @@ interface ChatProps {
export function Chat({ export function Chat({
communityKey, communityKey,
fetchMetadata,
identity, identity,
setActiveChannel, setActiveChannel,
activeChannel, activeChannel,
@ -109,7 +106,6 @@ export function Chat({
community={communityData} community={communityData}
showCommunity={!showChannels} showCommunity={!showChannels}
onCommunityClick={showModal} onCommunityClick={showModal}
fetchMetadata={fetchMetadata}
channels={channels} channels={channels}
membersList={membersList} membersList={membersList}
setMembersList={setMembersList} setMembersList={setMembersList}

View File

@ -6,7 +6,6 @@ import { useMessengerContext } from "../../contexts/messengerProvider";
import { useNarrow } from "../../contexts/narrowProvider"; import { useNarrow } from "../../contexts/narrowProvider";
import { ChannelData } from "../../models/ChannelData"; import { ChannelData } from "../../models/ChannelData";
import { CommunityData } from "../../models/CommunityData"; import { CommunityData } from "../../models/CommunityData";
import { Metadata } from "../../models/Metadata";
import { Channel } from "../Channels/Channel"; import { Channel } from "../Channels/Channel";
import { EmptyChannel } from "../Channels/EmptyChannel"; import { EmptyChannel } from "../Channels/EmptyChannel";
import { Community } from "../Community"; import { Community } from "../Community";
@ -30,7 +29,6 @@ interface ChatBodyProps {
setActiveChannel: (val: ChannelData) => void; setActiveChannel: (val: ChannelData) => void;
activeChannelId: string; activeChannelId: string;
onCommunityClick: () => void; onCommunityClick: () => void;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
channels: ChannelData[]; channels: ChannelData[];
membersList: string[]; membersList: string[];
setMembersList: any; setMembersList: any;
@ -47,7 +45,6 @@ export function ChatBody({
setActiveChannel, setActiveChannel,
activeChannelId, activeChannelId,
onCommunityClick, onCommunityClick,
fetchMetadata,
channels, channels,
membersList, membersList,
setMembersList, setMembersList,
@ -125,7 +122,6 @@ export function ChatBody({
<ChatMessages <ChatMessages
messages={messages} messages={messages}
activeChannelId={activeChannelId} activeChannelId={activeChannelId}
fetchMetadata={fetchMetadata}
/> />
) : ( ) : (
<LoadingSkeleton /> <LoadingSkeleton />

View File

@ -2,10 +2,10 @@ import { decode } from "html-entities";
import React, { useEffect, useMemo, useState } from "react"; import React, { useEffect, useMemo, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { useFetchMetadata } from "../../contexts/fetchMetadataProvider";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { Metadata } from "../../models/Metadata"; import { Metadata } from "../../models/Metadata";
import { ImageMenu } from "../Form/ImageMenu"; import { ImageMenu } from "../Form/ImageMenu";
/* eslint-disable no-useless-escape */ /* eslint-disable no-useless-escape */
const regEx = const regEx =
/(?:(?:http|https):\/\/)?(?:[-a-z0-9]+\.)+[a-z]+(?::\d+)?(?:(?:\/[-\+~%/\.\w]+)?\/?(?:[&?][-\+=&;%@\.\w]+)?(?:#[\w-]+)?)?/gi; /(?:(?:http|https):\/\/)?(?:[-a-z0-9]+\.)+[a-z]+(?::\d+)?(?:(?:\/[-\+~%/\.\w]+)?\/?(?:[&?][-\+=&;%@\.\w]+)?(?:#[\w-]+)?)?/gi;
@ -13,17 +13,16 @@ const regEx =
type ChatMessageContentProps = { type ChatMessageContentProps = {
message: ChatMessage; message: ChatMessage;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
setImage: (image: string) => void; setImage: (image: string) => void;
setLinkOpen: (link: string) => void; setLinkOpen: (link: string) => void;
}; };
export function ChatMessageContent({ export function ChatMessageContent({
message, message,
fetchMetadata,
setImage, setImage,
setLinkOpen, setLinkOpen,
}: ChatMessageContentProps) { }: ChatMessageContentProps) {
const fetchMetadata = useFetchMetadata();
const { content, image } = useMemo(() => message, [message]); const { content, image } = useMemo(() => message, [message]);
const [elements, setElements] = useState<(string | React.ReactElement)[]>([ const [elements, setElements] = useState<(string | React.ReactElement)[]>([
content, content,

View File

@ -3,7 +3,6 @@ import styled from "styled-components";
import { useMessengerContext } from "../../contexts/messengerProvider"; import { useMessengerContext } from "../../contexts/messengerProvider";
import { ChatMessage } from "../../models/ChatMessage"; import { ChatMessage } from "../../models/ChatMessage";
import { Metadata } from "../../models/Metadata";
import { equalDate } from "../../utils"; import { equalDate } from "../../utils";
import { LoadingIcon } from "../Icons/LoadingIcon"; import { LoadingIcon } from "../Icons/LoadingIcon";
import { UserIcon } from "../Icons/UserIcon"; import { UserIcon } from "../Icons/UserIcon";
@ -16,14 +15,9 @@ import { ChatMessageContent } from "./ChatMessageContent";
type ChatMessagesProps = { type ChatMessagesProps = {
messages: ChatMessage[]; messages: ChatMessage[];
activeChannelId: string; activeChannelId: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
}; };
export function ChatMessages({ export function ChatMessages({ messages, activeChannelId }: ChatMessagesProps) {
messages,
activeChannelId,
fetchMetadata,
}: ChatMessagesProps) {
const { loadPrevDay, loadingMessages } = useMessengerContext(); const { loadPrevDay, loadingMessages } = useMessengerContext();
const [scrollOnBot, setScrollOnBot] = useState(true); const [scrollOnBot, setScrollOnBot] = useState(true);
const ref = useRef<HTMLHeadingElement>(null); const ref = useRef<HTMLHeadingElement>(null);
@ -108,7 +102,6 @@ export function ChatMessages({
<MessageText> <MessageText>
<ChatMessageContent <ChatMessageContent
message={message} message={message}
fetchMetadata={fetchMetadata}
setImage={setImage} setImage={setImage}
setLinkOpen={setLink} setLinkOpen={setLink}
/> />

View File

@ -3,17 +3,15 @@ import { Identity } from "status-communities/dist/cjs";
import { MessengerProvider } from "../contexts/messengerProvider"; import { MessengerProvider } from "../contexts/messengerProvider";
import { ChannelData } from "../models/ChannelData"; import { ChannelData } from "../models/ChannelData";
import { Metadata } from "../models/Metadata";
import { Chat } from "./Chat"; import { Chat } from "./Chat";
import { IdentityLoader } from "./Form/IdentityLoader"; import { IdentityLoader } from "./Form/IdentityLoader";
interface ChatLoaderProps { interface ChatLoaderProps {
communityKey: string; communityKey: string;
fetchMetadata?: (url: string) => Promise<Metadata | undefined>;
} }
export function ChatLoader({ communityKey, fetchMetadata }: ChatLoaderProps) { export function ChatLoader({ communityKey }: ChatLoaderProps) {
const [identity, setIdentity] = useState<Identity | undefined>(undefined); const [identity, setIdentity] = useState<Identity | undefined>(undefined);
const [activeChannel, setActiveChannel] = useState<ChannelData>({ const [activeChannel, setActiveChannel] = useState<ChannelData>({
id: "", id: "",
@ -32,7 +30,6 @@ export function ChatLoader({ communityKey, fetchMetadata }: ChatLoaderProps) {
communityKey={communityKey} communityKey={communityKey}
activeChannel={activeChannel} activeChannel={activeChannel}
setActiveChannel={setActiveChannel} setActiveChannel={setActiveChannel}
fetchMetadata={fetchMetadata}
identity={identity} identity={identity}
/> />
</MessengerProvider> </MessengerProvider>

View File

@ -2,6 +2,7 @@ import React, { useRef } from "react";
import { ThemeProvider } from "styled-components"; import { ThemeProvider } from "styled-components";
import styled from "styled-components"; import styled from "styled-components";
import { FetchMetadataProvider } from "../contexts/fetchMetadataProvider";
import { NarrowProvider } from "../contexts/narrowProvider"; import { NarrowProvider } from "../contexts/narrowProvider";
import { Metadata } from "../models/Metadata"; import { Metadata } from "../models/Metadata";
import { GlobalStyle } from "../styles/GlobalStyle"; import { GlobalStyle } from "../styles/GlobalStyle";
@ -24,13 +25,12 @@ export function ReactChat({
return ( return (
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<NarrowProvider myRef={ref}> <NarrowProvider myRef={ref}>
<FetchMetadataProvider fetchMetadata={fetchMetadata}>
<Wrapper ref={ref}> <Wrapper ref={ref}>
<GlobalStyle /> <GlobalStyle />
<ChatLoader <ChatLoader communityKey={communityKey} />
communityKey={communityKey}
fetchMetadata={fetchMetadata}
/>
</Wrapper> </Wrapper>
</FetchMetadataProvider>
</NarrowProvider> </NarrowProvider>
</ThemeProvider> </ThemeProvider>
); );

View File

@ -0,0 +1,23 @@
import React, { createContext, useContext } from "react";
const FetchMetadataContext = createContext<
((link: string) => Promise<any>) | undefined
>(undefined);
export function useFetchMetadata() {
return useContext(FetchMetadataContext);
}
interface FetchMetadataProviderProps {
children: React.ReactNode;
fetchMetadata?: (link: string) => Promise<any>;
}
export function FetchMetadataProvider({
children,
fetchMetadata,
}: FetchMetadataProviderProps) {
return (
<FetchMetadataContext.Provider value={fetchMetadata} children={children} />
);
}

View File

@ -19,7 +19,7 @@ export function useMessengerContext() {
return useContext(MessengerContext); return useContext(MessengerContext);
} }
interface NarrowProviderProps { interface MessengerProviderProps {
children: React.ReactNode; children: React.ReactNode;
activeChannel: string; activeChannel: string;
communityKey?: string; communityKey?: string;
@ -31,7 +31,7 @@ export function MessengerProvider({
activeChannel, activeChannel,
communityKey, communityKey,
identity, identity,
}: NarrowProviderProps) { }: MessengerProviderProps) {
const messenger = useMessenger(activeChannel, communityKey, identity); const messenger = useMessenger(activeChannel, communityKey, identity);
return <MessengerContext.Provider value={messenger} children={children} />; return <MessengerContext.Provider value={messenger} children={children} />;
} }