Files
suwayomi-material-you-webui/src/modules/navigation-bar/contexts/NavBarContextProvider.tsx

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-03-03 15:09:18 +05:30
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2022-03-03 15:09:18 +05:30
import React, { useCallback, useMemo, useState } from 'react';
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
2024-10-05 16:09:34 +02:00
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
import { INavbarOverride } from '@/modules/navigation-bar/NavigationBar.types.ts';
2022-03-03 15:09:18 +05:30
interface IProps {
children: React.ReactNode;
2022-03-03 15:09:18 +05:30
}
2023-10-28 00:32:02 +02:00
export function NavBarContextProvider({ children }: IProps) {
const [title, setTitle] = useState<string | React.ReactNode>('Suwayomi');
2022-03-03 15:09:18 +05:30
const [action, setAction] = useState<any>(<div />);
2024-07-11 17:22:52 +02:00
const [appBarHeight, setAppBarHeight] = useState(0);
2022-03-03 15:09:18 +05:30
const [override, setOverride] = useState<INavbarOverride>({
status: false,
value: <div />,
});
2024-07-11 17:22:52 +02:00
const [isCollapsed, setIsCollapsed] = useLocalStorage('NavBar::isCollapsed', false);
const [navBarWidth, setNavBarWidth] = useState(0);
const [readerNavBarWidth, setReaderNavBarWidth] = useState(0);
2024-07-11 17:22:52 +02:00
const [bottomBarHeight, setBottomBarHeight] = useState(0);
2022-03-03 15:09:18 +05:30
const updateTitle = useCallback(
(newTitle: string | React.ReactNode, browserTitle: string = typeof newTitle === 'string' ? newTitle : '') => {
document.title = `${browserTitle} - Suwayomi`;
setTitle(newTitle);
},
[setTitle],
);
const value = useMemo(
() => ({
title,
setTitle: updateTitle,
2024-07-11 17:22:52 +02:00
appBarHeight,
setAppBarHeight,
action,
setAction,
override,
setOverride,
2024-07-11 17:22:52 +02:00
isCollapsed,
setIsCollapsed,
navBarWidth,
setNavBarWidth,
readerNavBarWidth,
setReaderNavBarWidth,
2024-07-11 17:22:52 +02:00
bottomBarHeight,
setBottomBarHeight,
}),
2024-07-11 17:22:52 +02:00
[
title,
updateTitle,
appBarHeight,
setAppBarHeight,
action,
setAction,
override,
setOverride,
isCollapsed,
setIsCollapsed,
navBarWidth,
setNavBarWidth,
readerNavBarWidth,
setReaderNavBarWidth,
2024-07-11 17:22:52 +02:00
bottomBarHeight,
setBottomBarHeight,
],
);
return <NavBarContext.Provider value={value}>{children}</NavBarContext.Provider>;
2022-03-03 15:09:18 +05:30
}