-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External Media: Introduce external media modal
- Loading branch information
1 parent
d7fc5a3
commit 118bd1a
Showing
3 changed files
with
83 additions
and
9 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
75 changes: 74 additions & 1 deletion
75
projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-external-media-import.js
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 |
---|---|---|
@@ -1 +1,74 @@ | ||
console.log( 'Hello, Import Media Page' ); // eslint-disable-line no-console | ||
import { | ||
GooglePhotosMedia, | ||
OpenverseMedia, | ||
PexelsMedia, | ||
} from '@automattic/jetpack-shared-extension-utils'; | ||
import { useState, useEffect } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
|
||
const getExternalLibrary = slug => { | ||
switch ( slug ) { | ||
case 'google_photos': | ||
return GooglePhotosMedia; | ||
case 'openverse': | ||
return OpenverseMedia; | ||
case 'pexels': | ||
return PexelsMedia; | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
const WpcomExternalMediaImport = () => { | ||
const [ selectedSource, setSelectedSource ] = useState( null ); | ||
const ExternalLibrary = getExternalLibrary( selectedSource ); | ||
|
||
const handleSelect = media => { | ||
console.log( media ); // eslint-disable-line no-console | ||
}; | ||
|
||
const closeLibrary = event => { | ||
if ( event ) { | ||
event.stopPropagation(); | ||
|
||
// The DateTime picker is triggering a modal close when selected. We don't want this to close the modal | ||
if ( event.target.closest( '.jetpack-external-media-header__dropdown' ) ) { | ||
return; | ||
} | ||
} | ||
|
||
setSelectedSource( null ); | ||
}; | ||
|
||
useEffect( () => { | ||
const element = document.getElementById( 'wpcom-external-media-import' ); | ||
const handleClick = event => { | ||
const slug = event.target.dataset.slug; | ||
if ( slug ) { | ||
setSelectedSource( slug ); | ||
} | ||
}; | ||
|
||
if ( element ) { | ||
element.addEventListener( 'click', handleClick ); | ||
} | ||
|
||
return () => { | ||
if ( element ) { | ||
element.removeEventListener( 'click', handleClick ); | ||
} | ||
}; | ||
}, [] ); | ||
|
||
if ( ! ExternalLibrary ) { | ||
return null; | ||
} | ||
|
||
return <ExternalLibrary onSelect={ handleSelect } onClose={ closeLibrary } />; | ||
}; | ||
|
||
const container = document.getElementById( 'wpcom-external-media-import-modal' ); | ||
if ( container ) { | ||
const root = ReactDOM.createRoot( container ); | ||
root.render( <WpcomExternalMediaImport /> ); | ||
} |
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