// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. import { formatError, type GraphQLError } from "@probo/helpers"; import { useTranslate } from "@probo/i18n"; import { useToast } from "@probo/ui"; import { useEffect } from "react"; import { useMutation } from "react-relay"; import { useLocation, useSearchParams } from "react-router"; import { graphql } from "relay-runtime"; import type { useRequestAccessCallback_allMutation } from "./__generated__/useRequestAccessCallback_allMutation.graphql"; import type { useRequestAccessCallback_documentMutation } from "./__generated__/useRequestAccessCallback_documentMutation.graphql"; import type { useRequestAccessCallback_fileMutation } from "./__generated__/useRequestAccessCallback_fileMutation.graphql"; import type { useRequestAccessCallback_reportMutation } from "./__generated__/useRequestAccessCallback_reportMutation.graphql"; const documentMutation = graphql` mutation useRequestAccessCallback_documentMutation( $input: RequestDocumentAccessInput! ) { requestDocumentAccess(input: $input) { document { access { id status } } } } `; const reportMutation = graphql` mutation useRequestAccessCallback_reportMutation( $input: RequestReportAccessInput! ) { requestReportAccess(input: $input) { audit { reportFile { access { id status } } } } } `; const fileMutation = graphql` mutation useRequestAccessCallback_fileMutation( $input: RequestCompliancePortalFileAccessInput! ) { requestCompliancePortalFileAccess(input: $input) { file { access { id status } } } } `; const allMutation = graphql` mutation useRequestAccessCallback_allMutation { requestAllAccesses { compliancePortalAccess { id } } } `; function errorToastArgs(__: (s: string) => string, error: GraphQLError | GraphQLError[]) { return { title: __("Error"), description: formatError(__("Cannot request access"), error), variant: "error" as const, }; } function successToastArgs(__: (s: string) => string) { return { title: __("Success"), description: __("Access request submitted successfully."), variant: "success" as const, }; } export function useRequestAccessCallback() { const [searchParams, setSearchParams] = useSearchParams(); const location = useLocation(); const documentId = searchParams.get("request-document-id"); const reportId = searchParams.get("request-report-id"); const fileId = searchParams.get("request-file-id"); const all = searchParams.get("request-all"); const { __ } = useTranslate(); const { toast } = useToast(); const [requestDocumentAccess] = useMutation(documentMutation); const [requestReportAccess] = useMutation(reportMutation); const [requestFileAccess] = useMutation(fileMutation); const [requestAll] = useMutation(allMutation); useEffect(() => { if (documentId) { searchParams.delete("request-document-id"); void requestDocumentAccess({ variables: { input: { documentId }, }, onCompleted: (_, errors) => { if (errors?.length) { toast(errorToastArgs(__, errors)); return; } toast(successToastArgs(__)); }, onError: (error) => { toast(errorToastArgs(__, error)); }, }); setSearchParams(searchParams); } else if (reportId) { searchParams.delete("request-report-id"); void requestReportAccess({ variables: { input: { reportId }, }, onCompleted: (_, errors) => { if (errors?.length) { toast(errorToastArgs(__, errors)); return; } toast(successToastArgs(__)); }, onError: (error) => { toast(errorToastArgs(__, error)); }, }); setSearchParams(searchParams); } else if (fileId) { searchParams.delete("request-file-id"); void requestFileAccess({ variables: { input: { compliancePortalFileId: fileId }, }, onCompleted: (_, errors) => { if (errors?.length) { toast(errorToastArgs(__, errors)); return; } toast(successToastArgs(__)); }, onError: (error) => { toast(errorToastArgs(__, error)); }, }); setSearchParams(searchParams); } else if (all) { searchParams.delete("request-all"); void requestAll({ variables: {}, onCompleted: (_, errors) => { if (errors?.length) { toast(errorToastArgs(__, errors)); return; } toast(successToastArgs(__)); window.location.href = window.location.origin + location.pathname; }, onError: (error) => { toast(errorToastArgs(__, error)); }, }); setSearchParams(searchParams); } }, [ documentId, reportId, fileId, all, __, requestDocumentAccess, requestReportAccess, requestFileAccess, requestAll, searchParams, setSearchParams, toast, location, ]); }