Path: blob/master/src/packages/frontend/app/context.tsx
1496 views
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { theme, ThemeConfig } from "antd";6import { debounce } from "lodash";7import { ReactNode, useEffect, useMemo, useState } from "react";8import { useIntl } from "react-intl";910import { useTypedRedux } from "@cocalc/frontend/app-framework";11import { IntlMessage, isIntlMessage } from "@cocalc/frontend/i18n";12import { ACTIVITY_BAR_LABELS } from "@cocalc/frontend/project/page/activity-bar-consts";13import { COLORS } from "@cocalc/util/theme";14import { NARROW_THRESHOLD_PX, PageStyle } from "./top-nav-consts";15import useAppContext, { AppContext, AppState, calcStyle } from "./use-context";1617export { AppContext, useAppContext };1819export function useAppContextProvider(): AppState {20const intl = useIntl();21const other_settings = useTypedRedux("account", "other_settings");22const showActBarLabels = other_settings.get(ACTIVITY_BAR_LABELS) ?? true;2324const [pageWidthPx, setPageWidthPx] = useState<number>(window.innerWidth);2526const [narrow, setNarrow] = useState<boolean>(isNarrow());2728function update() {29setNarrow(isNarrow());30if (window.innerWidth != pageWidthPx) {31setPageWidthPx(window.innerWidth);32}33}3435useEffect(() => {36const handleResize = debounce(update, 50, {37leading: false,38trailing: true,39});4041window.addEventListener("resize", handleResize);42return () => window.removeEventListener("resize", handleResize);43}, []);4445// avoid updating the style on every resize event46const pageStyle: PageStyle = useMemo(() => {47return calcStyle(narrow);48}, [narrow]);4950function formatIntl(51msg: IntlMessage | ReactNode | string,52): ReactNode | string {53if (isIntlMessage(msg)) {54return intl.formatMessage(msg);55} else {56return msg;57}58}5960return {61formatIntl,62pageWidthPx,63pageStyle,64showActBarLabels,65};66}6768export function useAntdStyleProvider() {69const other_settings = useTypedRedux("account", "other_settings");70const rounded = other_settings?.get("antd_rounded", true);71const animate = other_settings?.get("antd_animate", true);72const branded = other_settings?.get("antd_brandcolors", false);73const compact = other_settings?.get("antd_compact", false);7475const borderStyle = rounded76? undefined77: { borderRadius: 0, borderRadiusLG: 0, borderRadiusSM: 0 };7879const animationStyle = animate ? undefined : { motion: false };8081const brandedColors = branded82? { colorPrimary: COLORS.COCALC_BLUE }83: undefined;8485const algorithm = compact ? { algorithm: theme.compactAlgorithm } : undefined;8687const antdTheme: ThemeConfig = {88...algorithm,89token: {90...brandedColors,91...borderStyle,92...animationStyle,93},94components: {95Button: {96...brandedColors,97},98},99};100101return {102antdTheme,103};104}105106function isNarrow(): boolean {107return window.innerWidth != null && window.innerWidth <= NARROW_THRESHOLD_PX;108}109110111