|
| 1 | +<template> |
| 2 | + <Dialog |
| 3 | + :open="isOpen" |
| 4 | + class="inset-0 z-10 fixed overflow-y-auto" |
| 5 | + variant="bare" |
| 6 | + :initial-focus="container" |
| 7 | + @close="closeModal()" |
| 8 | + > |
| 9 | + <div class="flex min-h-screen items-center justify-center"> |
| 10 | + <DialogOverlay class="bg-gray-800 opacity-90 fixed sm:inset-0" /> |
| 11 | + <div ref="container" /> |
| 12 | + </div> |
| 13 | + </Dialog> |
| 14 | +</template> |
| 15 | + |
| 16 | +<script setup lang="ts"> |
| 17 | +import { Dialog, DialogOverlay } from '@headlessui/vue' |
| 18 | +import { init, loadRemote } from '@module-federation/runtime' |
| 19 | +import { ref, onMounted, onBeforeUnmount } from 'vue' |
| 20 | +import type { CyPromptAppDefaultShape, MoreInfoNeededModalContentsShape } from './prompt-app-types' |
| 21 | +import { usePromptStore } from '../store/prompt-store' |
| 22 | +
|
| 23 | +interface CyPromptApp { default: CyPromptAppDefaultShape } |
| 24 | +
|
| 25 | +// Mirrors the ReactDOM.Root type since incorporating those types |
| 26 | +// messes up vue typing elsewhere |
| 27 | +interface Root { |
| 28 | + render: (element: JSX.Element) => void |
| 29 | + unmount: () => void |
| 30 | +} |
| 31 | +
|
| 32 | +const emit = defineEmits<{ |
| 33 | + (e: 'close'): void |
| 34 | +}>() |
| 35 | +
|
| 36 | +withDefaults(defineProps<{ |
| 37 | + isOpen: boolean |
| 38 | +}>(), { |
| 39 | + isOpen: false, |
| 40 | +}) |
| 41 | +
|
| 42 | +const closeModal = () => { |
| 43 | + emit('close') |
| 44 | +} |
| 45 | +
|
| 46 | +const container = ref<HTMLDivElement | null>(null) |
| 47 | +const error = ref<string | null>(null) |
| 48 | +const ReactMoreInfoNeededModalContents = ref<MoreInfoNeededModalContentsShape | null>(null) |
| 49 | +const reactRoot = ref<Root | null>(null) |
| 50 | +const promptStore = usePromptStore() |
| 51 | +
|
| 52 | +const maybeRenderReactComponent = () => { |
| 53 | + if (!ReactMoreInfoNeededModalContents.value || !!error.value) { |
| 54 | + return |
| 55 | + } |
| 56 | +
|
| 57 | + const panel = window.UnifiedRunner.React.createElement(ReactMoreInfoNeededModalContents.value, { |
| 58 | + Cypress, |
| 59 | + testId: promptStore.currentMoreInfoNeededModalInfo?.testId, |
| 60 | + logId: promptStore.currentMoreInfoNeededModalInfo?.logId, |
| 61 | + eventManager: window.getEventManager(), |
| 62 | + onClose: () => { |
| 63 | + promptStore.currentMoreInfoNeededModalInfo?.onCancel() |
| 64 | + closeModal() |
| 65 | + }, |
| 66 | + }) |
| 67 | +
|
| 68 | + if (!reactRoot.value) { |
| 69 | + reactRoot.value = window.UnifiedRunner.ReactDOM.createRoot(container.value) |
| 70 | + } |
| 71 | +
|
| 72 | + reactRoot.value?.render(panel) |
| 73 | +} |
| 74 | +
|
| 75 | +const unmountReactComponent = () => { |
| 76 | + if (!ReactMoreInfoNeededModalContents.value || !container.value) { |
| 77 | + return |
| 78 | + } |
| 79 | +
|
| 80 | + reactRoot.value?.unmount() |
| 81 | +} |
| 82 | +
|
| 83 | +onMounted(maybeRenderReactComponent) |
| 84 | +onBeforeUnmount(unmountReactComponent) |
| 85 | +
|
| 86 | +init({ |
| 87 | + remotes: [{ |
| 88 | + alias: 'cy-prompt', |
| 89 | + type: 'module', |
| 90 | + name: 'cy-prompt', |
| 91 | + entryGlobalName: 'cy-prompt', |
| 92 | + entry: '/__cypress-cy-prompt/app/cy-prompt.js', |
| 93 | + shareScope: 'default', |
| 94 | + }], |
| 95 | + name: 'app', |
| 96 | + shared: { |
| 97 | + react: { |
| 98 | + scope: 'default', |
| 99 | + version: '18.3.1', |
| 100 | + lib: () => window.UnifiedRunner.React, |
| 101 | + shareConfig: { |
| 102 | + singleton: true, |
| 103 | + requiredVersion: '^18.3.1', |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | +}) |
| 108 | +
|
| 109 | +// We are not using any kind of loading state, because when we get |
| 110 | +// to this point, prompt should have already executed, which |
| 111 | +// means that the bundle has been downloaded |
| 112 | +loadRemote<CyPromptApp>('cy-prompt').then((module) => { |
| 113 | + if (!module?.default) { |
| 114 | + error.value = 'The panel was not loaded successfully' |
| 115 | +
|
| 116 | + return |
| 117 | + } |
| 118 | +
|
| 119 | + ReactMoreInfoNeededModalContents.value = module.default.MoreInfoNeededModalContents |
| 120 | + maybeRenderReactComponent() |
| 121 | +}).catch((e) => { |
| 122 | + error.value = e.message |
| 123 | +}) |
| 124 | +
|
| 125 | +</script> |
0 commit comments