diff --git a/apps/judicial-system/web/src/utils/hooks/useFileList/index.ts b/apps/judicial-system/web/src/utils/hooks/useFileList/index.ts index 0ab7be7464ed..5a668284561b 100644 --- a/apps/judicial-system/web/src/utils/hooks/useFileList/index.ts +++ b/apps/judicial-system/web/src/utils/hooks/useFileList/index.ts @@ -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' @@ -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() + const openFile = (url: string) => { + window.open(url, isMobile ? '_self' : '_blank', 'noopener, noreferrer') + } + const [ getSignedUrl, { error: fullAccessError, variables: fullAccessVariables }, @@ -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: () => { @@ -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: () => { diff --git a/apps/judicial-system/web/src/utils/hooks/useIsMobile/useIsMobile.ts b/apps/judicial-system/web/src/utils/hooks/useIsMobile/useIsMobile.ts new file mode 100644 index 000000000000..5afc2ea17f66 --- /dev/null +++ b/apps/judicial-system/web/src/utils/hooks/useIsMobile/useIsMobile.ts @@ -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