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