|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2026, GeoSolutions Sas. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This source code is licensed under the BSD-style license found in the |
| 7 | + * LICENSE file in the root directory of this source tree. |
| 8 | + */ |
| 9 | + |
| 10 | +import React, { useEffect, useState, useRef } from 'react'; |
| 11 | +import PropTypes from 'prop-types'; |
| 12 | +import { Dropdown, MenuItem } from 'react-bootstrap'; |
| 13 | +import GeoFence from '../../api/geoserver/GeoFence'; |
| 14 | +import Message from '../../components/I18N/Message'; |
| 15 | + |
| 16 | +/** |
| 17 | + * A dropdown menu to select GeoServer instances for cleaning their cache. |
| 18 | + * |
| 19 | + * @param {Object[]} gsInstances - List of available GeoServer instances (name, url, etc.). |
| 20 | + * @param {boolean} show - Controls whether the menu is visible or not. |
| 21 | + * @param {Function} storeGSInstancesDDList - Redux action to save the fetched instances to the store. |
| 22 | + * @param {Function} onClose - Function to close the menu, used if an error occurs. |
| 23 | + * @param {Function} onError - Function to show an error notification. |
| 24 | + * @param {Function} onSelectAll - Function called when 'Clear For All Instances' is clicked. |
| 25 | + * @param {Function} onSelectInstance - Function called when a specific instance is clicked. |
| 26 | + */ |
| 27 | +const GSCleanCacheMenu = ({ |
| 28 | + gsInstances = [], |
| 29 | + show, |
| 30 | + storeGSInstancesDDList, |
| 31 | + onClose, |
| 32 | + onError, |
| 33 | + onSelectAll, |
| 34 | + onSelectInstance |
| 35 | +}) => { |
| 36 | + const [loading, setLoading] = useState(gsInstances.length ? false : true); |
| 37 | + const menuRef = useRef(null); |
| 38 | + const handleGetGSInstances = () => { |
| 39 | + GeoFence.getGSInstancesForDD().then(response => { |
| 40 | + storeGSInstancesDDList(response.data); |
| 41 | + setLoading(false); |
| 42 | + }).catch(() => { |
| 43 | + setLoading(false); |
| 44 | + onClose(); |
| 45 | + onError({ |
| 46 | + title: "rulesmanager.errorTitle", |
| 47 | + message: "rulesmanager.errorLoadingGSInstances" |
| 48 | + }); |
| 49 | + } |
| 50 | + ); |
| 51 | + |
| 52 | + }; |
| 53 | + useEffect(() => { |
| 54 | + if (gsInstances.length) return; |
| 55 | + handleGetGSInstances(); |
| 56 | + }, []); |
| 57 | + useEffect(() => { |
| 58 | + // Function to check if the click was outside the menu |
| 59 | + const handleClickOutside = (event) => { |
| 60 | + if (menuRef.current && !menuRef.current.contains(event.target)) { |
| 61 | + onClose(); // Call the close handler passed from props |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Only add the listener if the menu is showing |
| 66 | + if (show) { |
| 67 | + document.addEventListener('mousedown', handleClickOutside); |
| 68 | + } |
| 69 | + |
| 70 | + // Cleanup the listener when the menu closes or unmounts |
| 71 | + return () => { |
| 72 | + document.removeEventListener('mousedown', handleClickOutside); |
| 73 | + }; |
| 74 | + }, [show, onClose]); |
| 75 | + // Handler to wrap selection and close the menu |
| 76 | + const handleSelectInstance = (instance) => { |
| 77 | + onSelectInstance(instance); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleSelectAll = (e) => { |
| 81 | + e.stopPropagation(); |
| 82 | + onSelectAll(); |
| 83 | + }; |
| 84 | + |
| 85 | + // If not showing, render null to keep DOM clean |
| 86 | + if (!show) return null; |
| 87 | + |
| 88 | + return ( |
| 89 | + <div ref={menuRef} |
| 90 | + className="gs-cache-menu-container" |
| 91 | + style={{ |
| 92 | + marginTop: '5px' |
| 93 | + }} |
| 94 | + > |
| 95 | + {loading ? <div className={`toolbar-loader ${loading ? 'ms-circle-loader-md' : ''}`}/> : ( |
| 96 | + <Dropdown open id="gs-cache-dropdown"> |
| 97 | + <Dropdown.Toggle style={{ display: 'none' }} /> {/** hide arrow icon of DD */} |
| 98 | + <Dropdown.Menu> |
| 99 | + {/* Instance List */} |
| 100 | + {gsInstances.length === 0 ? ( |
| 101 | + <MenuItem disabled> |
| 102 | + <Message msgId="rulesmanager.noAvailGsInstances" /> |
| 103 | + </MenuItem> |
| 104 | + ) : ( |
| 105 | + gsInstances.map((instance, idx) => { |
| 106 | + const name = instance.name; |
| 107 | + return ( |
| 108 | + <MenuItem |
| 109 | + key={idx} |
| 110 | + onClick={(e) => { |
| 111 | + if (e && e.stopPropagation) e.stopPropagation(); |
| 112 | + handleSelectInstance(instance); |
| 113 | + }} |
| 114 | + title={name} |
| 115 | + > |
| 116 | + {name} |
| 117 | + </MenuItem> |
| 118 | + ); |
| 119 | + }) |
| 120 | + )} |
| 121 | + <MenuItem divider /> |
| 122 | + {/* Clear All Item */} |
| 123 | + <MenuItem |
| 124 | + disabled={gsInstances.length === 0} |
| 125 | + onClick={handleSelectAll} |
| 126 | + className="gs-clear-all-item" |
| 127 | + > |
| 128 | + <strong><Message msgId="rulesmanager.clearAllgsInstances" /></strong> |
| 129 | + </MenuItem> |
| 130 | + |
| 131 | + </Dropdown.Menu> |
| 132 | + </Dropdown>) |
| 133 | + } |
| 134 | + </div> |
| 135 | + ); |
| 136 | +}; |
| 137 | +GSCleanCacheMenu.propTypes = { |
| 138 | + gsInstances: PropTypes.array, |
| 139 | + show: PropTypes.bool, |
| 140 | + storeGSInstancesDDList: PropTypes.func, |
| 141 | + onClose: PropTypes.func, |
| 142 | + onError: PropTypes.func, |
| 143 | + onSelectAll: PropTypes.func, |
| 144 | + onSelectInstance: PropTypes.func |
| 145 | +}; |
| 146 | + |
| 147 | +export default GSCleanCacheMenu; |
0 commit comments