-
Notifications
You must be signed in to change notification settings - Fork 374
Fix: Import & Export from Github causes reloading the playground even before accept this step. #1908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Import & Export from Github causes reloading the playground even before accept this step. #1908
Changes from all commits
50878bf
a62d862
b2a9515
ab78d44
2bceaf1
ccdb830
192b7f8
60ffe8a
b6a140d
71cc2e3
b5f3355
419deb7
02932bb
c06b09b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,35 @@ | ||
| import { signal } from '@preact/signals-react'; | ||
| import Modal, { defaultStyles } from '../../components/modal'; | ||
| import GitHubImportForm, { GitHubImportFormProps } from './form'; | ||
| import { usePlaygroundClient } from '../../lib/use-playground-client'; | ||
|
|
||
| const query = new URLSearchParams(window.location.search); | ||
| export const isGitHubModalOpen = signal(query.get('state') === 'github-import'); | ||
| import { setActiveModal } from '../../lib/state/redux/slice-ui'; | ||
| import { PlaygroundDispatch } from '../../lib/state/redux/store'; | ||
| import { useDispatch } from 'react-redux'; | ||
| import { useEffect } from 'react'; | ||
|
|
||
| interface GithubImportModalProps { | ||
| defaultOpen?: boolean; | ||
| onImported?: GitHubImportFormProps['onImported']; | ||
| } | ||
| export function closeModal() { | ||
| isGitHubModalOpen.value = false; | ||
| // Remove ?state=github-import from the URL. | ||
| const url = new URL(window.location.href); | ||
| url.searchParams.delete('state'); | ||
| window.history.replaceState({}, '', url.href); | ||
| } | ||
| export function openModal() { | ||
| isGitHubModalOpen.value = true; | ||
| // Add a ?state=github-import to the URL so that the user can refresh the page | ||
| // and still see the modal. | ||
| const url = new URL(window.location.href); | ||
| url.searchParams.set('state', 'github-import'); | ||
| window.history.replaceState({}, '', url.href); | ||
| } | ||
| export function GithubImportModal({ onImported }: GithubImportModalProps) { | ||
| export function GithubImportModal({ defaultOpen, onImported }: GithubImportModalProps) { | ||
| const dispatch: PlaygroundDispatch = useDispatch(); | ||
| const playground = usePlaygroundClient(); | ||
|
|
||
| useEffect(() => { | ||
| const url = new URL(window.location.href); | ||
| url.searchParams.set('modal', 'github-import'); | ||
| window.history.replaceState({}, '', url.href); | ||
| }, []); | ||
|
|
||
| const closeModal = () => { | ||
| dispatch(setActiveModal(null)); | ||
| } | ||
| return ( | ||
| <Modal | ||
| style={{ | ||
| ...defaultStyles, | ||
| content: { ...defaultStyles.content, width: 600 }, | ||
| }} | ||
| isOpen={isGitHubModalOpen.value} | ||
| isOpen | ||
| onRequestClose={closeModal} | ||
| > | ||
| <GitHubImportForm | ||
|
|
@@ -44,8 +41,8 @@ export function GithubImportModal({ onImported }: GithubImportModalProps) { | |
| alert( | ||
| 'Import finished! Your Playground site has been updated.' | ||
| ); | ||
| closeModal(); | ||
| onImported?.(details); | ||
| closeModal(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a good change because we'll have a chance to notice the modal remaining open if an error is thrown. |
||
| }} | ||
| /> | ||
| </Modal> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| export const usePrevious = <T>(value: T): T | undefined => { | ||
| const ref = useRef<T>(); | ||
| useEffect(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a fun use of |
||
| ref.current = value; | ||
| }); | ||
| return ref.current; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice