import { createElement, useState, useEffect } from '@wordpress/element' import { dateI18n } from '@wordpress/date' import { __, sprintf } from 'ct-i18n' import classnames from 'classnames' import { Transition, animated } from 'react-spring' const encodedStr = (rawStr) => rawStr.replace(/[\u00A0-\u9999<>\&]/g, (i) => '&#' + i.charCodeAt(0) + ';') let changelog_cache = null const parseChangelog = (changelog, { hasBetas } = {}) => encodedStr(changelog) .replace(/\r/g, '') .replace(/(\r\n|\r|\n){3,}/g, '$1\n\n') .split('\n\n') .map((versionDescriptor) => { let [version, date] = versionDescriptor.split(/\r?\n/)[0].split(':') return { version, date: dateI18n('F j, Y', new Date(date.trim())), descriptor: versionDescriptor, } }) .filter(({ version }) => hasBetas ? true : version.indexOf('beta') === -1 ) const SingleVersion = ({ versionDescriptor }) => { const [_, ...allReleaseChanges] = versionDescriptor.descriptor.split(/\r?\n/) return (

{sprintf( // translators: placeholder here means the actual version. __('Version: %s', 'blocksy'), versionDescriptor.version )} {sprintf( // translators: placeholder here means the actual date. __('Released on %s', 'blocksy'), versionDescriptor.date )}

  • ${allReleaseChanges .join('\n') .trim() .split('\n') .map((c) => c.replace(/^-\s/, '')) .map((c) => c.replace(/`(.*?)`/g, '$1') ) .map((c) => c.replace( /\[(.*?)\]\((.*?)\)/g, '$1' ) ) .join('
  • ')}
  • ` .replace( /New:/g, '' ) .replace( /Improvement:/g, '' ) .replace( /Fix:/g, '' ), }} />
    ) } export default () => { const [isLoading, setIsLoading] = useState(!changelog_cache) const [changelog, setChangelog] = useState(changelog_cache) const [currentChangelog, setCurrentChangelog] = useState(0) let hasBetas = false if ( window.ctDashboardLocalizations && window.ctDashboardLocalizations.plugin_data && window.ctDashboardLocalizations.plugin_data.has_beta_consent ) { hasBetas = window.ctDashboardLocalizations.plugin_data.has_beta_consent } const syncChangelog = async (verbose = false) => { if (verbose) { setIsLoading(true) } const body = new FormData() body.append('action', 'get_latest_changelog') try { const response = await fetch(ctDashboardLocalizations.ajax_url, { method: 'POST', body, }) if (response.status === 200) { const { success, data } = await response.json() if (success && data.changelog) { setChangelog(data.changelog) changelog_cache = data.changelog } } } catch (e) {} setIsLoading(false) } useEffect(() => { syncChangelog(!changelog_cache) }, []) return (
    { return { duration: 300, } }}> {(props, isLoading) => { if (isLoading) { return ( {__('Loading changelog...', 'blocksy')} ) } return (
    1, })}> {changelog && changelog.length > 1 && (
      {changelog.map(({ title }, index) => (
    • setCurrentChangelog(index) } key={title}> {title}
    • ))}
    )}
    • {' '} {__('New', 'blocksy')}
    • {' '} {__('Fix', 'blocksy')}
    • {__('Improvement', 'blocksy')}
    {changelog[currentChangelog].changelog ? parseChangelog( changelog[currentChangelog] .changelog, { hasBetas } ).map((versionDescriptor) => ( )) : __( 'No changelog present at the moment.', 'blocksy' )}
    ) }}
    ) }