-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate with fifo-logger (#2231)
* feat: logger context * feat: display logs history * feat: trace DropZone errors with fifo-logger * chore: fix prettier * feat: improve logs history badge * refactor: pass 'logger' object down to nmr-load-save
- Loading branch information
1 parent
991748a
commit 0282462
Showing
8 changed files
with
308 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,48 @@ | ||
import { FifoLogger, LogEntry } from 'fifo-logger'; | ||
import { | ||
createContext, | ||
useContext, | ||
ReactNode, | ||
useRef, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
export const LoggerContext = createContext<{ | ||
logger: FifoLogger; | ||
logsHistory: LogEntry[]; | ||
} | null>(null); | ||
|
||
export function useLogger() { | ||
const context = useContext(LoggerContext); | ||
if (!context) { | ||
throw new Error('Logger context was not found'); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
interface LoggerProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export function LoggerProvider({ children }: LoggerProviderProps) { | ||
const [logsHistory, setLogsHistory] = useState<LogEntry[]>([]); | ||
const loggerRef = useRef<FifoLogger>( | ||
new FifoLogger({ | ||
onChange: (log, logs) => { | ||
setLogsHistory(logs.slice()); | ||
}, | ||
}), | ||
); | ||
|
||
const loggerState = useMemo(() => { | ||
return { logger: loggerRef.current, logsHistory }; | ||
}, [logsHistory]); | ||
|
||
return ( | ||
<LoggerContext.Provider value={loggerState}> | ||
{children} | ||
</LoggerContext.Provider> | ||
); | ||
} |
This file contains 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 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
Oops, something went wrong.