1- import * as React from 'react' ;
1+ import { useCallback } from 'react' ;
22
33import {
44 type GalleryItemAction ,
@@ -13,6 +13,7 @@ import {useToaster} from '@gravity-ui/uikit';
1313import { i18n } from 'src/i18n/gallery' ;
1414
1515import { extensionRegex , supportedExtensions , supportedVideoExtensions } from './constants' ;
16+ import { buildLinkObject } from './helpers' ;
1617import type { FilesGalleryItemType , GalleryItemPropsWithUrl , UseFilesGalleryOptions } from './types' ;
1718
1819export function useFilesGallery (
@@ -28,40 +29,32 @@ export function useFilesGallery(
2829 const toaster = useToaster ( ) ;
2930
3031 return {
31- openFilesGallery : React . useCallback (
32+ openFilesGallery : useCallback (
3233 ( event : React . MouseEvent < HTMLDivElement > ) => {
3334 if ( ! ( event . target instanceof HTMLElement ) ) {
3435 return false ;
3536 }
3637
37- let fileLink = '' ;
38+ if ( event . target . tagName === 'IMG' && event . target . closest ( 'a' ) ) return false ;
3839
39- if ( event . target . tagName === 'IMG' && ! event . target . closest ( 'a' ) ) {
40- fileLink = event . target . getAttribute ( 'src' ) ?? '' ;
41- } else if ( event . target . tagName === 'A' ) {
42- fileLink = event . target . getAttribute ( 'href' ) ?? '' ;
43- }
40+ const targetFile = buildLinkObject ( event . target ) ;
4441
45- if ( ! fileLink ) {
46- return false ;
47- }
42+ if ( ! targetFile || ! targetFile . link ) return false ;
4843
4944 const filesFromContent = [
5045 ...( event . currentTarget ?. querySelectorAll ( 'img,a' ) ?? [ ] ) ,
5146 ] . reduce < GalleryItemPropsWithUrl [ ] > ( ( result , element ) => {
52- const isImage = element . tagName === 'IMG' ;
53- const link = isImage
54- ? element . getAttribute ( 'src' )
55- : element . getAttribute ( 'href' ) ;
47+ const linkObj = buildLinkObject ( element ) ;
5648
57- if ( link && ! customFiles ?. some ( ( item ) => item . url === link ) ) {
58- const extension = link . match ( extensionRegex ) ?. [ 0 ] || '' ;
49+ if ( linkObj ?. link && ! customFiles ?. some ( ( item ) => item . url === linkObj . link ) ) {
50+ const extension =
51+ linkObj . mimetype ?. match ( extensionRegex ) ?. [ 0 ] ||
52+ linkObj . link . match ( extensionRegex ) ?. [ 0 ] ||
53+ '' ;
5954
60- if ( isImage || supportedExtensions . includes ( extension ) ) {
61- const name =
62- ( isImage
63- ? element . getAttribute ( 'alt' )
64- : element . getAttribute ( 'title' ) ) || '' ;
55+ if ( linkObj . type === 'image' || supportedExtensions . includes ( extension ) ) {
56+ const link = linkObj . link ;
57+ const name = linkObj . name || '' ;
6558
6659 const filesGalleryItemType : FilesGalleryItemType =
6760 supportedVideoExtensions . includes ( extension ) ? 'video' : 'image' ;
@@ -127,7 +120,7 @@ export function useFilesGallery(
127120
128121 const files = [ ...( customFiles ?? [ ] ) , ...filesFromContent ] ;
129122
130- const initialItemIndex = files . findIndex ( ( item ) => item . url === fileLink ) ;
123+ const initialItemIndex = files . findIndex ( ( item ) => item . url === targetFile . link ) ;
131124
132125 if ( initialItemIndex !== - 1 ) {
133126 event . preventDefault ( ) ;
0 commit comments