11import jwtDecode from 'jwt-decode' ;
22import React , { useEffect , useRef , useState } from 'react' ;
3+ import PropTypes from 'prop-types' ;
34import Screen from './screen' ;
45import ContentService from './service/contentService' ;
56import ConfigLoader from './config-loader' ;
@@ -13,10 +14,12 @@ import idFromPath from './id-from-path';
1314/**
1415 * App component.
1516 *
16- * @returns {object }
17- * The component.
17+ * @param {object } props The props.
18+ * @param {string } props.preview Enable preview mode.
19+ * @param {string } props.previewId Id of the item to preview.
20+ * @returns {JSX.Element } The component.
1821 */
19- function App ( ) {
22+ function App ( { preview , previewId } ) {
2023 const fallbackImageUrl = localStorage . getItem (
2124 localStorageKeys . FALLBACK_IMAGE
2225 ) ;
@@ -305,7 +308,7 @@ function App() {
305308 setDisplayFallback ( false ) ;
306309 } ;
307310
308- // ctrl/cmd i will log screen out and refresh
311+ // ctrl/cmd+ i will log screen out and refresh
309312 const handleKeyboard = ( { repeat, metaKey, ctrlKey, code } ) => {
310313 if ( ! repeat && ( metaKey || ctrlKey ) && code === 'KeyI' ) {
311314 localStorage . clear ( ) ;
@@ -314,41 +317,49 @@ function App() {
314317 } ;
315318
316319 useEffect ( ( ) => {
317- const currentUrl = new URL ( window . location . href ) ;
318-
319- // Make sure have releaseVersion and releaseTimestamp set in url parameters.
320- if (
321- ! currentUrl . searchParams . has ( 'releaseVersion' ) ||
322- ! currentUrl . searchParams . has ( 'releaseTimestamp' )
323- ) {
324- ReleaseLoader . loadConfig ( ) . then ( ( release ) => {
325- currentUrl . searchParams . set (
326- 'releaseTimestamp' ,
327- release . releaseTimestamp
328- ) ;
329- currentUrl . searchParams . set ( 'releaseVersion' , release . releaseVersion ) ;
320+ if ( preview !== null ) {
321+ document . addEventListener ( 'screen' , screenHandler ) ;
322+ document . addEventListener ( 'contentEmpty' , contentEmpty ) ;
323+ document . addEventListener ( 'contentNotEmpty' , contentNotEmpty ) ;
330324
331- window . history . replaceState ( null , '' , currentUrl ) ;
332- } ) ;
333- }
325+ startContent ( previewId ) ;
326+ } else {
327+ const currentUrl = new URL ( window . location . href ) ;
328+
329+ // Make sure have releaseVersion and releaseTimestamp set in url parameters.
330+ if (
331+ ! currentUrl . searchParams . has ( 'releaseVersion' ) ||
332+ ! currentUrl . searchParams . has ( 'releaseTimestamp' )
333+ ) {
334+ ReleaseLoader . loadConfig ( ) . then ( ( release ) => {
335+ currentUrl . searchParams . set (
336+ 'releaseTimestamp' ,
337+ release . releaseTimestamp
338+ ) ;
339+ currentUrl . searchParams . set ( 'releaseVersion' , release . releaseVersion ) ;
340+
341+ window . history . replaceState ( null , '' , currentUrl ) ;
342+ } ) ;
343+ }
334344
335- document . addEventListener ( 'screen' , screenHandler ) ;
336- document . addEventListener ( 'reauthenticate' , reauthenticateHandler ) ;
337- document . addEventListener ( 'contentEmpty' , contentEmpty ) ;
338- document . addEventListener ( 'contentNotEmpty' , contentNotEmpty ) ;
339- document . addEventListener ( 'keypress' , handleKeyboard ) ;
345+ document . addEventListener ( 'screen' , screenHandler ) ;
346+ document . addEventListener ( 'reauthenticate' , reauthenticateHandler ) ;
347+ document . addEventListener ( 'contentEmpty' , contentEmpty ) ;
348+ document . addEventListener ( 'contentNotEmpty' , contentNotEmpty ) ;
349+ document . addEventListener ( 'keypress' , handleKeyboard ) ;
340350
341- checkLogin ( ) ;
351+ checkLogin ( ) ;
342352
343- checkForUpdates ( ) ;
353+ checkForUpdates ( ) ;
344354
345- ConfigLoader . loadConfig ( ) . then ( ( config ) => {
346- releaseTimestampIntervalRef . current = setInterval (
347- checkForUpdates ,
348- config . releaseTimestampIntervalTimeout ??
349- releaseTimestampIntervalTimeoutDefault
350- ) ;
351- } ) ;
355+ ConfigLoader . loadConfig ( ) . then ( ( config ) => {
356+ releaseTimestampIntervalRef . current = setInterval (
357+ checkForUpdates ,
358+ config . releaseTimestampIntervalTimeout ??
359+ releaseTimestampIntervalTimeoutDefault
360+ ) ;
361+ } ) ;
362+ }
352363
353364 return function cleanup ( ) {
354365 Logger . log ( 'info' , 'Unmounting App.' ) ;
@@ -374,41 +385,45 @@ function App() {
374385 } , [ ] ) ;
375386
376387 useEffect ( ( ) => {
377- // Append screenId to current url for easier debugging. If errors are logged in the API's standard http log this
378- // makes it easy to see what screen client has made the http call by putting the screen id in the referer http
379- // header.
380- if ( screen && screen [ '@id' ] ) {
381- const url = new URL ( window . location . href ) ;
382- url . searchParams . set ( 'screenId' , idFromPath ( screen [ '@id' ] ) ) ;
383- window . history . replaceState ( null , '' , url ) ;
384- }
385-
386- ConfigLoader . loadConfig ( ) . then ( ( config ) => {
387- const token = localStorage . getItem ( localStorageKeys . API_TOKEN ) ;
388- const tenantKey = localStorage . getItem ( localStorageKeys . TENANT_KEY ) ;
389- const tenantId = localStorage . getItem ( localStorageKeys . TENANT_ID ) ;
390-
391- if ( token && tenantKey && tenantId ) {
392- // Get fallback image.
393- fetch ( `${ config . apiEndpoint } /v2/tenants/${ tenantId } ` , {
394- headers : {
395- authorization : `Bearer ${ token } ` ,
396- 'Authorization-Tenant-Key' : tenantKey ,
397- } ,
398- } )
399- . then ( ( response ) => response . json ( ) )
400- . then ( ( tenantData ) => {
401- if ( tenantData . fallbackImageUrl ) {
402- localStorage . setItem (
403- localStorageKeys . FALLBACK_IMAGE ,
404- tenantData . fallbackImageUrl
405- ) ;
406- }
407- } ) ;
388+ if ( preview === null ) {
389+ // Append screenId to current url for easier debugging. If errors are logged in the API's standard http log this
390+ // makes it easy to see what screen client has made the http call by putting the screen id in the referer http
391+ // header.
392+ if ( screen && screen [ '@id' ] ) {
393+ const url = new URL ( window . location . href ) ;
394+ url . searchParams . set ( 'screenId' , idFromPath ( screen [ '@id' ] ) ) ;
395+ window . history . replaceState ( null , '' , url ) ;
408396 }
409397
410- setDebug ( config ?. debug ?? false ) ;
411- } ) ;
398+ ConfigLoader . loadConfig ( ) . then ( ( config ) => {
399+ const token = localStorage . getItem ( localStorageKeys . API_TOKEN ) ;
400+ const tenantKey = localStorage . getItem ( localStorageKeys . TENANT_KEY ) ;
401+ const tenantId = localStorage . getItem ( localStorageKeys . TENANT_ID ) ;
402+
403+ if ( token && tenantKey && tenantId ) {
404+ // Get fallback image.
405+ fetch ( `${ config . apiEndpoint } /v2/tenants/${ tenantId } ` , {
406+ headers : {
407+ authorization : `Bearer ${ token } ` ,
408+ 'Authorization-Tenant-Key' : tenantKey ,
409+ } ,
410+ } )
411+ . then ( ( response ) => response . json ( ) )
412+ . then ( ( tenantData ) => {
413+ if ( tenantData . fallbackImageUrl ) {
414+ localStorage . setItem (
415+ localStorageKeys . FALLBACK_IMAGE ,
416+ tenantData . fallbackImageUrl
417+ ) ;
418+ }
419+ } ) ;
420+ }
421+
422+ setDebug ( config ?. debug ?? false ) ;
423+ } ) ;
424+ } else {
425+ setDebug ( false ) ;
426+ }
412427 } , [ screen ] ) ;
413428
414429 return (
@@ -430,4 +445,14 @@ function App() {
430445 ) ;
431446}
432447
448+ App . defaultProps = {
449+ preview : null ,
450+ previewId : null ,
451+ } ;
452+
453+ App . propTypes = {
454+ preview : PropTypes . string ,
455+ previewId : PropTypes . string ,
456+ } ;
457+
433458export default App ;
0 commit comments