Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/use-context.ts
5790 views
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
11
import { isIntlMessage, type IntlMessage } from "@cocalc/util/i18n/types";
12
import { COLORS } from "@cocalc/util/theme";
13
14
import { ACTIVITY_BAR_LABELS_DEFAULT } from "@cocalc/frontend/project/page/activity-bar-consts";
15
import {
16
FONT_SIZE_ICONS_NARROW,
17
FONT_SIZE_ICONS_NORMAL,
18
NAV_HEIGHT_NARROW_PX,
19
NAV_HEIGHT_PX,
20
type PageStyle,
21
} from "./top-nav-consts";
22
23
export interface AppState {
24
pageWidthPx: number;
25
pageStyle: PageStyle;
26
antdComponentSize?: SizeType;
27
antdTheme?: ThemeConfig;
28
formatIntl: (msg: IntlMessage | ReactNode | string) => ReactNode | string;
29
displayI18N: (label: string | IntlMessage | ReactNode) => string | ReactNode;
30
timeAgoAbsolute?: boolean;
31
setTimeAgoAbsolute?: (boolean) => void;
32
showActBarLabels?: boolean; // whether to show labels on the vertical fixed bar
33
}
34
35
export const DEFAULT_CONTEXT = {
36
pageWidthPx: 1000, // gets updated
37
pageStyle: calcStyle(false), // gets updated
38
formatIntl: () => "Loading…",
39
displayI18N: (label: string | IntlMessage | ReactNode) => {
40
// Fallback when intl is not available
41
if (isIntlMessage(label)) {
42
return label.defaultMessage;
43
}
44
return label;
45
},
46
showActBarLabels: ACTIVITY_BAR_LABELS_DEFAULT,
47
};
48
49
export const AppContext = createContext<AppState>(DEFAULT_CONTEXT);
50
51
export default function useAppContext() {
52
return useContext(AppContext);
53
}
54
55
export function calcStyle(isNarrow: boolean): PageStyle {
56
const fontSizeIcons: string = isNarrow
57
? FONT_SIZE_ICONS_NARROW
58
: FONT_SIZE_ICONS_NORMAL;
59
const topPaddingIcons: string = isNarrow ? "2px" : "5px";
60
const sidePaddingIcons: string = isNarrow ? "7px" : "14px";
61
62
const height = isNarrow ? NAV_HEIGHT_NARROW_PX : NAV_HEIGHT_PX;
63
64
const topBarStyle = {
65
height: `${height}px`,
66
background: "#fafafa",
67
} as const;
68
69
const fileUseStyle = {
70
background: "white",
71
border: `2px solid ${COLORS.GRAY_DDD}`,
72
borderRadius: "5px",
73
boxShadow: "0 0 15px #aaa",
74
fontSize: "10pt",
75
height: "90%",
76
margin: 0,
77
overflowX: "hidden",
78
overflowY: "auto",
79
padding: "4px",
80
position: "fixed",
81
right: "5vw",
82
top: `${height}px`,
83
width: isNarrow ? "90vw" : "50vw",
84
zIndex: 110,
85
} as const;
86
87
const projectsNavStyle = isNarrow
88
? ({
89
/* this makes it so the projects tabs are on a separate row; otherwise, there is literally no room for them at all... */
90
width: "100vw",
91
marginTop: "4px",
92
height: `${height}px`,
93
// no flex!
94
} as const)
95
: ({
96
flex: "1 1 auto", // necessary to stretch out to the full width
97
} as const);
98
99
return {
100
topBarStyle,
101
fileUseStyle,
102
projectsNavStyle,
103
isNarrow,
104
sidePaddingIcons,
105
topPaddingIcons,
106
fontSizeIcons,
107
height,
108
};
109
}
110
111