| 1 |
import { useLaunchDataStore } from '@auto-launch/state/launch-data'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { Spinner } from '@wordpress/components'; |
| 4 |
import { useState } from '@wordpress/element'; |
| 5 |
import { __, sprintf } from '@wordpress/i18n'; |
| 6 |
import { chevronRight, Icon } from '@wordpress/icons'; |
| 7 |
|
| 8 |
// A build-id deep link must resume its build after the restart cleanup. |
| 9 |
export const restartUrl = (href, buildId) => { |
| 10 |
if (!buildId) return null; |
| 11 |
const url = new URL(href); |
| 12 |
url.searchParams.set('build-id', buildId); |
| 13 |
return url.toString(); |
| 14 |
}; |
| 15 |
|
| 16 |
export const RestartLaunchModal = ({ pages }) => { |
| 17 |
const { resetSiteInformation } = window.extLaunchData; |
| 18 |
const { navigationIds, templatePartsIds, pageWithTitleTemplateId } = |
| 19 |
resetSiteInformation || {}; |
| 20 |
const globalStylesPostID = window.extSharedData.globalStylesPostID; |
| 21 |
|
| 22 |
const { reset: resetLaunchData } = useLaunchDataStore(); |
| 23 |
const [processing, setProcessing] = useState(false); |
| 24 |
const handleExit = () => { |
| 25 |
window.location.href = `${window.extSharedData.adminUrl}admin.php?page=extendify-assist`; |
| 26 |
}; |
| 27 |
|
| 28 |
const handleOk = async () => { |
| 29 |
setProcessing(true); |
| 30 |
const buildId = useLaunchDataStore.getState().urlParams?.['build-id']; |
| 31 |
resetLaunchData({ exclude: ['descriptionBackup'] }); |
| 32 |
// remove any workflow info |
| 33 |
localStorage.removeItem( |
| 34 |
`extendify-agent-workflows-${window.extSharedData.siteId}`, |
| 35 |
); |
| 36 |
for (const pageId of pages) { |
| 37 |
try { |
| 38 |
await apiFetch({ |
| 39 |
path: `/wp/v2/pages/${pageId}`, |
| 40 |
method: 'DELETE', |
| 41 |
}); |
| 42 |
} catch (responseError) { |
| 43 |
console.warn( |
| 44 |
`delete pages failed to delete a page (id: ${pageId}) with the following error`, |
| 45 |
responseError, |
| 46 |
); |
| 47 |
} |
| 48 |
} |
| 49 |
// They could be posts |
| 50 |
for (const pageId of pages) { |
| 51 |
try { |
| 52 |
await apiFetch({ |
| 53 |
path: `/wp/v2/posts/${pageId}`, |
| 54 |
method: 'DELETE', |
| 55 |
}); |
| 56 |
} catch (responseError) { |
| 57 |
console.warn( |
| 58 |
`delete posts failed to delete a page (id: ${pageId}) with the following error`, |
| 59 |
responseError, |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
// delete the wp_navigation posts created by Launch |
| 64 |
for (const navigationId of navigationIds || []) { |
| 65 |
try { |
| 66 |
await apiFetch({ |
| 67 |
path: `/wp/v2/navigation/${navigationId}`, |
| 68 |
method: 'DELETE', |
| 69 |
}); |
| 70 |
} catch (responseError) { |
| 71 |
console.warn( |
| 72 |
`delete navigation failed to delete a navigation (id: ${navigationId}) with the following error`, |
| 73 |
responseError, |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
for (const template of templatePartsIds || []) { |
| 79 |
try { |
| 80 |
await apiFetch({ |
| 81 |
path: `/wp/v2/template-parts/${template}?force=true`, |
| 82 |
method: 'DELETE', |
| 83 |
}); |
| 84 |
} catch (responseError) { |
| 85 |
console.warn( |
| 86 |
`delete template failed to delete template (id: ${template}) with the following error`, |
| 87 |
responseError, |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
try { |
| 93 |
if (pageWithTitleTemplateId) { |
| 94 |
await apiFetch({ |
| 95 |
path: `/wp/v2/templates/${pageWithTitleTemplateId}?force=true`, |
| 96 |
method: 'DELETE', |
| 97 |
}); |
| 98 |
} |
| 99 |
} catch (responseError) { |
| 100 |
console.warn('Failed to delete page-with-title template:', responseError); |
| 101 |
} |
| 102 |
|
| 103 |
// Reset global styles |
| 104 |
try { |
| 105 |
if (globalStylesPostID) { |
| 106 |
await apiFetch({ |
| 107 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 108 |
method: 'POST', |
| 109 |
body: JSON.stringify({ settings: {}, styles: {} }), |
| 110 |
}); |
| 111 |
} |
| 112 |
} catch (styleResetError) { |
| 113 |
console.warn( |
| 114 |
'Failed to reset global styles with the following error:', |
| 115 |
styleResetError, |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
const target = restartUrl(window.location.href, buildId); |
| 120 |
if (target) { |
| 121 |
window.location.href = target; |
| 122 |
return; |
| 123 |
} |
| 124 |
window.location.reload(); |
| 125 |
}; |
| 126 |
|
| 127 |
return ( |
| 128 |
<div className="flex w-full flex-col gap-6 p-6"> |
| 129 |
<h2 className="text-2xl leading-8 text-left text-gray-900 font-medium py-0 m-0"> |
| 130 |
{__('Start Over?', 'extendify-local')} |
| 131 |
</h2> |
| 132 |
|
| 133 |
<div className="flex flex-col gap-3"> |
| 134 |
<p className="text-base leading-6 text-left text-gray-900 m-0"> |
| 135 |
{__( |
| 136 |
'It looks like you have been here before. We need to clean up some things before we can continue.', |
| 137 |
'extendify-local', |
| 138 |
)} |
| 139 |
</p> |
| 140 |
<p className="text-base leading-6 text-left text-gray-900 font-medium m-0"> |
| 141 |
{sprintf( |
| 142 |
// translators: %3$s is the number of old pages |
| 143 |
__('%s pages/posts will be deleted.', 'extendify-local'), |
| 144 |
pages.length, |
| 145 |
)} |
| 146 |
</p> |
| 147 |
</div> |
| 148 |
|
| 149 |
<div className="flex justify-between items-center mt-2"> |
| 150 |
<Nav |
| 151 |
handleOk={handleOk} |
| 152 |
handleExit={handleExit} |
| 153 |
processing={processing} |
| 154 |
/> |
| 155 |
</div> |
| 156 |
</div> |
| 157 |
); |
| 158 |
}; |
| 159 |
|
| 160 |
const Nav = ({ handleOk, handleExit, processing }) => { |
| 161 |
return ( |
| 162 |
<> |
| 163 |
<button |
| 164 |
type="button" |
| 165 |
onClick={handleExit} |
| 166 |
disabled={processing} |
| 167 |
className="inline-flex items-center gap-2 rounded-full ring-1 ring-gray-800 px-3 py-2.5 text-sm leading-5 font-normal text-gray-800 transition-colors hover:bg-gray-600/5 disabled:opacity-40" |
| 168 |
> |
| 169 |
<span className="px-1">{__('Exit', 'extendify-local')}</span> |
| 170 |
</button> |
| 171 |
{processing ? ( |
| 172 |
<button |
| 173 |
type="button" |
| 174 |
disabled |
| 175 |
className="inline-flex items-center justify-center gap-2 rounded-full border-0 bg-design-main px-3 py-2 text-sm leading-5 font-normal text-design-text disabled:opacity-40" |
| 176 |
> |
| 177 |
<Spinner className="m-0" /> |
| 178 |
<span className="px-1">{__('Processing...', 'extendify-local')}</span> |
| 179 |
</button> |
| 180 |
) : ( |
| 181 |
<button |
| 182 |
type="button" |
| 183 |
onClick={handleOk} |
| 184 |
className="inline-flex items-center justify-center rounded-full border-0 bg-design-main px-3 py-2 text-sm leading-5 font-normal text-design-text group hover:opacity-90 transition-opacity" |
| 185 |
> |
| 186 |
<span className="px-1"> |
| 187 |
{__('Delete and start over', 'extendify-local')} |
| 188 |
</span> |
| 189 |
<Icon fill="currentColor" icon={chevronRight} size={24} /> |
| 190 |
</button> |
| 191 |
)} |
| 192 |
</> |
| 193 |
); |
| 194 |
}; |
| 195 |
|