index.js
227 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | |
| 3 | const { isPro, adminURL, showAnalyticsTab } = |
| 4 | typeof _EVF_DASHBOARD_ !== 'undefined' && _EVF_DASHBOARD_; |
| 5 | |
| 6 | const normalizeAdminURL = (url) => { |
| 7 | if (!url) return ''; |
| 8 | |
| 9 | let cleanURL = url.endsWith('/') ? url.slice(0, -1) : url; |
| 10 | |
| 11 | if (cleanURL.endsWith('/admin.php')) { |
| 12 | cleanURL = cleanURL.slice(0, -10); |
| 13 | } |
| 14 | |
| 15 | return cleanURL; |
| 16 | }; |
| 17 | |
| 18 | const cleanAdminURL = normalizeAdminURL(adminURL); |
| 19 | |
| 20 | let ROUTES = [ |
| 21 | { |
| 22 | route: '/', |
| 23 | label: __('Site Assistant', 'everest-forms'), |
| 24 | external: false, |
| 25 | key: 'siteAssistant', |
| 26 | }, |
| 27 | { |
| 28 | route: `${cleanAdminURL}/admin.php?page=evf-analytics`, |
| 29 | label: __('Analytics', 'everest-forms'), |
| 30 | external: true, |
| 31 | }, |
| 32 | { |
| 33 | route: `${cleanAdminURL}/admin.php?page=evf-builder`, |
| 34 | label: __('All Forms', 'everest-forms'), |
| 35 | external: true, |
| 36 | key: 'forms', |
| 37 | }, |
| 38 | { |
| 39 | route: `${cleanAdminURL}/admin.php?page=evf-entries`, |
| 40 | label: __('Entries', 'everest-forms'), |
| 41 | external: true, |
| 42 | key: 'entries', |
| 43 | }, |
| 44 | { |
| 45 | route: `${cleanAdminURL}/admin.php?page=evf-settings`, |
| 46 | label: __('Settings', 'everest-forms'), |
| 47 | external: true, |
| 48 | key: 'settings', |
| 49 | }, |
| 50 | |
| 51 | { |
| 52 | route: '/features', |
| 53 | label: __('Addons', 'everest-forms'), |
| 54 | external: false, |
| 55 | key: 'features', |
| 56 | }, |
| 57 | { |
| 58 | route: '/help', |
| 59 | label: __('Help', 'everest-forms'), |
| 60 | external: false, |
| 61 | key: 'help', |
| 62 | }, |
| 63 | ]; |
| 64 | |
| 65 | if (!isPro) { |
| 66 | ROUTES = [ |
| 67 | ...ROUTES.slice(0, 4), |
| 68 | { |
| 69 | route: 'https://everestforms.net/free-vs-pro/', |
| 70 | label: __('Free vs Pro', 'everest-forms'), |
| 71 | external: true, |
| 72 | }, |
| 73 | ...ROUTES.slice(4), |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | export default ROUTES; |
| 78 | |
| 79 | export const CHANGELOG_TAG_COLORS = { |
| 80 | fix: { |
| 81 | color: 'primary.500', |
| 82 | bgColor: 'primary.100', |
| 83 | scheme: 'primary', |
| 84 | }, |
| 85 | feature: { |
| 86 | color: 'green.500', |
| 87 | bgColor: 'green.50', |
| 88 | scheme: 'green', |
| 89 | }, |
| 90 | enhance: { |
| 91 | color: 'teal.500', |
| 92 | bgColor: 'teal.50', |
| 93 | scheme: 'teal', |
| 94 | }, |
| 95 | refactor: { |
| 96 | color: 'pink.500', |
| 97 | bgColor: 'pink.50', |
| 98 | scheme: 'pink', |
| 99 | }, |
| 100 | dev: { |
| 101 | color: 'orange.500', |
| 102 | bgColor: 'orange.50', |
| 103 | scheme: 'orange', |
| 104 | }, |
| 105 | tweak: { |
| 106 | color: 'purple.500', |
| 107 | bgColor: 'purple.50', |
| 108 | scheme: 'purple', |
| 109 | }, |
| 110 | }; |
| 111 | |
| 112 | export const facebookUrl = 'https://www.facebook.com/groups/everestforms'; |
| 113 | export const youtubeChannelUrl = 'https://www.youtube.com/@EverestForms'; |
| 114 | export const twitterUrl = 'https://twitter.com/everestforms'; |
| 115 | export const reviewUrl = |
| 116 | 'https://wordpress.org/support/plugin/everest-forms/reviews/?rate=5#new-post'; |
| 117 | |
| 118 | /** |
| 119 | * Normalize admin URL - remove trailing slashes and admin.php if present |
| 120 | * @param {string} url - WordPress admin URL |
| 121 | * @returns {string} - Normalized URL |
| 122 | */ |
| 123 | const normalizeURL = (url) => { |
| 124 | if (!url) return ''; |
| 125 | |
| 126 | let cleanURL = url.endsWith('/') ? url.slice(0, -1) : url; |
| 127 | |
| 128 | if (cleanURL.endsWith('/admin.php')) { |
| 129 | cleanURL = cleanURL.slice(0, -10); |
| 130 | } |
| 131 | |
| 132 | return cleanURL; |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * Convert internal routes to full admin URLs when needed |
| 137 | * @param {string} route - The route path |
| 138 | * @param {boolean} isNonDashboardPage - Whether we're on a non-dashboard page (settings, entries, etc.) |
| 139 | * @param {string} adminURL - WordPress admin URL |
| 140 | * @returns {string} - Converted route |
| 141 | */ |
| 142 | export const convertRoute = (route, isNonDashboardPage, adminURL) => { |
| 143 | if (route.includes('admin.php') || route.includes('?page=')) { |
| 144 | return route; |
| 145 | } |
| 146 | |
| 147 | if (isNonDashboardPage) { |
| 148 | const cleanURL = normalizeURL(adminURL); |
| 149 | |
| 150 | if (route === '/') { |
| 151 | return `${cleanURL}/admin.php?page=evf-dashboard`; |
| 152 | } |
| 153 | return `${cleanURL}/admin.php?page=evf-dashboard#${route}`; |
| 154 | } |
| 155 | |
| 156 | return route; |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * Check if route is external (WordPress admin link) |
| 161 | * @param {string} route - The route path |
| 162 | * @returns {boolean} |
| 163 | */ |
| 164 | export const isExternalRoute = (route) => { |
| 165 | return route.includes('admin.php') || route.includes('?page='); |
| 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * Check if route is active |
| 170 | * @param {string} route - The route path |
| 171 | * @param {string} currentPath - Current location path (from React Router) |
| 172 | * @param {string} pageType - Current page type (settings, entries, analytics, forms, etc.) |
| 173 | * @returns {boolean} |
| 174 | */ |
| 175 | export const isRouteActive = (route, currentPath, pageType) => { |
| 176 | // Get current page from URL parameters |
| 177 | const urlParams = new URLSearchParams(window.location.search); |
| 178 | const currentPage = urlParams.get('page'); |
| 179 | |
| 180 | // Check for analytics page |
| 181 | if (currentPage === 'evf-analytics' && route.includes('evf-analytics')) { |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | if (currentPage === 'evf-entries' && route.includes('evf-entries')) { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | if (currentPage === 'evf-builder' && route.includes('evf-builder')) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | if (currentPage === 'evf-settings' && route.includes('evf-settings')) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | // For dashboard internal routes (hash-based navigation) |
| 198 | if (currentPage === 'evf-dashboard' || !currentPage) { |
| 199 | // Check if it's a hash route |
| 200 | const hash = window.location.hash.replace('#', ''); |
| 201 | |
| 202 | if (!route.includes('admin.php') && !route.includes('?page=')) { |
| 203 | // This is an internal route |
| 204 | if (hash === '' && route === '/') { |
| 205 | return true; |
| 206 | } |
| 207 | if (hash && route === `/${hash}`) { |
| 208 | return true; |
| 209 | } |
| 210 | if (hash && route === hash) { |
| 211 | return true; |
| 212 | } |
| 213 | // Use React Router's currentPath as fallback |
| 214 | if (currentPath === route) { |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // Check external routes like free-vs-pro |
| 221 | if (route.includes('everestforms.net/free-vs-pro')) { |
| 222 | return false; // External links are never "active" |
| 223 | } |
| 224 | |
| 225 | return false; |
| 226 | }; |
| 227 |