mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Merge pull request #4571 from thornbill/web-config-hook
Add useWebConfig hook
This commit is contained in:
commit
7f23d7b498
3 changed files with 70 additions and 7 deletions
|
@ -5,6 +5,7 @@ import React from 'react';
|
||||||
import StableApp from './apps/stable/App';
|
import StableApp from './apps/stable/App';
|
||||||
import { HistoryRouter } from './components/router/HistoryRouter';
|
import { HistoryRouter } from './components/router/HistoryRouter';
|
||||||
import { ApiProvider } from './hooks/useApi';
|
import { ApiProvider } from './hooks/useApi';
|
||||||
|
import { WebConfigProvider } from './hooks/useWebConfig';
|
||||||
|
|
||||||
const ExperimentalApp = loadable(() => import('./apps/experimental/App'));
|
const ExperimentalApp = loadable(() => import('./apps/experimental/App'));
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ const RootApp = ({ history }: { history: History }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ApiProvider>
|
<ApiProvider>
|
||||||
|
<WebConfigProvider>
|
||||||
<HistoryRouter history={history}>
|
<HistoryRouter history={history}>
|
||||||
{
|
{
|
||||||
layoutMode === 'experimental' ?
|
layoutMode === 'experimental' ?
|
||||||
|
@ -20,6 +22,7 @@ const RootApp = ({ history }: { history: History }) => {
|
||||||
<StableApp />
|
<StableApp />
|
||||||
}
|
}
|
||||||
</HistoryRouter>
|
</HistoryRouter>
|
||||||
|
</WebConfigProvider>
|
||||||
</ApiProvider>
|
</ApiProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
40
src/hooks/useWebConfig.tsx
Normal file
40
src/hooks/useWebConfig.tsx
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import React, { createContext, FC, useContext, useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
export const WebConfigProvider: FC = ({ children }) => {
|
||||||
|
const [ config, setConfig ] = useState<WebConfig>(defaultConfig);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchConfig = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetchLocal('config.json', { cache: 'no-cache' });
|
||||||
|
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
};
|
20
src/types/webConfig.ts
Normal file
20
src/types/webConfig.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
interface Theme {
|
||||||
|
name: string
|
||||||
|
id: string
|
||||||
|
color: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MenuLink {
|
||||||
|
name: string
|
||||||
|
icon?: string
|
||||||
|
url: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebConfig {
|
||||||
|
includeCorsCredentials?: boolean
|
||||||
|
multiserver?: boolean
|
||||||
|
themes?: Theme[]
|
||||||
|
menuLinks?: MenuLink[]
|
||||||
|
servers?: string[]
|
||||||
|
plugins?: string[]
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue