1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/hooks/useWebConfig.tsx

41 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-06-02 20:58:11 +03:00
import React, { type FC, type PropsWithChildren, createContext, useContext, useEffect, useState } from 'react';
2023-05-05 11:47:08 -04:00
import type { WebConfig } from '../types/webConfig';
import defaultConfig from '../config.json';
import fetchLocal from '../utils/fetchLocal';
export const WebConfigContext = createContext<WebConfig>(defaultConfig);
export const useWebConfig = () => useContext(WebConfigContext);
2024-06-02 20:58:11 +03:00
export const WebConfigProvider: FC<PropsWithChildren<unknown>> = ({ children }) => {
2023-05-05 11:47:08 -04:00
const [ config, setConfig ] = useState<WebConfig>(defaultConfig);
useEffect(() => {
const fetchConfig = async () => {
try {
2023-05-15 09:46:22 -04:00
const response = await fetchLocal('config.json', { cache: 'no-store' });
2023-05-05 11:47:08 -04:00
if (!response.ok) {
throw new Error('network response was not ok');
}
const configData = await response.json();
setConfig(configData);
} catch (err) {
console.warn('[WebConfigProvider] failed to fetch config file', err);
}
};
fetchConfig()
.catch(() => {
// This should never happen since fetchConfig catches errors internally
});
}, [ setConfig ]);
return (
<WebConfigContext.Provider value={config}>
{children}
</WebConfigContext.Provider>
);
};