@@ -2,7 +2,8 @@ import { Typography } from '@core/Typography'
22import { Modal } from '@sections/Modal'
33import FriendlyCaptcha from '@templates/forms/FriendlyCaptcha'
44import { forwardRef , useCallback , useState } from 'react'
5- import { BsFiletypePdf , BsFiletypeXlsx } from 'react-icons/bs'
5+ //BsFiletypeDoc, BsFiletypeMov,
6+ import { BsFiletypeJpg , BsFiletypePdf , BsFiletypePng , BsFiletypeXls , BsFiletypeXlsx , BsFileZip } from 'react-icons/bs'
67import { useIntl } from 'react-intl'
78import { ArrowRight } from '../../icons'
89import envisTwMerge from '../../twMerge'
@@ -17,25 +18,19 @@ export type DownloadableLinkProps = {
1718} & Omit < ResourceLinkProps , 'variant' >
1819
1920const DownloadableLink = forwardRef < HTMLDivElement , DownloadableLinkProps > ( function DownloadableLink (
20- {
21- children,
22- fileName,
23- fileId,
24- label,
25- type = 'downloadableFile' ,
26- extension,
27- showExtensionIcon = true ,
28- ariaHideText,
29- variant = 'fit' ,
30- isAttachment = false ,
31- } ,
21+ { children, file, type = 'downloadableFile' , showExtensionIcon = true , variant = 'fit' , isAttachment = false } ,
3222 ref ,
3323) {
24+ const { label, originalFilename, url, extension } = file || { }
25+ const fileUrl = url
26+ ? `${ url . replace ( 'cdn.sanity.io' , 'cdn.equinor.com' ) } ?${ originalFilename . replace ( / / g, '-' ) } `
27+ : null
3428 const intl = useIntl ( )
3529 const [ showModal , setShowModal ] = useState ( false )
3630 const [ isFriendlyChallengeDone , setIsFriendlyChallengeDone ] = useState ( false )
37- const [ downloadRequestUrl , setDownloadRequestUrl ] = useState ( null )
38- const downloadLabel = label ?? ( Array . isArray ( children ) ? children ?. [ 0 ] : '' )
31+ const assetLabel = label ?? ( Array . isArray ( children ) ? children ?. [ 0 ] : originalFilename )
32+ const hasIcon = [ 'pdf' , 'png' , 'jpg' , 'xlsx' , 'xls' , 'zip' ]
33+ const openInNewTab = [ 'pdf' , 'png' , 'jpg' ]
3934
4035 const [ notHuman , setNotHuman ] = useState ( false )
4136
@@ -59,92 +54,45 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
5954 setShowModal ( false )
6055 }
6156
62- const getTranslation = ( ) => {
63- return intl . formatMessage ( {
64- id : 'downloadDocument' ,
65- defaultMessage : 'Download document' ,
66- } )
67- }
68-
69- const getContentElements = ( children ?: React . ReactNode ) => {
70- const textClassNames = `pt-1 grow leading-none`
71- switch ( type ) {
72- case 'downloadableFile' :
73- return extension &&
74- ( extension . toUpperCase ( ) === 'PDF' ||
75- extension . toUpperCase ( ) === 'XLS' ||
76- extension . toUpperCase ( ) === 'XLSX' ) &&
77- showExtensionIcon ? (
78- < >
79- { extension . toUpperCase ( ) === 'PDF' ? (
80- < BsFiletypePdf title = "pdf" size = { 24 } className = "mr-2 min-w-6 min-h-6" />
81- ) : (
82- < BsFiletypeXlsx title = "xlsx" size = { 24 } className = "mr-2 min-w-6 min-h-6" />
83- ) }
84- < span
85- className = { textClassNames }
86- { ...( ariaHideText && {
87- 'aria-hidden' : true ,
88- } ) }
89- >
90- { children }
91- </ span >
92- </ >
93- ) : (
94- < span
95- className = { textClassNames }
96- { ...( ariaHideText && {
97- 'aria-hidden' : true ,
98- } ) }
99- >
100- { children }
101- { ( extension && ! showExtensionIcon ) ||
102- ( extension &&
103- ( extension . toUpperCase ( ) !== 'PDF' ||
104- extension . toUpperCase ( ) !== 'XLS' ||
105- extension . toUpperCase ( ) !== 'XLSX' ) &&
106- showExtensionIcon ) ? (
107- < span > { `(${ extension . toUpperCase ( ) } )` } </ span >
108- ) : null }
109- </ span >
110- )
57+ const getExtensionIcon = ( ) => {
58+ const iconClassName = 'min-w-5'
59+ switch ( extension ?. toUpperCase ( ) ) {
60+ case 'PDF' :
61+ return < BsFiletypePdf title = "pdf" size = { 20 } className = { iconClassName } />
62+ case 'XLSX' :
63+ return < BsFiletypeXlsx title = "xlsx" size = { 20 } className = { iconClassName } />
64+ case 'PNG' :
65+ return < BsFiletypePng title = "png" size = { 20 } className = { iconClassName } />
66+ case 'JPG' :
67+ return < BsFiletypeJpg title = "jpg" size = { 20 } className = { iconClassName } />
68+ case 'XLS' :
69+ return < BsFiletypeXls title = "xls" size = { 20 } className = { iconClassName } />
70+ case 'ZIP' :
71+ return < BsFileZip title = "zip" size = { 20 } className = { iconClassName } />
11172 default :
112- return (
113- < span
114- className = { textClassNames }
115- { ...( ariaHideText && {
116- 'aria-hidden' : true ,
117- } ) }
118- >
119- { children }
120- { extension ? < span > { `(${ extension . toUpperCase ( ) } )` } </ span > : null }
121- </ span >
122- )
73+ return null
12374 }
12475 }
12576
126- const handleSuccessfullFriendlyChallenge = useCallback (
127- async ( event : any ) => {
128- const solution = event . detail . response
129- if ( fileId || fileName ) {
130- setIsFriendlyChallengeDone ( true )
131- const response = await fetch ( '/api/download/getFileUrl' , {
132- body : JSON . stringify ( {
133- fileName : fileName ,
134- fileId : fileId ,
135- frcCaptchaSolution : solution ,
136- } ) ,
137- headers : {
138- 'Content-Type' : 'application/json' ,
139- } ,
140- method : 'POST' ,
141- } )
142- const url = await response . json ( )
143- setDownloadRequestUrl ( url . url )
144- }
145- } ,
146- [ fileName , fileId ] ,
147- )
77+ const handleSuccessfullFriendlyChallenge = useCallback ( async ( event : any ) => {
78+ const solution = event . detail . response
79+ setIsFriendlyChallengeDone ( true )
80+ const response = await fetch ( '/api/download/getFileUrl' , {
81+ body : JSON . stringify ( {
82+ frcCaptchaSolution : solution ,
83+ } ) ,
84+ headers : {
85+ 'Content-Type' : 'application/json' ,
86+ } ,
87+ method : 'POST' ,
88+ } )
89+ console . log ( 'response' )
90+ if ( ! response . ok ) {
91+ setNotHuman ( true )
92+ }
93+ /* const url = await response.json()
94+ setDownloadRequestUrl(url.url) */
95+ } , [ ] )
14896
14997 const commonResourceLinkWrapperClassName = `
15098 group
@@ -163,6 +111,18 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
163111 no-underline
164112 ${ variantClassName [ variant ] } `
165113
114+ const linkElement = (
115+ < div className = "flex gap-1 items-baseline" >
116+ { showExtensionIcon && getExtensionIcon ( ) }
117+ < div className = "pt-1 leading-none" >
118+ { assetLabel }
119+ { extension && ( ! showExtensionIcon || ! hasIcon . includes ( extension ) ) ? (
120+ < span > { `(${ extension . toLowerCase ( ) } )` } </ span >
121+ ) : null }
122+ </ div >
123+ </ div >
124+ )
125+
166126 return (
167127 < div ref = { ref } >
168128 < button
@@ -179,9 +139,9 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
179139 w-fit
180140 underline-offset-2
181141 text-slate-80
182- text-sm`
142+ text-sm `
183143 }
184- title = { `${ getTranslation ( ) } : ${ fileName } ` }
144+ title = { `${ assetLabel } ` }
185145 aria-haspopup = "dialog"
186146 >
187147 < div
@@ -206,10 +166,9 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
206166 variant === 'stickyMenu'
207167 ? 'w-fit group-hover:underline no-underline leading-none align-middle items-end'
208168 : ''
209- }
210- ` }
169+ } `}
211170 >
212- { getContentElements ( < > { ` ${ downloadLabel } ` } </ > ) }
171+ { linkElement }
213172 </ div >
214173 { variant !== 'stickyMenu' && ! isAttachment && (
215174 < div className = { `flex flex-col px-1 translate-y-[1px]` } >
@@ -241,7 +200,7 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
241200 id : 'request_download_action_prefix' ,
242201 defaultMessage : 'Request' ,
243202 } ) }
244- { ` ${ downloadLabel } ` }
203+ { ` ${ assetLabel } ` }
245204 </ Typography >
246205 < Typography group = "plain" variant = "div" className = "mb-10" >
247206 { intl . formatMessage ( {
@@ -268,16 +227,15 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
268227 } ) }
269228 </ Typography >
270229 ) }
271- { downloadRequestUrl && isFriendlyChallengeDone && ! notHuman && (
230+ { isFriendlyChallengeDone && ! notHuman && (
272231 < BaseLink
273232 className = { envisTwMerge ( `${ commonResourceLinkWrapperClassName } ` , 'pt-20' ) }
274233 type = { type }
275- href = { downloadRequestUrl }
234+ href = { type === 'downloadableFile' ? fileUrl : url }
276235 { ...( extension &&
277- extension . toLowerCase ( ) === 'pdf' && {
236+ openInNewTab ?. includes ( extension . toLowerCase ( ) ) && {
278237 target : '_blank' ,
279238 } ) }
280- aria-description = { fileName }
281239 >
282240 < span
283241 className = { `h-full
@@ -288,8 +246,8 @@ const DownloadableLink = forwardRef<HTMLDivElement, DownloadableLinkProps>(funct
288246 gap-x-2
289247 ${ contentVariantClassName [ variant ] } ` }
290248 >
291- { getContentElements ( ` ${ downloadLabel } ` ) }
292- { getArrowElement ( extension && extension . toLowerCase ( ) === 'pdf' ? 'externalUrl' : type ) }
249+ { linkElement }
250+ { getArrowElement ( extension && openInNewTab ?. includes ( extension . toLowerCase ( ) ) ? 'externalUrl' : type ) }
293251 </ span >
294252
295253 < span className = "w-[0%] h-[1px] bg-grey-40 transition-all duration-300 group-hover:w-full" />
0 commit comments