Skip to content

Commit

Permalink
fix(j-s): Enable opening uploaded files on iOS (#17400)
Browse files Browse the repository at this point in the history
* Enable opening uploaded files on iOS

* Add SSR check

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
oddsson and kodiakhq[bot] authored Jan 6, 2025
1 parent c257544 commit f643adc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 8 additions & 2 deletions apps/judicial-system/web/src/utils/hooks/useFileList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@island.is/judicial-system-web/src/components'
import { CaseFileState } from '@island.is/judicial-system-web/src/graphql/schema'

import useIsMobile from '../useIsMobile/useIsMobile'
import { useGetSignedUrlLazyQuery } from './getSigendUrl.generated'
import { useLimitedAccessGetSignedUrlLazyQuery } from './limitedAccessGetSigendUrl.generated'

Expand All @@ -21,8 +22,13 @@ const useFileList = ({ caseId, connectedCaseParentId }: Parameters) => {
const { limitedAccess } = useContext(UserContext)
const { setWorkingCase } = useContext(FormContext)
const { formatMessage } = useIntl()
const isMobile = useIsMobile()
const [fileNotFound, setFileNotFound] = useState<boolean>()

const openFile = (url: string) => {
window.open(url, isMobile ? '_self' : '_blank', 'noopener, noreferrer')
}

const [
getSignedUrl,
{ error: fullAccessError, variables: fullAccessVariables },
Expand All @@ -31,7 +37,7 @@ const useFileList = ({ caseId, connectedCaseParentId }: Parameters) => {
errorPolicy: 'all',
onCompleted(data) {
if (data?.getSignedUrl?.url) {
window.open(data.getSignedUrl.url, '_blank')
openFile(data.getSignedUrl.url)
}
},
onError: () => {
Expand All @@ -47,7 +53,7 @@ const useFileList = ({ caseId, connectedCaseParentId }: Parameters) => {
errorPolicy: 'all',
onCompleted(data) {
if (data?.limitedAccessGetSignedUrl?.url) {
window.open(data.limitedAccessGetSignedUrl.url, '_blank')
openFile(data.limitedAccessGetSignedUrl.url)
}
},
onError: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect, useState } from 'react'

const useIsMobile = () => {
const [isMobile, setIsMobile] = useState(false)

useEffect(() => {
const checkMobile = () => {
// Check for SSR
if (!window) {
return false
}

const userAgent = navigator.userAgent
const mobileRegex =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
return mobileRegex.test(userAgent)
}

setIsMobile(checkMobile())
}, [])

return isMobile
}

export default useIsMobile

0 comments on commit f643adc

Please sign in to comment.