From 94c170e3d2bfd9d62f1211e785ab5890d42ea979 Mon Sep 17 00:00:00 2001 From: sarendsen Date: Mon, 13 Jan 2025 10:32:03 +0100 Subject: [PATCH] chore: some linting --- utils/atoms/settings.ts | 126 +++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/utils/atoms/settings.ts b/utils/atoms/settings.ts index d92f7a74..45e2b905 100644 --- a/utils/atoms/settings.ts +++ b/utils/atoms/settings.ts @@ -1,5 +1,5 @@ import { atom, useAtom } from "jotai"; -import {useCallback, useEffect, useMemo} from "react"; +import { useCallback, useEffect, useMemo } from "react"; import * as ScreenOrientation from "expo-screen-orientation"; import { storage } from "../mmkv"; import { Platform } from "react-native"; @@ -8,12 +8,12 @@ import { PluginStatus, SubtitlePlaybackMode, } from "@jellyfin/sdk/lib/generated-client"; -import {apiAtom} from "@/providers/JellyfinProvider"; -import {getPluginsApi} from "@jellyfin/sdk/lib/utils/api"; -import {writeErrorLog} from "@/utils/log"; +import { apiAtom } from "@/providers/JellyfinProvider"; +import { getPluginsApi } from "@jellyfin/sdk/lib/utils/api"; +import { writeErrorLog } from "@/utils/log"; -const STREAMYFIN_PLUGIN_ID = "1e9e5d386e6746158719e98a5c34f004" -const STREAMYFIN_PLUGIN_SETTINGS = "STREAMYFIN_PLUGIN_SETTINGS" +const STREAMYFIN_PLUGIN_ID = "1e9e5d386e6746158719e98a5c34f004"; +const STREAMYFIN_PLUGIN_SETTINGS = "STREAMYFIN_PLUGIN_SETTINGS"; export type DownloadQuality = "original" | "high" | "low"; @@ -68,7 +68,7 @@ export type DefaultLanguageOption = { export enum DownloadMethod { Remux = "remux", - Optimized = "optimized" + Optimized = "optimized", } export type Settings = { @@ -106,13 +106,15 @@ export type Settings = { export interface Lockable { locked: boolean; - value: T + value: T; } -export type PluginLockableSettings = { [K in keyof Settings]: Lockable }; +export type PluginLockableSettings = { + [K in keyof Settings]: Lockable; +}; export type StreamyfinPluginConfig = { - settings: PluginLockableSettings -} + settings: PluginLockableSettings; +}; const loadSettings = (): Settings => { const defaultValues: Settings = { @@ -172,7 +174,9 @@ const saveSettings = (settings: Settings) => { }; export const settingsAtom = atom(null); -export const pluginSettingsAtom = atom(storage.get(STREAMYFIN_PLUGIN_SETTINGS)); +export const pluginSettingsAtom = atom( + storage.get(STREAMYFIN_PLUGIN_SETTINGS) +); export const useSettings = () => { const [api] = useAtom(apiAtom); @@ -186,41 +190,43 @@ export const useSettings = () => { } }, [_settings, setSettings]); - const setPluginSettings = useCallback((settings: PluginLockableSettings | undefined) => { - storage.setAny(STREAMYFIN_PLUGIN_SETTINGS, settings) - _setPluginSettings(settings) + const setPluginSettings = useCallback( + (settings: PluginLockableSettings | undefined) => { + storage.setAny(STREAMYFIN_PLUGIN_SETTINGS, settings); + _setPluginSettings(settings); }, [_setPluginSettings] - ) + ); - const refreshStreamyfinPluginSettings = useCallback( - async () => { - if (!api) - return + const refreshStreamyfinPluginSettings = useCallback(async () => { + if (!api) return; - const plugins = await getPluginsApi(api).getPlugins().then(({data}) => data); + const plugins = await getPluginsApi(api) + .getPlugins() + .then(({ data }) => data); - if (plugins && plugins.length > 0) { - const streamyfinPlugin = plugins.find(plugin => plugin.Id === STREAMYFIN_PLUGIN_ID); + if (plugins && plugins.length > 0) { + const streamyfinPlugin = plugins.find( + (plugin) => plugin.Id === STREAMYFIN_PLUGIN_ID + ); - if (!streamyfinPlugin || streamyfinPlugin.Status != PluginStatus.Active) { - writeErrorLog( - "Streamyfin plugin is currently not active.\n" + + if (!streamyfinPlugin || streamyfinPlugin.Status != PluginStatus.Active) { + writeErrorLog( + "Streamyfin plugin is currently not active.\n" + `Current status is: ${streamyfinPlugin?.Status}` - ); - setPluginSettings(undefined); - return; - } - - const settings = await api.getStreamyfinPluginConfig() - .then(({data}) => data.settings) - - setPluginSettings(settings); - return settings; + ); + setPluginSettings(undefined); + return; } - }, - [api] - ) + + const settings = await api + .getStreamyfinPluginConfig() + .then(({ data }) => data.settings); + + setPluginSettings(settings); + return settings; + } + }, [api]); const updateSettings = (update: Partial) => { if (settings) { @@ -236,33 +242,47 @@ export const useSettings = () => { // use user settings first and fallback on admin setting if required. const settings: Settings = useMemo(() => { let unlockedPluginDefaults = {} as Settings; - const overrideSettings = Object.entries(pluginSettings || {}) - .reduce((acc, [key, setting]) => { + const overrideSettings = Object.entries(pluginSettings || {}).reduce( + (acc, [key, setting]) => { if (setting) { - const {value, locked} = setting + const { value, locked } = setting; // Make sure we override default settings with plugin settings when they are not locked. // Admin decided what users defaults should be and grants them the ability to change them too. - if (!locked && value && _settings?.[key as keyof Settings] !== value) { - unlockedPluginDefaults = Object.assign(unlockedPluginDefaults, {[key as keyof Settings]: value}) + if ( + !locked && + value && + _settings?.[key as keyof Settings] !== value + ) { + unlockedPluginDefaults = Object.assign(unlockedPluginDefaults, { + [key as keyof Settings]: value, + }); } acc = Object.assign(acc, { - [key]: locked ? value : _settings?.[key as keyof Settings] ?? value - }) + [key]: locked ? value : _settings?.[key as keyof Settings] ?? value, + }); } - return acc - }, {} as Settings) + return acc; + }, + {} as Settings + ); // Update settings with plugin defined defaults if (Object.keys(unlockedPluginDefaults).length > 0) { - updateSettings(unlockedPluginDefaults) + updateSettings(unlockedPluginDefaults); } return { ..._settings, - ...overrideSettings - } - }, [_settings, pluginSettings]) + ...overrideSettings, + }; + }, [_settings, pluginSettings]); - return [settings, updateSettings, pluginSettings, setPluginSettings, refreshStreamyfinPluginSettings] as const; + return [ + settings, + updateSettings, + pluginSettings, + setPluginSettings, + refreshStreamyfinPluginSettings, + ] as const; };