2025-08-27 08:44:30 +02:00
import {
createElement ,
Component ,
useEffect ,
useState ,
useMemo ,
createContext ,
Fragment ,
} from '@wordpress/element'
import { _ _ } from 'ct-i18n'
import classnames from 'classnames'
import useActivationAction from '../helpers/useActivationAction'
import { Transition , animated } from 'blocksy-options'
import DemosList from './DemoInstall/DemosList'
import DemoToInstall from './DemoInstall/DemoToInstall'
import useDemoListFilters from './DemoInstall/filters/useDemoListFilters'
import SubmitSupport from '../helpers/SubmitSupport'
import NoLicense from '../NoLicense'
import { getStarterSitesStatus } from '../helpers/starter-sites'
export const DemosContext = createContext ( {
demos : [ ] ,
} )
let demos _cache = null
let demos _error _cache = null
const DemoInstall = ( { children , path , location } ) => {
const [ isLoading , setIsLoading ] = useState ( ! demos _cache )
const [ demos _list , setDemosList ] = useState ( demos _cache || [ ] )
const [ pluginsStatus , setPluginsStatus ] = useState (
ctDashboardLocalizations . plugin _data . active _plugins
)
const [ currentDemo , setCurrentDemo ] = useState ( null )
const [ currentlyInstalledDemo , setCurrentlyInstalledDemo ] = useState (
ctDashboardLocalizations . plugin _data . current _installed _demo
)
const filtersPayload = useDemoListFilters ( {
demos _list ,
} )
const [ forceEmptyDemos , setForceEmptyDemos ] = useState ( false )
const [ demo _error , setDemoError ] = useState ( {
isError : false ,
message : '' ,
// generic | remote_fetch_failed | ajax_request_failed
reason : 'generic' ,
} )
const [ demoConfiguration , setDemoConfiguration ] = useState ( {
builder : '' ,
} )
const [ installerBlockingReleased , setInstallerBlockingReleased ] =
useState ( false )
const syncDemos = async ( verbose = false ) => {
if ( verbose ) {
setIsLoading ( true )
}
try {
let demosResponse = await getStarterSitesStatus ( )
if ( demosResponse . status && demosResponse . status === 511 ) {
// This is a special case where the license is invalid
setForceEmptyDemos ( true )
return
}
const data = await demosResponse . json ( )
setDemosList ( data )
demos _cache = data
} catch ( response ) {
// Usually this is a network error or some policty blocking
// external frontend requests
console . error ( 'Blocksy:Dashboard:DemoInstall:demos_list' , {
response ,
reason : 'frontend_starter_sites_fetch_failed' ,
} )
}
setIsLoading ( false )
}
useEffect ( ( ) => {
if ( ctDashboardLocalizations . plugin _data . demo _install _error ) {
setIsLoading ( false )
setDemoError ( {
isError : true ,
message :
ctDashboardLocalizations . plugin _data . demo _install _error ,
reason : 'generic' ,
} )
} else {
syncDemos ( ! demos _cache )
}
} , [ ] )
if ( forceEmptyDemos ) {
return < NoLicense / >
}
return (
< div className = "ct-demos-list-container" >
< Transition
items = { isLoading }
from = { { opacity : 0 } }
enter = { [ { opacity : 1 } ] }
leave = { [ { opacity : 0 } ] }
2025-12-12 13:13:07 +01:00
initial = { null }
2025-08-27 08:44:30 +02:00
config = { ( key , phase ) => {
return phase === 'leave'
? {
duration : 300 ,
}
: {
duration : 300 ,
}
} } >
{ ( props , isLoading ) => {
if ( isLoading ) {
return (
< animated . p
style = { props }
className = "ct-loading-text" >
< svg
width = "16"
height = "16"
viewBox = "0 0 100 100" >
< g transform = "translate(50,50)" >
< g transform = "scale(1)" >
< circle
cx = "0"
cy = "0"
r = "50"
fill = "currentColor" > < / c i r c l e >
< circle
cx = "0"
cy = "-26"
r = "12"
fill = "#ffffff"
transform = "rotate(161.634)" >
< animateTransform
attributeName = "transform"
type = "rotate"
calcMode = "linear"
values = "0 0 0;360 0 0"
keyTimes = "0;1"
dur = "1s"
begin = "0s"
repeatCount = "indefinite" > < / a n i m a t e T r a n s f o r m >
< / c i r c l e >
< / g >
< / g >
< / s v g >
{ _ _ (
'Loading Starter Sites...' ,
'blocksy-companion'
) }
< / a n i m a t e d . p >
)
}
if ( demo _error . isError ) {
return (
< animated . div style = { props } >
{ demo _error . reason ===
'ajax_request_failed' && (
< div
className = "ct-demo-notification"
dangerouslySetInnerHTML = { {
_ _html : sprintf (
_ _ (
'Your site is misconfigured and AJAX requests are not reaching your backend. Please click %shere%s to find the common causes and possible solutions to this.<br> Error code - %s' ,
'blocksy-companion'
) ,
'<a href="https://creativethemes.com/blocksy/docs/troubleshooting/starter-site-common-issues-possible-solutions/" target="_blank">' ,
'</a>' ,
demo _error . message
) ,
} }
/ >
) }
{ demo _error . reason ===
'remote_fetch_failed' && (
< div
className = "ct-demo-notification"
dangerouslySetInnerHTML = { {
_ _html : sprintf (
_ _ (
'Failed to retrieve starter sites list.<br> Error code - %s' ,
'blocksy-companion'
) ,
demo _error . message
) ,
} }
/ >
) }
{ demo _error . reason === 'generic' && (
< div
className = "ct-demo-notification"
dangerouslySetInnerHTML = { {
_ _html : demo _error . message ,
} }
/ >
) }
< SubmitSupport / >
< / a n i m a t e d . d i v >
)
}
return (
< animated . div style = { props } >
< Fragment >
< DemosContext . Provider
value = { {
currentDemo ,
pluginsStatus ,
setPluginsStatus ,
installerBlockingReleased ,
setInstallerBlockingReleased ,
setCurrentDemo ,
currentlyInstalledDemo ,
setCurrentlyInstalledDemo ,
unfiltered _demos _list :
filtersPayload . unfiltered _demos _list ,
demos _list : filtersPayload . demos _list ,
filters : filtersPayload . filters ,
setFilters : filtersPayload . setFilters ,
allPlans : filtersPayload . allPlans ,
allCategories :
filtersPayload . allCategories ,
} } >
{ filtersPayload . display ( ) }
< DemosList / >
< DemoToInstall / >
< / D e m o s C o n t e x t . P r o v i d e r >
< SubmitSupport / >
< / F r a g m e n t >
< / a n i m a t e d . d i v >
)
} }
< / T r a n s i t i o n >
< / d i v >
)
}
export default DemoInstall