generated from railmapgen/seed-project
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#285 Support importing project from RMG in upload view
- Loading branch information
Showing
7 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Modal, ModalContent, ModalOverlay } from '@chakra-ui/react'; | ||
import { useEffect, useState } from 'react'; | ||
import rmgRuntime from '@railmapgen/rmg-runtime'; | ||
import { useRootDispatch } from '../../redux'; | ||
|
||
const CHANNEL_PREFIX = 'rmg-bridge--'; | ||
|
||
interface RmgAppClipProps { | ||
templateId?: string; | ||
onClose: () => void; | ||
onImport: (param: Record<string, any>) => void; | ||
} | ||
|
||
export default function RmgParamAppClip(props: RmgAppClipProps) { | ||
const { templateId, onClose, onImport } = props; | ||
|
||
const dispatch = useRootDispatch(); | ||
|
||
const [appClipId] = useState(crypto.randomUUID()); | ||
const frameUrl = | ||
'/rmg/import?' + | ||
new URLSearchParams({ | ||
parentComponent: rmgRuntime.getAppName(), | ||
parentId: appClipId, | ||
}); | ||
|
||
useEffect(() => { | ||
const channel = new BroadcastChannel(CHANNEL_PREFIX + appClipId); | ||
channel.onmessage = ev => { | ||
const { event, data } = ev.data; | ||
console.log('[rmg-templates] Received event from RMG app clip:', event); | ||
if (event === 'CLOSE') { | ||
onClose(); | ||
} else if (event === 'IMPORT') { | ||
onImport(data); | ||
} | ||
}; | ||
|
||
return () => { | ||
channel.close(); | ||
}; | ||
}, [templateId]); | ||
|
||
return ( | ||
<Modal isOpen={!!templateId} onClose={onClose}> | ||
<ModalOverlay /> | ||
<ModalContent h={500} maxH="70%"> | ||
<iframe src={frameUrl} loading="lazy" width="100%" height="100%" /> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters