|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {useCloseSessionBeforeUnloadMutation$variables} from '@/common/components/session/__generated__/useCloseSessionBeforeUnloadMutation.graphql'; |
|
|
import {sessionAtom} from '@/demo/atoms'; |
|
|
import useSettingsContext from '@/settings/useSettingsContext'; |
|
|
import {useAtomValue} from 'jotai'; |
|
|
import {useEffect, useMemo} from 'react'; |
|
|
import {ConcreteRequest, graphql} from 'relay-runtime'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function useCloseSessionBeforeUnload() { |
|
|
const session = useAtomValue(sessionAtom); |
|
|
const {settings} = useSettingsContext(); |
|
|
|
|
|
const data = useMemo(() => { |
|
|
if (session == null) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const graphQLTaggedNode = graphql` |
|
|
mutation useCloseSessionBeforeUnloadMutation($input: CloseSessionInput!) { |
|
|
closeSession(input: $input) { |
|
|
success |
|
|
} |
|
|
} |
|
|
` as ConcreteRequest; |
|
|
|
|
|
const variables: useCloseSessionBeforeUnloadMutation$variables = { |
|
|
input: { |
|
|
sessionId: session.id, |
|
|
}, |
|
|
}; |
|
|
|
|
|
const query = graphQLTaggedNode.params.text; |
|
|
if (query === null) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
return { |
|
|
query, |
|
|
variables, |
|
|
}; |
|
|
}, [session]); |
|
|
|
|
|
useEffect(() => { |
|
|
function onBeforeUpload() { |
|
|
if (data == null) { |
|
|
return; |
|
|
} |
|
|
|
|
|
fetch(`${settings.inferenceAPIEndpoint}/graphql`, { |
|
|
method: 'POST', |
|
|
credentials: 'include', |
|
|
headers: { |
|
|
'Content-Type': 'application/json', |
|
|
}, |
|
|
keepalive: true, |
|
|
body: JSON.stringify(data), |
|
|
}); |
|
|
} |
|
|
window.addEventListener('beforeunload', onBeforeUpload); |
|
|
return () => { |
|
|
window.removeEventListener('beforeunload', onBeforeUpload); |
|
|
}; |
|
|
}, [data, session, settings.inferenceAPIEndpoint]); |
|
|
} |
|
|
|