Skip to content

fix: extend dialogManagerId's with dictinct strings #2696

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

Open
wants to merge 2 commits into
base: release-v12
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/components/Dialog/DialogManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nanoid } from 'nanoid';
import { StateStore } from 'stream-chat';

export type GetOrCreateDialogParams = {
Expand Down Expand Up @@ -43,7 +44,7 @@ export class DialogManager {
});

constructor({ id }: DialogManagerOptions = {}) {
this.id = id ?? new Date().getTime().toString();
this.id = id ?? nanoid();
}

get openDialogCount() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Dialog/DialogPortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DialogPortalDestination = () => {
'--str-chat__dialog-overlay-height': openedDialogCount > 0 ? '100%' : '0',
} as React.CSSProperties
}
></div>
/>
);
};

Expand Down
7 changes: 3 additions & 4 deletions src/components/Dialog/__tests__/DialogsManager.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DialogManager } from '../DialogManager';
import * as nanoid from 'nanoid';

const dialogId = 'dialogId';

Expand All @@ -10,11 +11,9 @@ describe('DialogManager', () => {
});

it('initiates with default options', () => {
const mockedId = '12345';
const spy = jest.spyOn(Date.prototype, 'getTime').mockReturnValueOnce(mockedId);
jest.spyOn(nanoid, 'nanoid').mockReturnValue('mockedId');
const dialogManager = new DialogManager();
expect(dialogManager.id).toBe(mockedId);
spy.mockRestore();
expect(dialogManager.id).toBe('mockedId');
});

it('creates a new closed dialog', () => {
Expand Down
15 changes: 4 additions & 11 deletions src/components/MessageInput/AttachmentSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { nanoid } from 'nanoid';
import React, {
ElementRef,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import React, { ElementRef, useCallback, useEffect, useRef, useState } from 'react';
import { UploadIcon as DefaultUploadIcon } from './icons';
import { CHANNEL_CONTAINER_ID } from '../Channel/constants';
import { DialogAnchor, useDialog, useDialogIsOpen } from '../Dialog';
Expand All @@ -25,16 +17,17 @@ import {
AttachmentSelectorContextProvider,
useAttachmentSelectorContext,
} from '../../context/AttachmentSelectorContext';
import { useStableId } from '../UtilityComponents/useStableId';
import type { DefaultStreamChatGenerics } from '../../types';

export const SimpleAttachmentSelector = () => {
const {
AttachmentSelectorInitiationButtonContents,
FileUploadIcon = DefaultUploadIcon,
} = useComponentContext();
const inputRef = useRef<ElementRef<'input'>>(null);
const inputRef = useRef<HTMLInputElement | null>(null);
const [labelElement, setLabelElement] = useState<HTMLLabelElement | null>(null);
const id = useMemo(() => nanoid(), []);
const id = useStableId();

useEffect(() => {
if (!labelElement) return;
Expand Down
9 changes: 6 additions & 3 deletions src/components/MessageInput/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
} from '../../context/ComponentContext';
import { MessageInputContextProvider } from '../../context/MessageInputContext';
import { DialogManagerProvider } from '../../context';
import { useRegisterDropHandlers } from './WithDragAndDropUpload';
import { useStableId } from '../UtilityComponents/useStableId';

import type { Channel, Message, SendFileAPIResponse } from 'stream-chat';

Expand All @@ -26,7 +28,6 @@ import type {
} from '../../types/types';
import type { URLEnrichmentConfig } from './hooks/useLinkPreviews';
import type { CustomAudioRecordingConfig } from '../MediaRecorder';
import { useRegisterDropHandlers } from './WithDragAndDropUpload';

export type EmojiSearchIndexResult = {
id: string;
Expand Down Expand Up @@ -174,10 +175,12 @@ const UnMemoizedMessageInput = <
const { Input: ContextInput, TriggerProvider = DefaultTriggerProvider } =
useComponentContext<StreamChatGenerics, V>('MessageInput');

const id = useStableId();

const Input = PropInput || ContextInput || MessageInputFlat;
const dialogManagerId = props.isThreadInput
? 'message-input-dialog-manager-thread'
: 'message-input-dialog-manager';
? `message-input-dialog-manager-thread-${id}`
: `message-input-dialog-manager-${id}`;

if (dragAndDropWindow)
return (
Expand Down
9 changes: 6 additions & 3 deletions src/components/MessageList/MessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { LoadingIndicator as DefaultLoadingIndicator } from '../Loading';
import { defaultPinPermissions, MESSAGE_ACTIONS } from '../Message/utils';
import { TypingIndicator as DefaultTypingIndicator } from '../TypingIndicator';
import { MessageListMainPanel as DefaultMessageListMainPanel } from './MessageListMainPanel';

import { useStableId } from '../UtilityComponents/useStableId';
import { defaultRenderMessages, MessageRenderer } from './renderMessages';

import type { GroupStyle, ProcessMessagesParams } from './utils';
Expand Down Expand Up @@ -225,10 +225,13 @@ const MessageListWithContext = <
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [highlightedMessageId]);

const id = useStableId();

const showEmptyStateIndicator = elements.length === 0 && !threadList;
const dialogManagerId = threadList
? 'message-list-dialog-manager-thread'
: 'message-list-dialog-manager';
? `message-list-dialog-manager-thread-${id}`
: `message-list-dialog-manager-${id}`;

return (
<MessageListContextProvider value={{ listElement, scrollToBottom }}>
<MessageListMainPanel>
Expand Down
7 changes: 5 additions & 2 deletions src/components/MessageList/VirtualizedMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import type {
} from 'stream-chat';
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
import { DEFAULT_NEXT_CHANNEL_PAGE_SIZE } from '../../constants/limits';
import { useStableId } from '../UtilityComponents/useStableId';

type PropsDrilledToMessage =
| 'additionalMessageInputProps'
Expand Down Expand Up @@ -456,11 +457,13 @@ const VirtualizedMessageListWithContext = <
};
}, [highlightedMessageId, processedMessages]);

const id = useStableId();

if (!processedMessages) return null;

const dialogManagerId = threadList
? 'virtualized-message-list-dialog-manager-thread'
: 'virtualized-message-list-dialog-manager';
? `virtualized-message-list-dialog-manager-thread-${id}`
: `virtualized-message-list-dialog-manager-${id}`;

return (
<VirtualizedMessageListContextProvider value={{ scrollToBottom }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { act } from 'react';
import { cleanup, render } from '@testing-library/react';
import * as nanoid from 'nanoid';

import '@testing-library/jest-dom';

Expand Down Expand Up @@ -75,6 +76,8 @@ describe('VirtualizedMessageList', () => {

it('should render the list without any message', async () => {
const { channel, client } = await createChannel(true);
jest.spyOn(nanoid, 'nanoid').mockReturnValue('mockedId');

let result;
await act(() => {
result = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports[`VirtualizedMessageList should render the list without any message 1`] =
</div>
<div
class="str-chat__dialog-overlay"
data-str-chat__portal-id="virtualized-message-list-dialog-manager"
data-str-chat__portal-id="virtualized-message-list-dialog-manager-mockedId"
data-testid="str-chat__dialog-overlay"
style="--str-chat__dialog-overlay-height: 0;"
/>
Expand Down
13 changes: 13 additions & 0 deletions src/components/UtilityComponents/useStableId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { nanoid } from 'nanoid';
import { useMemo } from 'react';

/**
* The ID is generated using the `nanoid` library and is memoized to ensure
* that it remains the same across renders unless the key changes.
*/
export const useStableId = (key?: string) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
const id = useMemo(() => nanoid(), [key]);

return id;
};