Files
ontime/apps/client/src/common/stores/logger.ts
T
Carlos Valente 1849b4d39f Deps migration (#1988)
* chore: migrate eslint to oxlint

* chore: migrate prettier to oxfmt

* chore: migrate typescript

* chore: toThrow should have a expected value

* chore: cast test value as Day

* chore: small title fix

* chore: mocks should be hoisted

* chore: incorrect async useage

* chore: test should be inside description

* chore: test sohuld include an expeced

* chore: oxfmt

---------

Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
2026-03-08 16:22:12 +01:00

85 lines
1.6 KiB
TypeScript

import { Log, LogLevel, LogOrigin, MessageTag } from 'ontime-types';
import { generateId, millisToString } from 'ontime-utils';
import { useCallback } from 'react';
import { useStore } from 'zustand';
import { createStore } from 'zustand/vanilla';
import { sendSocket } from '../utils/socket';
import { nowInMillis } from '../utils/time';
type LogStore = {
logs: Log[];
};
const logger = createStore<LogStore>(() => ({
logs: [],
}));
export const useLogData = () => useStore(logger);
export const addLog = (log: Log) =>
logger.setState((state) => ({
logs: [log, ...state.logs],
}));
export const clearLogs = () => logger.setState({ logs: [] });
export function useEmitLog() {
/**
* Utility function sends message over socket
* @param text
* @param level
* @private
*/
const _emit = useCallback((text: string, level: LogLevel) => {
const log = {
id: generateId(),
origin: LogOrigin.Client,
time: millisToString(nowInMillis()),
level,
text,
};
sendSocket(MessageTag.Log, log);
}, []);
/**
* Sends a message with level INFO
* @param text
*/
const emitInfo = useCallback(
(text: string) => {
_emit(text, LogLevel.Info);
},
[_emit],
);
/**
* Sends a message with level WARN
* @param text
*/
const emitWarning = useCallback(
(text: string) => {
_emit(text, LogLevel.Warn);
},
[_emit],
);
/**
* Sends a message with level ERROR
* @param text
*/
const emitError = useCallback(
(text: string) => {
_emit(text, LogLevel.Error);
},
[_emit],
);
return {
emitInfo,
emitWarning,
emitError,
};
}