@@ -583,6 +583,14 @@ function addToHistory(query) {
583583function updateHistoryUI ( ) {
584584 elements . queryHistoryElm . innerHTML = '' ;
585585
586+ if ( state . queryHistory . length === 0 ) {
587+ const emptyMessage = document . createElement ( 'div' ) ;
588+ emptyMessage . className = 'query-history-empty' ;
589+ emptyMessage . textContent = 'No query history yet' ;
590+ elements . queryHistoryElm . appendChild ( emptyMessage ) ;
591+ return ;
592+ }
593+
586594 state . queryHistory . forEach ( ( item ) => {
587595 const historyItem = createHistoryItem ( item ) ;
588596 elements . queryHistoryElm . appendChild ( historyItem ) ;
@@ -621,7 +629,29 @@ function truncateString(str, maxLength) {
621629}
622630
623631function toggleQueryHistory ( ) {
624- elements . queryHistoryElm . classList . toggle ( 'show' ) ;
632+ // Don't open history if there are no items
633+ if ( ! elements . queryHistoryElm . classList . contains ( 'show' ) && state . queryHistory . length === 0 ) {
634+ showNotification ( 'No query history yet' ) ;
635+ return ;
636+ }
637+
638+ const historyElement = elements . queryHistoryElm ;
639+ const isVisible = historyElement . classList . contains ( 'show' ) ;
640+
641+ if ( ! isVisible ) {
642+ // Position the history panel over the editor
643+ const editorRect = editor . getWrapperElement ( ) . getBoundingClientRect ( ) ;
644+ historyElement . style . width = `${ editorRect . width - 20 } px` ;
645+ historyElement . style . top = '10px' ;
646+ historyElement . style . height = 'auto' ;
647+ historyElement . style . maxHeight = `${ editorRect . height - 20 } px` ;
648+ }
649+
650+ historyElement . classList . toggle ( 'show' ) ;
651+ }
652+
653+ function closeQueryHistory ( ) {
654+ elements . queryHistoryElm . classList . remove ( 'show' ) ;
625655}
626656
627657// Toggle history button
@@ -632,7 +662,21 @@ document.addEventListener('click', function(e) {
632662 if ( elements . queryHistoryElm . classList . contains ( 'show' ) &&
633663 ! elements . queryHistoryElm . contains ( e . target ) &&
634664 e . target !== elements . toggleHistoryBtn ) {
635- elements . queryHistoryElm . classList . remove ( 'show' ) ;
665+ closeQueryHistory ( ) ;
666+ }
667+ } ) ;
668+
669+ // Close history when pressing Escape
670+ document . addEventListener ( 'keydown' , function ( e ) {
671+ if ( e . key === 'Escape' && elements . queryHistoryElm . classList . contains ( 'show' ) ) {
672+ closeQueryHistory ( ) ;
673+ }
674+ } ) ;
675+
676+ // Close history when editing code
677+ editor . on ( 'change' , function ( ) {
678+ if ( elements . queryHistoryElm . classList . contains ( 'show' ) ) {
679+ closeQueryHistory ( ) ;
636680 }
637681} ) ;
638682
0 commit comments