Path: blob/master/src/packages/frontend/app/use-context.ts
5790 views
/*1Just the minimal app context definitions so that this can also be imported by the nextjs app.23This should be renderable server side, e.g., no window references, etc.4*/56import { ThemeConfig } from "antd";7import type { SizeType } from "antd/es/config-provider/SizeContext";8import { createContext, ReactNode, useContext } from "react";910import { isIntlMessage, type IntlMessage } from "@cocalc/util/i18n/types";11import { COLORS } from "@cocalc/util/theme";1213import { ACTIVITY_BAR_LABELS_DEFAULT } from "@cocalc/frontend/project/page/activity-bar-consts";14import {15FONT_SIZE_ICONS_NARROW,16FONT_SIZE_ICONS_NORMAL,17NAV_HEIGHT_NARROW_PX,18NAV_HEIGHT_PX,19type PageStyle,20} from "./top-nav-consts";2122export interface AppState {23pageWidthPx: number;24pageStyle: PageStyle;25antdComponentSize?: SizeType;26antdTheme?: ThemeConfig;27formatIntl: (msg: IntlMessage | ReactNode | string) => ReactNode | string;28displayI18N: (label: string | IntlMessage | ReactNode) => string | ReactNode;29timeAgoAbsolute?: boolean;30setTimeAgoAbsolute?: (boolean) => void;31showActBarLabels?: boolean; // whether to show labels on the vertical fixed bar32}3334export const DEFAULT_CONTEXT = {35pageWidthPx: 1000, // gets updated36pageStyle: calcStyle(false), // gets updated37formatIntl: () => "Loading…",38displayI18N: (label: string | IntlMessage | ReactNode) => {39// Fallback when intl is not available40if (isIntlMessage(label)) {41return label.defaultMessage;42}43return label;44},45showActBarLabels: ACTIVITY_BAR_LABELS_DEFAULT,46};4748export const AppContext = createContext<AppState>(DEFAULT_CONTEXT);4950export default function useAppContext() {51return useContext(AppContext);52}5354export function calcStyle(isNarrow: boolean): PageStyle {55const fontSizeIcons: string = isNarrow56? FONT_SIZE_ICONS_NARROW57: FONT_SIZE_ICONS_NORMAL;58const topPaddingIcons: string = isNarrow ? "2px" : "5px";59const sidePaddingIcons: string = isNarrow ? "7px" : "14px";6061const height = isNarrow ? NAV_HEIGHT_NARROW_PX : NAV_HEIGHT_PX;6263const topBarStyle = {64height: `${height}px`,65background: "#fafafa",66} as const;6768const fileUseStyle = {69background: "white",70border: `2px solid ${COLORS.GRAY_DDD}`,71borderRadius: "5px",72boxShadow: "0 0 15px #aaa",73fontSize: "10pt",74height: "90%",75margin: 0,76overflowX: "hidden",77overflowY: "auto",78padding: "4px",79position: "fixed",80right: "5vw",81top: `${height}px`,82width: isNarrow ? "90vw" : "50vw",83zIndex: 110,84} as const;8586const projectsNavStyle = isNarrow87? ({88/* this makes it so the projects tabs are on a separate row; otherwise, there is literally no room for them at all... */89width: "100vw",90marginTop: "4px",91height: `${height}px`,92// no flex!93} as const)94: ({95flex: "1 1 auto", // necessary to stretch out to the full width96} as const);9798return {99topBarStyle,100fileUseStyle,101projectsNavStyle,102isNarrow,103sidePaddingIcons,104topPaddingIcons,105fontSizeIcons,106height,107};108}109110111