@@ -5,7 +5,7 @@ import { ConfigRelatedChangesFilters } from "@flanksource-ui/components/Configs/
55import { ConfigDetailsTabs } from "@flanksource-ui/components/Configs/ConfigDetailsTabs" ;
66import { InfoMessage } from "@flanksource-ui/components/InfoMessage" ;
77import { Toggle } from "@flanksource-ui/ui/FormControls/Toggle" ;
8- import { useEffect , useState } from "react" ;
8+ import { useEffect , useMemo , useRef , useState } from "react" ;
99import { useParams , useSearchParams } from "react-router-dom" ;
1010
1111function getNewestInsertedAt ( changes : ConfigChange [ ] ) : string | undefined {
@@ -19,6 +19,16 @@ function getNewestInsertedAt(changes: ConfigChange[]): string | undefined {
1919 return latest ;
2020}
2121
22+ function getLiveTailQueryKey ( params : URLSearchParams ) {
23+ return Array . from ( params . entries ( ) )
24+ . filter ( ( [ key ] ) => key !== "changeId" )
25+ . sort ( ( [ aKey , aValue ] , [ bKey , bValue ] ) =>
26+ aKey === bKey ? aValue . localeCompare ( bValue ) : aKey . localeCompare ( bKey )
27+ )
28+ . map ( ( [ key , value ] ) => `${ key } =${ value } ` )
29+ . join ( "&" ) ;
30+ }
31+
2232export function ConfigDetailsChangesPage ( ) {
2333 const { id } = useParams ( ) ;
2434
@@ -32,18 +42,21 @@ export function ConfigDetailsChangesPage() {
3242 const [ liveTail , setLiveTail ] = useState ( false ) ;
3343 const [ tailCursor , setTailCursor ] = useState < string | undefined > ( undefined ) ;
3444 const [ tailedChanges , setTailedChanges ] = useState < ConfigChange [ ] > ( [ ] ) ;
45+ const liveTailQueryKey = useMemo ( ( ) => getLiveTailQueryKey ( params ) , [ params ] ) ;
46+ const liveTailQueryKeyRef = useRef ( liveTailQueryKey ) ;
3547
36- const { data, isLoading, error, refetch } = useGetConfigChangesByIDQuery ( {
37- keepPreviousData : true ,
38- enabled : ! ! id
39- } ) ;
48+ const { data, isLoading, error, refetch, isPreviousData } =
49+ useGetConfigChangesByIDQuery ( {
50+ keepPreviousData : true ,
51+ enabled : ! ! id
52+ } ) ;
4053
4154 // Initialize cursor from base data when live tail is turned on
4255 useEffect ( ( ) => {
43- if ( liveTail && data ?. changes ?. length && ! tailCursor ) {
56+ if ( liveTail && data ?. changes ?. length && ! tailCursor && ! isPreviousData ) {
4457 setTailCursor ( getNewestInsertedAt ( data . changes ) ) ;
4558 }
46- } , [ liveTail , data , tailCursor ] ) ;
59+ } , [ liveTail , data , tailCursor , isPreviousData ] ) ;
4760
4861 // Reset when live tail is turned off
4962 useEffect ( ( ) => {
@@ -54,6 +67,20 @@ export function ConfigDetailsChangesPage() {
5467 }
5568 } , [ liveTail , refetch ] ) ;
5669
70+ // Reset live tail state whenever filters, sorting, pagination, or date range changes.
71+ useEffect ( ( ) => {
72+ if ( ! liveTail ) {
73+ liveTailQueryKeyRef . current = liveTailQueryKey ;
74+ return ;
75+ }
76+ if ( liveTailQueryKeyRef . current === liveTailQueryKey ) {
77+ return ;
78+ }
79+ liveTailQueryKeyRef . current = liveTailQueryKey ;
80+ setTailedChanges ( [ ] ) ;
81+ setTailCursor ( undefined ) ;
82+ } , [ liveTail , liveTailQueryKey ] ) ;
83+
5784 const { data : pollData } = useGetConfigChangesByIDQuery ( {
5885 from_inserted_at : tailCursor ,
5986 keepPreviousData : false ,
@@ -102,9 +129,13 @@ export function ConfigDetailsChangesPage() {
102129 const newTailedCount = tailedWithConfig . filter (
103130 ( c ) => ! baseIds . has ( c . id )
104131 ) . length ;
105- const changes = [ ...tailedWithConfig , ...baseWithoutTailed ] ;
132+ const changes = liveTail
133+ ? [ ...tailedWithConfig , ...baseWithoutTailed ]
134+ : baseChanges ;
106135
107- const totalChanges = ( data ?. total ?? 0 ) + newTailedCount ;
136+ const totalChanges = liveTail
137+ ? ( data ?. total ?? 0 ) + newTailedCount
138+ : ( data ?. total ?? 0 ) ;
108139 const totalChangesPages = Math . ceil ( totalChanges / parseInt ( pageSize ) ) ;
109140
110141 if ( error ) {
0 commit comments