11import { readTextFileLines } from '@tauri-apps/plugin-fs' ;
2- import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
2+ import { useMutation , useQuery } from '@tanstack/react-query' ;
33import { ServerRoute } from '@/constants/server' ;
44import { timeout } from '@/utils/timers' ;
55import { useConfigStore } from '@/store/config' ;
@@ -58,7 +58,6 @@ export const useLogs = (): {
5858 clear : ( ) => void ;
5959 onScreenshotChange : ( ) => void ;
6060} => {
61- const queryClient = useQueryClient ( ) ;
6261 const setDisconnected = useConfigStore ( ( state ) => state . setDisconnected ) ;
6362 const disconnected = useConfigStore ( ( state ) => state . disconnected ) ;
6463 const logFile = useConfigStore ( ( state ) => state . config ?. outfile || '' ) ;
@@ -67,6 +66,7 @@ export const useLogs = (): {
6766 const { url : serverUrl , apiKey } = useServer ( ) ;
6867 const [ screenshotEnabled , setScreenshotEnabled ] = useState ( false ) ;
6968 const sampleRate = useSampleRate ( ) ;
69+ const [ clearTime , setClearTime ] = useState ( 0 ) ;
7070
7171 const enabled = useMemo ( ( ) => {
7272 if ( overrideLogFile ) {
@@ -79,13 +79,13 @@ export const useLogs = (): {
7979 const logFilePathname = overrideLogFile || logFile ;
8080
8181 const { isPending, error, data, refetch } = useQuery ( {
82- queryKey : [ 'logs' , logFilePathname ] ,
82+ queryKey : [ 'logs' , logFilePathname , clearTime ] ,
8383 queryFn : async ( ) : Promise < Log [ ] > => {
8484 try {
8585 const dataLogs : Log [ ] = [ ] ;
8686
8787 if ( isWeb ( ) ) {
88- const response = await fetch ( `/public/example.log` ) ;
88+ const response = await fetch ( logFilePathname ) ;
8989 const raw = await response . text ( ) ;
9090 const lines = raw . split ( '\n' ) ;
9191 for ( const line of lines ) {
@@ -108,7 +108,8 @@ export const useLogs = (): {
108108 console . log ( { dataLogs } ) ;
109109
110110 const logs = unionBy < Log , string > ( data || [ ] , dataLogs , ( item ) => item . id ) as Log [ ] ;
111- return logs ;
111+
112+ return logs . filter ( ( log ) => log . time > clearTime ) ;
112113 } catch ( e ) {
113114 console . log ( 'error' , e ) ;
114115 setDisconnected ( true ) ;
@@ -119,30 +120,6 @@ export const useLogs = (): {
119120 enabled : enabled ,
120121 } ) ;
121122
122- const mutation = useMutation ( {
123- mutationFn : async ( ) => {
124- try {
125- await timeout < Response > (
126- 3000 ,
127- fetch ( `${ serverUrl } ${ ServerRoute . LOG } ?action=clear` , {
128- method : 'POST' ,
129- headers : {
130- 'x-api-key' : apiKey ,
131- } ,
132- } ) ,
133- ) ;
134-
135- return [ ] ;
136- } catch {
137- setDisconnected ( true ) ;
138- return [ ] ;
139- }
140- } ,
141- onSuccess : ( ) => {
142- queryClient . invalidateQueries ( { queryKey : [ 'logs' ] } ) ;
143- } ,
144- } ) ;
145-
146123 const enableScreenshotsMutation = useMutation ( {
147124 mutationFn : async ( ) => {
148125 try {
@@ -169,14 +146,9 @@ export const useLogs = (): {
169146 } ) ;
170147
171148 const clear = ( ) => {
172- mutation . mutate ( ) ;
173-
174- queryClient . cancelQueries ( {
175- queryKey : [ 'logs' ] ,
176- exact : true ,
177- } ) ;
149+ const lastNow = data ?. [ data . length - 1 ] ?. time || 0 ;
178150
179- queryClient . setQueryData ( [ 'logs' ] , [ ] ) ;
151+ setClearTime ( lastNow ) ;
180152 } ;
181153
182154 const onScreenshotChange = ( ) => {
0 commit comments