import React, { PropsWithChildren, useContext, useEffect, useState } from 'react'; import { getDisplayName } from '../utils/getDisplayName'; import type { Asset } from '../../native'; import type { UnknownType } from '../../types/types'; export type AttachmentPickerIconProps = { numberOfImageUploads: number; selectedPicker?: 'images'; }; export type AttachmentPickerContextValue = { /** * `bottomInset` determine the height of the `AttachmentPicker` and the underlying shift to the `MessageList` when it is opened. * This can also be set via the `setBottomInset` function provided by the `useAttachmentPickerContext` hook. * * Please check [OverlayProvider](https://github.com/GetStream/stream-chat-react-native/wiki/Cookbook-v3.0#overlayprovider) section in Cookbook * for more details. */ bottomInset: number; /** * Custom UI component for [camera selector icon](https://github.com/GetStream/stream-chat-react-native/blob/master/screenshots/docs/1.png) * * **Default: ** [CameraSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/master/src/components/AttachmentPicker/components/CameraSelectorIcon.tsx) */ CameraSelectorIcon: React.ComponentType; closePicker: () => void; /** * Custom UI component for [file selector icon](https://github.com/GetStream/stream-chat-react-native/blob/master/screenshots/docs/1.png) * * **Default: ** [FileSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/master/src/components/AttachmentPicker/components/FileSelectorIcon.tsx) */ FileSelectorIcon: React.ComponentType; /** * Custom UI component for [image selector icon](https://github.com/GetStream/stream-chat-react-native/blob/master/screenshots/docs/1.png) * * **Default: ** [ImageSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/master/src/components/AttachmentPicker/components/ImageSelectorIcon.tsx) */ ImageSelectorIcon: React.ComponentType; /** * Limit for maximum files that can be attached per message. */ maxNumberOfFiles: number; openPicker: () => void; selectedImages: Asset[]; setBottomInset: React.Dispatch>; setMaxNumberOfFiles: React.Dispatch>; setSelectedImages: React.Dispatch>; setSelectedPicker: React.Dispatch>; setTopInset: React.Dispatch>; topInset: number; attachmentPickerBottomSheetHeight?: number; attachmentSelectionBarHeight?: number; selectedPicker?: 'images'; }; export const AttachmentPickerContext = React.createContext( {} as AttachmentPickerContextValue, ); export const AttachmentPickerProvider = ({ children, value, }: PropsWithChildren<{ value?: Pick< AttachmentPickerContextValue, | 'attachmentSelectionBarHeight' | 'attachmentPickerBottomSheetHeight' | 'CameraSelectorIcon' | 'closePicker' | 'FileSelectorIcon' | 'ImageSelectorIcon' | 'openPicker' > & Partial>; }>) => { const bottomInsetValue = value?.bottomInset; const topInsetValue = value?.topInset; const [bottomInset, setBottomInset] = useState(bottomInsetValue ?? 0); const [maxNumberOfFiles, setMaxNumberOfFiles] = useState(10); const [selectedImages, setSelectedImages] = useState([]); const [selectedPicker, setSelectedPicker] = useState<'images'>(); const [topInset, setTopInset] = useState(value?.topInset ?? 0); useEffect(() => { setBottomInset(bottomInsetValue ?? 0); }, [bottomInsetValue]); useEffect(() => { setTopInset(topInsetValue ?? 0); }, [topInsetValue]); const combinedValue = { maxNumberOfFiles, selectedImages, selectedPicker, setBottomInset, setMaxNumberOfFiles, setSelectedImages, setSelectedPicker, setTopInset, ...value, bottomInset, topInset, }; return ( {children} ); }; export const useAttachmentPickerContext = () => useContext(AttachmentPickerContext) as unknown as AttachmentPickerContextValue; export const withAttachmentPickerContext =

( Component: React.ComponentType

, ): React.FC> => { const WithAttachmentPickerContextComponent = ( props: Omit, ) => { const attachmentPickerContext = useAttachmentPickerContext(); return ; }; WithAttachmentPickerContextComponent.displayName = `WithAttachmentPickerContext${getDisplayName( Component, )}`; return WithAttachmentPickerContextComponent; };