Files
streamyfin/utils/log.tsx
Uruk f543fa9e3e refactor: remove unused code and simplify implementations
Removes extensive dead code including unused components, utilities, and augmentations that were no longer referenced in the codebase.

Simplifies play settings logic by removing complex stream ranking algorithm in favor of direct previous index matching for audio and subtitle selections.

Removes aspectRatio prop from video player as it was set to a constant "default" value and never changed.

Inlines POSTER_CAROUSEL_HEIGHT constant directly where used instead of importing from centralized constants file.

Eliminates unused features including image color extraction for TV platforms, M3U8 subtitle parsing, and various Jellyfin API helpers that were no longer needed.

Cleans up credential management by making internal helper functions private that should not be exposed to external consumers.
2026-01-12 11:04:23 +01:00

90 lines
2.4 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { atomWithStorage, createJSONStorage } from "jotai/utils";
import type React from "react";
import { createContext, useContext } from "react";
import { storage } from "./mmkv";
export type LogLevel = "INFO" | "WARN" | "ERROR" | "DEBUG";
interface LogEntry {
timestamp: string;
level: LogLevel;
message: string;
data?: any;
}
const mmkvStorage = createJSONStorage(() => ({
getItem: (key: string) => storage.getString(key) || null,
setItem: (key: string, value: string) => storage.set(key, value),
removeItem: (key: string) => storage.remove(key),
}));
const logsAtom = atomWithStorage("logs", [], mmkvStorage);
const LogContext = createContext<ReturnType<typeof useLogProvider> | null>(
null,
);
const _DownloadContext = createContext<ReturnType<
typeof useLogProvider
> | null>(null);
function useLogProvider() {
const { data: logs } = useQuery({
queryKey: ["logs"],
queryFn: async () => readFromLog(),
refetchInterval: 1000,
});
return {
logs,
};
}
export const writeToLog = (level: LogLevel, message: string, data?: any) => {
const newEntry: LogEntry = {
timestamp: new Date().toISOString(),
level: level,
message: message,
data: data,
};
const currentLogs = storage.getString("logs");
const logs: LogEntry[] = currentLogs ? JSON.parse(currentLogs) : [];
logs.push(newEntry);
const maxLogs = 100;
const recentLogs = logs.slice(Math.max(logs.length - maxLogs, 0));
storage.set("logs", JSON.stringify(recentLogs));
};
export const writeInfoLog = (message: string, data?: any) =>
writeToLog("INFO", message, data);
export const writeErrorLog = (message: string, data?: any) =>
writeToLog("ERROR", message, data);
export const writeDebugLog = (message: string, data?: any) => {
if (process.env.EXPO_PUBLIC_WRITE_DEBUG === "1") {
writeToLog("DEBUG", message, data);
}
};
export const readFromLog = (): LogEntry[] => {
const logs = storage.getString("logs");
return logs ? JSON.parse(logs) : [];
};
export function useLog() {
const context = useContext(LogContext);
if (context === null) {
throw new Error("useLog must be used within a LogProvider");
}
return context;
}
export function LogProvider({ children }: { children: React.ReactNode }) {
const provider = useLogProvider();
return <LogContext.Provider value={provider}>{children}</LogContext.Provider>;
}
export default logsAtom;