CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/use-context.ts
Views: 821
1
/*
2
Just the minimal app context definitions so that this can also be imported by the nextjs app.
3
4
This should be renderable server side, e.g., no window references, etc.
5
*/
6
7
import { ThemeConfig } from "antd";
8
import type { SizeType } from "antd/es/config-provider/SizeContext";
9
import { createContext, ReactNode, useContext } from "react";
10
import type { IntlMessage } from "@cocalc/util/i18n/types";
11
import { COLORS } from "@cocalc/util/theme";
12
13
import {
14
FONT_SIZE_ICONS_NARROW,
15
FONT_SIZE_ICONS_NORMAL,
16
NAV_HEIGHT_NARROW_PX,
17
NAV_HEIGHT_PX,
18
type PageStyle,
19
} from "./top-nav-consts";
20
21
export interface AppState {
22
pageWidthPx: number;
23
pageStyle: PageStyle;
24
antdComponentSize?: SizeType;
25
antdTheme?: ThemeConfig;
26
formatIntl: (msg: IntlMessage | ReactNode | string) => ReactNode | string;
27
timeAgoAbsolute?: boolean;
28
setTimeAgoAbsolute?: (boolean) => void;
29
}
30
31
export const DEFAULT_CONTEXT = {
32
pageWidthPx: 1000, // gets updated
33
pageStyle: calcStyle(false), // gets updated
34
formatIntl: () => "Loading…",
35
};
36
37
export const AppContext = createContext<AppState>(DEFAULT_CONTEXT);
38
39
export default function useAppContext() {
40
return useContext(AppContext);
41
}
42
43
export function calcStyle(isNarrow: boolean): PageStyle {
44
const fontSizeIcons: string = isNarrow
45
? FONT_SIZE_ICONS_NARROW
46
: FONT_SIZE_ICONS_NORMAL;
47
const topPaddingIcons: string = isNarrow ? "2px" : "5px";
48
const sidePaddingIcons: string = isNarrow ? "7px" : "14px";
49
50
const height = isNarrow ? NAV_HEIGHT_NARROW_PX : NAV_HEIGHT_PX;
51
52
const topBarStyle = {
53
height: `${height}px`,
54
} as const;
55
56
const fileUseStyle = {
57
background: "white",
58
border: `2px solid ${COLORS.GRAY_DDD}`,
59
borderRadius: "5px",
60
boxShadow: "0 0 15px #aaa",
61
fontSize: "10pt",
62
height: "90%",
63
margin: 0,
64
overflowX: "hidden",
65
overflowY: "auto",
66
padding: "4px",
67
position: "fixed",
68
right: "5vw",
69
top: `${height}px`,
70
width: isNarrow ? "90vw" : "50vw",
71
zIndex: 110,
72
} as const;
73
74
const projectsNavStyle = isNarrow
75
? ({
76
/* this makes it so the projects tabs are on a separate row; otherwise, there is literally no room for them at all... */
77
width: "100vw",
78
marginTop: "4px",
79
height: `${height}px`,
80
// no flex!
81
} as const)
82
: ({
83
flex: "1 1 auto", // necessary to stretch out to the full width
84
} as const);
85
86
return {
87
topBarStyle,
88
fileUseStyle,
89
projectsNavStyle,
90
isNarrow,
91
sidePaddingIcons,
92
topPaddingIcons,
93
fontSizeIcons,
94
height,
95
};
96
}
97
98