/
Versions.tsx
182 lines (164 loc) · 4.87 KB
/
Versions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, {
useContext,
useEffect,
useState,
useRef,
type ReactNode,
} from 'react';
import {useDocsPreferredVersion} from '@docusaurus/theme-common/internal';
import {useVersions} from '@docusaurus/plugin-content-docs/client';
import Translate from '@docusaurus/Translate';
import CodeBlock from '@theme/CodeBlock';
type ContextValue = {
name: string;
time: string | undefined;
};
const Context = React.createContext<ContextValue | null>(null);
export function VersionsProvider({
children,
}: {
children: ReactNode;
}): JSX.Element {
const [canaryVersion, setCanaryVersion] = useState<ContextValue | null>(null);
const mounted = useRef(true);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
useEffect(() => {
fetch('https://registry.npmjs.org/@docusaurus/core')
.then((res) => res.json())
.then(
(data: {versions: string[]; time: {[versionName: string]: string}}) => {
if (!mounted.current) {
return;
}
const name = Object.keys(data.versions).at(-1)!;
const time = data.time[name];
setCanaryVersion({name, time});
},
);
}, []);
return <Context.Provider value={canaryVersion}>{children}</Context.Provider>;
}
function useStableVersion(): string {
const preferredVersion =
useDocsPreferredVersion('default').preferredVersion?.name;
const allVersions = useVersions('default');
const lastVersion = (
allVersions.find((v) => v.name !== 'current') ?? allVersions[0]!
).name;
return preferredVersion && preferredVersion !== 'current'
? preferredVersion
: lastVersion;
}
export function CanaryVersion(): JSX.Element {
const canaryVersion = useContext(Context);
// Show a sensible name
return canaryVersion ? (
<span>
<Translate
description="The hint text for the current canary version tag."
values={{canaryVersionName: <b>{canaryVersion.name}</b>}}>
{'Current: {canaryVersionName}'}
</Translate>
</span>
) : (
<span>
<Translate description="An example canary version tag when the actual version can't be fetched.">
Example: 0.0.0-4922
</Translate>
</span>
);
}
export function StableVersion(): JSX.Element {
const currentVersion = useStableVersion();
return <span>{currentVersion}</span>;
}
// TODO temporary: assumes main is already on v3 (not the case yet)
function useNextMajorVersionNumber(): number {
return 3; // TODO later read from main@package.json or something...
}
function useStableMajorVersionNumber(): number {
// -1 because website is published from main, which is the next version
return useNextMajorVersionNumber() - 1;
}
export function NextMajorVersion(): JSX.Element {
const majorVersionNumber = useNextMajorVersionNumber();
return <span>{majorVersionNumber}</span>;
}
export function StableMajorVersion(): JSX.Element {
const majorVersionNumber = useStableMajorVersionNumber();
return <span>{majorVersionNumber}</span>;
}
function GitBranchLink({branch}: {branch: string}): JSX.Element {
return (
<a href={`https://github.com/facebook/docusaurus/tree/${branch}`}>
<code>{branch}</code>
</a>
);
}
export function StableMajorBranchLink(): JSX.Element {
const majorVersionNumber = useStableMajorVersionNumber();
const branch = `docusaurus-v${majorVersionNumber}`;
return <GitBranchLink branch={branch} />;
}
export function NextMajorBranchLink(): JSX.Element {
return <GitBranchLink branch="main" />;
}
export function InsertIfCanaryVersionUnknown({
children,
}: {
children: ReactNode;
}): ReactNode | null {
const canaryVersion = useContext(Context);
if (!canaryVersion) {
return children;
}
return null;
}
export function InsertIfCanaryVersionKnown({
children,
}: {
children: ReactNode;
}): ReactNode | null {
const canaryVersion = useContext(Context);
if (canaryVersion) {
return children;
}
return null;
}
export function PackageJSONDiff(): JSX.Element {
const canaryVersion = useContext(Context)?.name ?? '0.0.0-4922';
const stableVersion = useStableVersion();
return (
<CodeBlock language="diff">
{`- "@docusaurus/core": "^${stableVersion}",
- "@docusaurus/preset-classic": "^${stableVersion}",
+ "@docusaurus/core": "${canaryVersion}",
+ "@docusaurus/preset-classic": "${canaryVersion}",
`}
</CodeBlock>
);
}
export function PublishTime(): JSX.Element | null {
const time = useContext(Context)?.time;
if (!time) {
return null;
}
return (
<Translate values={{time: <b>{new Date(time).toLocaleString()}</b>}}>
{
"The latest canary version that's available on npm is published at {time}."
}
</Translate>
);
}