| 1 |
import { resetLaunchState } from '@auto-launch/functions/setup'; |
| 2 |
import { useLaunchDataStore } from '@auto-launch/state/launch-data'; |
| 3 |
import { launchStrings } from '@auto-launch/strings'; |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import { useState } from '@wordpress/element'; |
| 6 |
|
| 7 |
// A build-id deep link must resume its build after the restart cleanup. |
| 8 |
export const restartUrl = (href, buildId) => { |
| 9 |
if (!buildId) return null; |
| 10 |
const url = new URL(href); |
| 11 |
url.searchParams.set('build-id', buildId); |
| 12 |
return url.toString(); |
| 13 |
}; |
| 14 |
|
| 15 |
export const useRestartLaunch = ({ pages }) => { |
| 16 |
const { resetSiteInformation } = window.extLaunchData; |
| 17 |
const { navigationsIds, templatePartsIds, pageWithTitleTemplateId } = |
| 18 |
resetSiteInformation || {}; |
| 19 |
const globalStylesPostID = window.extSharedData.globalStylesPostID; |
| 20 |
|
| 21 |
const { reset: resetLaunchData } = useLaunchDataStore(); |
| 22 |
const [processing, setProcessing] = useState(false); |
| 23 |
|
| 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 |
try { |
| 32 |
await resetLaunchState(); |
| 33 |
} catch (resetError) { |
| 34 |
console.warn('Failed to reset launch state:', resetError); |
| 35 |
} |
| 36 |
resetLaunchData({ exclude: ['descriptionBackup'] }); |
| 37 |
localStorage.removeItem( |
| 38 |
`extendify-agent-workflows-${window.extSharedData.siteId}`, |
| 39 |
); |
| 40 |
for (const pageId of pages) { |
| 41 |
try { |
| 42 |
await apiFetch({ |
| 43 |
path: `/wp/v2/pages/${pageId}`, |
| 44 |
method: 'DELETE', |
| 45 |
}); |
| 46 |
} catch (responseError) { |
| 47 |
console.warn( |
| 48 |
`delete pages failed to delete a page (id: ${pageId}) with the following error`, |
| 49 |
responseError, |
| 50 |
); |
| 51 |
} |
| 52 |
} |
| 53 |
// They could be posts |
| 54 |
for (const pageId of pages) { |
| 55 |
try { |
| 56 |
await apiFetch({ |
| 57 |
path: `/wp/v2/posts/${pageId}`, |
| 58 |
method: 'DELETE', |
| 59 |
}); |
| 60 |
} catch (responseError) { |
| 61 |
console.warn( |
| 62 |
`delete posts failed to delete a page (id: ${pageId}) with the following error`, |
| 63 |
responseError, |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
for (const navigationId of navigationsIds || []) { |
| 68 |
try { |
| 69 |
await apiFetch({ |
| 70 |
path: `/wp/v2/navigation/${navigationId}`, |
| 71 |
method: 'DELETE', |
| 72 |
}); |
| 73 |
} catch (responseError) { |
| 74 |
console.warn( |
| 75 |
`delete navigation failed to delete a navigation (id: ${navigationId}) with the following error`, |
| 76 |
responseError, |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
for (const template of templatePartsIds || []) { |
| 82 |
try { |
| 83 |
await apiFetch({ |
| 84 |
path: `/wp/v2/template-parts/${template}?force=true`, |
| 85 |
method: 'DELETE', |
| 86 |
}); |
| 87 |
} catch (responseError) { |
| 88 |
console.warn( |
| 89 |
`delete template failed to delete template (id: ${template}) with the following error`, |
| 90 |
responseError, |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
try { |
| 96 |
if (pageWithTitleTemplateId) { |
| 97 |
await apiFetch({ |
| 98 |
path: `/wp/v2/templates/${pageWithTitleTemplateId}?force=true`, |
| 99 |
method: 'DELETE', |
| 100 |
}); |
| 101 |
} |
| 102 |
} catch (responseError) { |
| 103 |
console.warn('Failed to delete page-with-title template:', responseError); |
| 104 |
} |
| 105 |
|
| 106 |
try { |
| 107 |
if (globalStylesPostID) { |
| 108 |
await apiFetch({ |
| 109 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 110 |
method: 'POST', |
| 111 |
body: JSON.stringify({ settings: {}, styles: {} }), |
| 112 |
}); |
| 113 |
} |
| 114 |
} catch (styleResetError) { |
| 115 |
console.warn( |
| 116 |
'Failed to reset global styles with the following error:', |
| 117 |
styleResetError, |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
const target = restartUrl(window.location.href, buildId); |
| 122 |
if (target) { |
| 123 |
window.location.href = target; |
| 124 |
return; |
| 125 |
} |
| 126 |
window.location.reload(); |
| 127 |
}; |
| 128 |
|
| 129 |
const strings = launchStrings(); |
| 130 |
|
| 131 |
return { |
| 132 |
title: strings.restartTitle, |
| 133 |
body: strings.restartBody, |
| 134 |
note: strings.restartNote(pages.length), |
| 135 |
exit: { label: strings.restartExit, onClick: handleExit }, |
| 136 |
confirm: { |
| 137 |
label: strings.restartConfirm, |
| 138 |
processing, |
| 139 |
onClick: handleOk, |
| 140 |
}, |
| 141 |
}; |
| 142 |
}; |
| 143 |
|