-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try using mshots for fetching vertically oriented screenshots in the …
…design step in Gutenboarding
- Loading branch information
1 parent
f03416d
commit 541ab92
Showing
4 changed files
with
74 additions
and
8 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
59 changes: 59 additions & 0 deletions
59
client/landing/gutenboarding/components/mshots-image/index.tsx
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,59 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
/** | ||
* Style dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
interface Props { | ||
src: string; | ||
alt: string; | ||
} | ||
|
||
const MshotsImage: React.FunctionComponent< Props > = ( { src, alt } ) => { | ||
const [ resolvedUrl, setResolvedUrl ] = React.useState< string | undefined >(); | ||
const [ count, setCount ] = React.useState( 0 ); | ||
|
||
const mShotsEndpointUrl = src; | ||
|
||
React.useEffect( () => { | ||
async function fetchMshots() { | ||
const response = await window.fetch( mShotsEndpointUrl, { | ||
method: 'GET', | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
} ); | ||
|
||
if ( response.ok && response.headers.get( 'Content-Type' ) === 'image/jpeg' ) { | ||
try { | ||
const blob = await response.blob(); | ||
setResolvedUrl( window.URL.createObjectURL( blob ) ); | ||
} catch ( e ) { | ||
setResolvedUrl( mShotsEndpointUrl ); | ||
} | ||
} | ||
|
||
if ( response.status === 307 ) { | ||
const id = setTimeout( () => setCount( count + 1 ), 1000 ); | ||
return () => clearTimeout( id ); | ||
} | ||
} | ||
fetchMshots(); | ||
}, [ mShotsEndpointUrl, count ] ); | ||
|
||
return ( | ||
<div className="mshots-image"> | ||
{ ! resolvedUrl && <p>Loading...</p> } | ||
{ resolvedUrl && <img src={ src } alt={ alt } /> } | ||
</div> | ||
); | ||
}; | ||
|
||
export default MshotsImage; |
Empty file.
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