| import { mergeFileConfig } from 'librechat-data-provider'; |
| import { useCallback } from 'react'; |
| import { useGetFileConfig } from '~/data-provider'; |
| import { |
| resizeImage, |
| shouldResizeImage, |
| supportsClientResize, |
| type ResizeOptions, |
| type ResizeResult, |
| } from '~/utils/imageResize'; |
|
|
| |
| |
| |
| |
| export const useClientResize = () => { |
| const { data: fileConfig = null } = useGetFileConfig({ |
| select: (data) => mergeFileConfig(data), |
| }); |
|
|
| |
| |
| const config = (fileConfig as any)?.clientImageResize ?? { |
| enabled: false, |
| maxWidth: 1900, |
| maxHeight: 1900, |
| quality: 0.92, |
| }; |
| const isEnabled = config?.enabled ?? false; |
|
|
| |
| |
| |
| |
| |
| |
| const resizeImageIfNeeded = useCallback( |
| async ( |
| file: File, |
| options?: Partial<ResizeOptions>, |
| ): Promise<{ file: File; resized: boolean; result?: ResizeResult }> => { |
| |
| if (!isEnabled) { |
| return { file, resized: false }; |
| } |
|
|
| |
| if (!supportsClientResize()) { |
| console.warn('Client-side image resizing not supported in this browser'); |
| return { file, resized: false }; |
| } |
|
|
| |
| if (!shouldResizeImage(file)) { |
| return { file, resized: false }; |
| } |
|
|
| try { |
| const resizeOptions: Partial<ResizeOptions> = { |
| maxWidth: config?.maxWidth, |
| maxHeight: config?.maxHeight, |
| quality: config?.quality, |
| ...options, |
| }; |
|
|
| const result = await resizeImage(file, resizeOptions); |
| return { file: result.file, resized: true, result }; |
| } catch (error) { |
| console.warn('Client-side image resizing failed:', error); |
| return { file, resized: false }; |
| } |
| }, |
| [isEnabled, config], |
| ); |
|
|
| return { |
| isEnabled, |
| isSupported: supportsClientResize(), |
| config, |
| resizeImageIfNeeded, |
| }; |
| }; |
|
|
| export default useClientResize; |
|
|