| 1 |
import { useState, useEffect } from "@wordpress/element"; |
| 2 |
import { __ } from "@wordpress/i18n"; |
| 3 |
import { GeneratorSettingsPageArrow, SiteAvailabilityIcon, AdvancedIcon, ToolsIcon } from "../../../icons/icons"; |
| 4 |
|
| 5 |
import { SiteAvailability, Advanced, Tools } from "./SettingsOptions"; |
| 6 |
|
| 7 |
import "./setting-styles.scss"; |
| 8 |
|
| 9 |
const Settings = (props) => { |
| 10 |
|
| 11 |
const { setPage, initialTab } = props; |
| 12 |
const [settingsOptions, setSettingsOptions] = useState(sp_pcp_block_settings?.settings); |
| 13 |
|
| 14 |
const tabs = [ |
| 15 |
{ |
| 16 |
label: __("Site Availability", "post-carousel"), |
| 17 |
Icon: SiteAvailabilityIcon, |
| 18 |
value: "site-availability", |
| 19 |
Render: SiteAvailability, |
| 20 |
}, |
| 21 |
{ |
| 22 |
label: __("Advanced Controls", "post-carousel"), |
| 23 |
Icon: AdvancedIcon, |
| 24 |
value: "advanced", |
| 25 |
Render: Advanced, |
| 26 |
}, |
| 27 |
{ |
| 28 |
label: __("Tools", "post-carousel"), |
| 29 |
Icon: ToolsIcon, |
| 30 |
value: "tools", |
| 31 |
Render: Tools, |
| 32 |
}, |
| 33 |
]; |
| 34 |
|
| 35 |
// Get default tab from prop, hash or use 'site-availability' |
| 36 |
const getInitialTab = () => { |
| 37 |
if (initialTab) { |
| 38 |
return initialTab; |
| 39 |
} |
| 40 |
const hash = window.location.hash.replace("#", ""); |
| 41 |
const tabValue = hash.split("=")[1]; |
| 42 |
return tabValue || "site-availability"; |
| 43 |
}; |
| 44 |
|
| 45 |
const [settingTab, setSettingTab] = useState(getInitialTab()); |
| 46 |
|
| 47 |
// Update hash when tab changes |
| 48 |
useEffect(() => { |
| 49 |
window.location.hash = `#settings=${settingTab}`; |
| 50 |
}, [settingTab]); |
| 51 |
|
| 52 |
// Listen for hash changes (back button, direct URL) |
| 53 |
useEffect(() => { |
| 54 |
const handleHashChange = () => { |
| 55 |
const hash = window.location.hash.replace("#", ""); |
| 56 |
const tabValue = hash.split("=")[1]; |
| 57 |
if (tabValue) { |
| 58 |
setSettingTab(tabValue); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
window.addEventListener("hashchange", handleHashChange); |
| 63 |
return () => window.removeEventListener("hashchange", handleHashChange); |
| 64 |
}, []); |
| 65 |
|
| 66 |
return ( |
| 67 |
<> |
| 68 |
<section className="sp-pcp-settings-page-container"> |
| 69 |
<div className="sp-pcp-setting-tabs"> |
| 70 |
<ul> |
| 71 |
{tabs?.map(({ label, value, Icon }) => ( |
| 72 |
<li |
| 73 |
key={value} |
| 74 |
className={`sp-pcp-setting-tab ${settingTab === value ? "active" : ""}`} |
| 75 |
onClick={() => setSettingTab(value)} |
| 76 |
> |
| 77 |
<span className="sp-pcp-setting-icon"> |
| 78 |
<Icon /> |
| 79 |
</span>{" "} |
| 80 |
<span>{label}</span> |
| 81 |
</li> |
| 82 |
))} |
| 83 |
</ul> |
| 84 |
<div className="sp-pcp-setting-tabs-bottom"></div> |
| 85 |
</div> |
| 86 |
<div className="sp-pcp-setting-tab-content"> |
| 87 |
{tabs?.map(({ value, Render }) => |
| 88 |
settingTab === value ? (<> |
| 89 |
<Render |
| 90 |
key={value} |
| 91 |
settingsOptions={settingsOptions} |
| 92 |
setSettingsOptions={setSettingsOptions} |
| 93 |
setPage={setPage} |
| 94 |
/> |
| 95 |
</>) : null |
| 96 |
|
| 97 |
)} |
| 98 |
</div> |
| 99 |
</section> |
| 100 |
<a |
| 101 |
href={`${sp_pcp_block_settings?.homeUrl}wp-admin/edit.php?post_type=sp_post_carousel&page=pcp_settings`} |
| 102 |
rel="noopener noreferrer" |
| 103 |
className="sp-pcp-classic-settings-page-link" |
| 104 |
> |
| 105 |
Classic Shortcode Generator Setting |
| 106 |
<span className="sp-pcp-classic-settings-page-link-arrow"> |
| 107 |
<GeneratorSettingsPageArrow /> |
| 108 |
</span> |
| 109 |
</a> |
| 110 |
</> |
| 111 |
); |
| 112 |
}; |
| 113 |
|
| 114 |
export default Settings; |
| 115 |
|