Skip to content

Commit

Permalink
refactor: change onLinkPreviewDismissed signature
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Aug 30, 2023
1 parent 44675df commit 7292244
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,8 @@ export type URLEnrichmentConfig = {
enrichURLForPreview?: boolean;
/** Custom function to identify URLs in a string and request OG data */
findURLFn?: (text: string) => string[];
/** Custom function to handle link preview dismissal */
onLinkPreviewDismissed?: (
linkPreview: LinkPreview,
setEnrichURLEnabled: Dispatch<React.SetStateAction<boolean>>,
) => void;
/** Custom function to react to link preview dismissal */
onLinkPreviewDismissed?: (linkPreview: LinkPreview) => void;
};
```

Expand Down
8 changes: 1 addition & 7 deletions docusaurus/docs/React/guides/customization/link-preview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ It can be customized how, the link previews are acquired and dismissed or how lo
By default, when a link preview is dismissed, it's state is set to `'dismissed'`. This behavior can be expanded by providing `onLinkPreviewDismissed` callback:

```typescript jsx
import { Dispatch } from 'react';
import {
Channel,
LinkPreview,
Expand All @@ -221,10 +220,7 @@ import {
VirtualizedMessageList as MessageList,
} from 'stream-chat-react';

const onLinkPreviewDismissed = (
linkPreview: LinkPreview,
setEnrichURLEnabled: Dispatch<React.SetStateAction<boolean>>
) => {
const onLinkPreviewDismissed = (linkPreview: LinkPreview) => {
// custom logic to invoke, when a given link preview is dismissed
}

Expand All @@ -241,8 +237,6 @@ const App = () => (
)
```

The `onLinkPreviewDismissed` receives a state change dispatcher, that allows us to disable the link previews and URL enrichment.

#### Custom text parsing function

If the default link parsing functionality is not sufficient, this can be overridden by providing `findURLFn` custom function. The requirement is that the function return an array of strings - links - that will be later used to scrape the data.
Expand Down
6 changes: 3 additions & 3 deletions src/components/MessageInput/__tests__/LinkPreviewList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ describe('Link preview', () => {
});

it('enables custom handling of dismissal', async () => {
const onPreviewDismissed = jest.fn();
const onLinkPreviewDismissed = jest.fn();
const typedText = `X ${scrapedData1.og_scrape_url}`;
jest
.spyOn(chatClient, 'enrichURL')
Expand All @@ -1211,7 +1211,7 @@ describe('Link preview', () => {
messageInputProps: {
urlEnrichmentConfig: {
enrichURLForPreview: true,
onPreviewDismissed,
onLinkPreviewDismissed,
},
},
});
Expand All @@ -1229,7 +1229,7 @@ describe('Link preview', () => {
fireEvent.click(await screen.findByTestId(LINK_PREVIEW_DISMISS_BTN_TEST_ID));
});

expect(onPreviewDismissed).toHaveBeenCalledTimes(1);
expect(onLinkPreviewDismissed).toHaveBeenCalledTimes(1);
});

it('are rendered in custom LinkPreviewList component', async () => {
Expand Down
14 changes: 5 additions & 9 deletions src/components/MessageInput/hooks/useLinkPreviews.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { find } from 'linkifyjs';
import { Dispatch, useCallback, useEffect, useState } from 'react';
import { Dispatch, useCallback, useEffect } from 'react';
import debounce from 'lodash.debounce';
import { useChannelStateContext, useChatContext } from '../../../context';
import type { MessageInputReducerAction, MessageInputState } from './useMessageInputState';
Expand All @@ -15,11 +15,8 @@ export type URLEnrichmentConfig = {
enrichURLForPreview?: boolean;
/** Custom function to identify URLs in a string and request OG data */
findURLFn?: (text: string) => string[];
/** Custom function to handle link preview dismissal */
onLinkPreviewDismissed?: (
linkPreview: LinkPreview,
setEnrichURLEnabled: Dispatch<React.SetStateAction<boolean>>,
) => void;
/** Custom function to react to link preview dismissal */
onLinkPreviewDismissed?: (linkPreview: LinkPreview) => void;
};

type UseEnrichURLsParams<
Expand Down Expand Up @@ -49,11 +46,10 @@ export const useLinkPreviews = <
const { client } = useChatContext();
// FIXME: the value of channelConfig is stale due to omitting it from the memoization deps in useCreateChannelStateContext
const { channelConfig } = useChannelStateContext();
const [enrichURLsEnabled, setEnrichURLsEnabled] = useState(enrichURLForPreview);

const dismissLinkPreview = useCallback(
(linkPreview: LinkPreview) => {
onLinkPreviewDismissed?.(linkPreview, setEnrichURLsEnabled);
onLinkPreviewDismissed?.(linkPreview);
const previewToRemoveMap = new Map();
linkPreview.state = LinkPreviewState.DISMISSED;
previewToRemoveMap.set(linkPreview.og_scrape_url, linkPreview);
Expand Down Expand Up @@ -148,6 +144,6 @@ export const useLinkPreviews = <
return {
dismissLinkPreview,
findAndEnqueueURLsToEnrich:
channelConfig?.url_enrichment && enrichURLsEnabled ? findAndEnqueueURLsToEnrich : undefined,
channelConfig?.url_enrichment && enrichURLForPreview ? findAndEnqueueURLsToEnrich : undefined,
};
};

0 comments on commit 7292244

Please sign in to comment.