Line data Source code
1 : import { storeToRefs } from 'pinia'
2 :
3 : import { skipLeavePromptOnce } from '@/platform/workflow/management/leavePrompt'
4 : import { useDeploymentLabels } from '@/platform/workspace/composables/useDeploymentLabels'
5 : import { confirmUnsavedWorkBeforeSwitch } from '@/platform/workspace/confirmUnsavedWorkBeforeSwitch'
6 : import { reloadForDeploymentSwitch } from '@/platform/workspace/deploymentSwitchReload'
7 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
8 :
9 : /** What every deployment switch goes through before the page reloads. */
10 2009 : export function useDeploymentSwitchGuard() {
11 2009 : const store = useDeploymentPickStore()
12 2009 : const { deployments, isSwitching } = storeToRefs(store)
13 2009 : const { deploymentFullLabel, currentLabel } = useDeploymentLabels()
14 :
15 : /** The name the save-before-switch dialog gives the deployment. */
16 0 : function targetLabel(deploymentId: string | null): string {
17 0 : return deploymentFullLabel(
18 : deployments.value.find(
19 : (deployment) => deployment.deployment_id === deploymentId
20 : ) ?? null
21 : )
22 : }
23 :
24 : /** Starts a switch whose reload skips the leave prompt; a refusal keeps it. */
25 0 : async function withoutLeavePrompt(
26 : start: () => Promise<string | null>
27 : ): Promise<string | null> {
28 0 : const keepLeavePrompt = skipLeavePromptOnce()
29 0 : const refusal = await start()
30 0 : if (!isSwitching.value) keepLeavePrompt()
31 0 : return refusal
32 : }
33 :
34 : /** Reloads onto where this browser runs now, once unsaved work is settled. */
35 0 : async function reloadOntoCurrent(
36 : confirm: (
37 : deployment: string
38 : ) => Promise<boolean> = confirmUnsavedWorkBeforeSwitch
39 : ) {
40 0 : if (!(await confirm(currentLabel.value)) || isSwitching.value) return
41 0 : skipLeavePromptOnce()
42 0 : reloadForDeploymentSwitch(() => void store.load())
43 : }
44 :
45 2009 : return { targetLabel, withoutLeavePrompt, reloadOntoCurrent }
46 : }
|