-
Notifications
You must be signed in to change notification settings - Fork 885
Add support for filtering in the Object Explorer based on server tags… #8955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
web/pgadmin/static/js/tree/ObjectExplorer/ObjectExplorerFilter.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| ///////////////////////////////////////////////////////////// | ||
| // | ||
| // pgAdmin 4 - PostgreSQL Tools | ||
| // | ||
| // Copyright (C) 2013 - 2025, The pgAdmin Development Team | ||
| // This software is released under the PostgreSQL Licence | ||
| // | ||
| ////////////////////////////////////////////////////////////// | ||
|
|
||
| import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; | ||
| import { usePgAdmin } from '../../PgAdminProvider'; | ||
| import { Box, styled } from '@mui/material'; | ||
| import { DefaultButton, PrimaryButton } from '../../components/Buttons'; | ||
| import gettext from 'sources/gettext'; | ||
| import { FormInputSelect, FormNote } from '../../components/FormComponents'; | ||
| import getApiInstance, { parseApiError } from '../../api_instance'; | ||
| import url_for from 'sources/url_for'; | ||
| import Loader from '../../components/Loader'; | ||
|
|
||
| const ObjectExplorerFilterRoot = styled('div')(({ theme }) => ({ | ||
| position: 'absolute', | ||
| top: 0, | ||
| left: 0, | ||
| width: '100%', | ||
| display: 'flex', | ||
| backgroundColor: theme.palette.background.paper, | ||
| padding: '8px', | ||
| flexDirection: 'column', | ||
| gap: '8px', | ||
| ...theme.mixins.panelBorder?.bottom, | ||
| })); | ||
|
|
||
|
|
||
| export default function ObjectExplorerFilter() { | ||
| const [open, setOpen] = useState(false); | ||
| const appliedFiltersRef = useRef(null); | ||
| const [currFilter, setCurrFilter] = useState({ | ||
| tags: [], | ||
| }); | ||
| const [loadingText, setLoadingText] = useState(''); | ||
| const api = useMemo(()=>getApiInstance(), []); | ||
| const pgAdmin = usePgAdmin(); | ||
| const firstEleRef = useRef(null); | ||
| const openServersRef = useRef({}); | ||
|
|
||
| const onClose = ()=>{ | ||
| setOpen(false); | ||
| }; | ||
|
|
||
| const updateAppliedFilters = (filters) => { | ||
| appliedFiltersRef.current = filters; | ||
| const hasFilters = filters?.tags?.length > 0; | ||
| pgAdmin.Browser.Events.trigger('pgadmin:object-explorer:filter:apply', hasFilters); | ||
| }; | ||
|
|
||
| const fetchFilter = async () => { | ||
| try { | ||
| setLoadingText(gettext('Loading...')); | ||
| const {data: resp} = await api.get(url_for('settings.object_explorer_filter')); | ||
| setCurrFilter(resp.data.result); | ||
| updateAppliedFilters(resp.data.result); | ||
| } catch(error) { | ||
| console.error('Error fetching object explorer filter:', error); | ||
| } | ||
| setLoadingText(''); | ||
| }; | ||
|
|
||
| const applyFilter = async () => { | ||
| try { | ||
| setLoadingText(gettext('Applying filter...')); | ||
| await api.put(url_for('settings.object_explorer_filter'), currFilter); | ||
| updateAppliedFilters(currFilter); | ||
|
|
||
| // Save the state of the browser tree | ||
| await pgAdmin.Browser.browserTreeState.save_state(); | ||
|
|
||
| // register to add event to open the server | ||
| const deregister = pgAdmin.Browser.Events.on('pgadmin-browser:tree:added', async (item, d)=>{ | ||
| if(d._type == 'server' && openServersRef.current[d._id]) { | ||
| delete openServersRef.current[d._id]; | ||
| await pgAdmin.Browser.tree.ensureLoaded(item); | ||
| await pgAdmin.Browser.tree.open(item); | ||
| } | ||
| if(Object.keys(openServersRef.current).length === 0) { | ||
| // all servers are opened, deregister the event to avoid unnecessary calls | ||
| deregister(); | ||
| }; | ||
| }); | ||
|
|
||
| // lets do one server group at a time | ||
| (pgAdmin.Browser.tree.children()||[]).forEach(async (serverGroup)=>{ | ||
| // restore tree state works after a server is opened | ||
| // We will note server open state here before refreshing | ||
| pgAdmin.Browser.tree.children(serverGroup).forEach((server)=>{ | ||
| const serverData = server._metadata.data; | ||
| if(pgAdmin.Browser.tree.isOpen(server)) { | ||
| openServersRef.current[serverData._id] = serverData._id; | ||
| } else { | ||
| delete openServersRef.current[serverData._id]; | ||
| } | ||
| }); | ||
|
|
||
| // refresh the server group to apply the filter | ||
| await pgAdmin.Browser.tree.refresh(serverGroup); | ||
| }); | ||
| setLoadingText(''); | ||
| onClose(); | ||
| } catch(error) { | ||
| console.error('Error applying object explorer filter:', error); | ||
| pgAdmin.Browser.notifier.error(parseApiError(error)); | ||
| setLoadingText(''); | ||
| } | ||
| }; | ||
|
|
||
| const onChange = (v) => { | ||
| setCurrFilter((prev)=>({...prev, tags: v})); | ||
| }; | ||
|
|
||
| useEffect(()=>{ | ||
| fetchFilter(); | ||
| const deregister = pgAdmin.Browser.Events.on('pgadmin:object-explorer:filter:show', ()=>{ | ||
| setOpen(true); | ||
| }); | ||
| return ()=>{ | ||
| deregister(); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(()=>{ | ||
| if(!open) return; | ||
| setCurrFilter(appliedFiltersRef.current); | ||
| }, [open]); | ||
|
|
||
| useLayoutEffect(()=>{ | ||
| if(!open) return; | ||
| // Focus on the first element when the filter is opened | ||
| firstEleRef.current?.focus(); | ||
| }, [open]); | ||
|
|
||
| if(!open) { | ||
| return <></>; | ||
| } | ||
|
|
||
| return ( | ||
| <ObjectExplorerFilterRoot sx={{ display: open ? 'flex' : 'none' }}> | ||
| <Loader message={loadingText} /> | ||
| <FormInputSelect inputRef={firstEleRef} label={gettext('Tags')} controlProps={{ | ||
| multiple: true, | ||
| allowClear: true, | ||
| creatable: true, | ||
| noDropdown: true, | ||
| }} value={currFilter.tags} onChange={onChange} placeholder={gettext('Specify the tags...')} /> | ||
| <FormNote text={gettext('Applying the filter will only hide the servers from view, it won\'t close any active connections.')} /> | ||
| <Box sx={{display: 'flex', justifyContent: 'flex-end'}}> | ||
| <DefaultButton size="small" onClick={()=>onClose()}>Close</DefaultButton> | ||
| <PrimaryButton size="small" onClick={applyFilter} sx={{marginLeft: '8px'}}>Apply</PrimaryButton> | ||
| </Box> | ||
| </ObjectExplorerFilterRoot> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.