import React, { FC, Fragment, useEffect, useState } from 'react' import { Link, Notice, Stack, Text } from '@wordpress/ui' import { getAdminLink } from '@woocommerce/settings' import { useI18n } from '@wordpress/react-i18n' import { ChangelogEntry, fetchChangelog, formatChangelogDate, } from '../model/changelog' import ChangelogTypeBadge from '../components/ChangelogTypeBadge' import RequiresUpdateBadge from '../components/RequiresUpdateBadge' import LandingSection from './LandingSection' import Skeleton from './Skeleton' import { pluginPageLink } from './pluginPage' import useAutoUpdates from '../model/autoUpdates' const CHANGES_SHOWN = 3 interface LatestChanges { changes: Array updateAvailable: boolean } const latestChangesOf = (entries: Array): LatestChanges => ({ changes: entries.slice(0, CHANGES_SHOWN), updateAvailable: entries.some(entry => !entry.installed), }) const useLatestChanges = (): 'loading' | LatestChanges => { const [latest, setLatest] = useState<'loading' | LatestChanges>('loading') useEffect(() => { fetchChangelog() .then(({ entries }) => setLatest(latestChangesOf(entries ?? []))) .catch(() => setLatest(latestChangesOf([]))) }, []) return latest } const UpdateAvailable: FC<{ canUpdatePlugins: boolean }> = ({ canUpdatePlugins, }) => { const { __ } = useI18n() return ( {__( 'A newer version of the plugin is available.', 'posten-bring-checkout' )} {!canUpdatePlugins && ` ${__('Ask your administrator to update it.', 'posten-bring-checkout')}`} {canUpdatePlugins && ( {__('Update the plugin', 'posten-bring-checkout')} )} ) } export declare interface WhatsNewProps { canUpdatePlugins: boolean } /** * Shows the latest changes to the plugin, and points out when some of them * need a newer version than the one installed. */ const WhatsNew: FC = ({ canUpdatePlugins }) => { const { __ } = useI18n() const latest = useLatestChanges() const { autoUpdates } = useAutoUpdates() // With automatic updates on, WordPress installs the new version by itself. // Until we know, say nothing rather than take it back a moment later. const askToUpdate = autoUpdates !== undefined && !autoUpdates.enabled const changelog = pluginPageLink('changelog') if (latest !== 'loading' && latest.changes.length === 0) { return null } return ( {latest === 'loading' ? ( ) : ( {latest.updateAvailable && askToUpdate && ( )} {latest.changes.map((change, index) => ( {index > 0 &&
} {formatChangelogDate(change.date)} {change.type && } {!change.installed && }

{change.title}

))} {__('See all updates', 'posten-bring-checkout')}
)}
) } export default WhatsNew