-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): persist docs preferred version #3543
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5a3c38e
persist docs preferred version
slorber 40f626f
add proper implementation for useDocsPreferredVersion
slorber 9896fb1
add proper implementation for useDocsPreferredVersion
slorber babc278
useDocsPreferredVersion => make localstorage read only after mount
slorber 645e7fe
why @docusaurus/constants can't work?
slorber 94da3c8
Merge branch 'master' into slorber/persist-docs-preferred-version
slorber 2c1b7b4
fix weird TS issue when not duplicating constants
slorber 959938b
add basic @docusaurus/constants doc
slorber fc53a04
attempt to fix docs-only mode where we should not call useDocs hooks
slorber 6191bb0
attempt to fix docs-only mode where we should not call useDocs hooks
slorber 80d1fba
fix children
slorber 15424bb
encapsulate hacky isDocsPluginEnabled in docsUtils
slorber d9c4d9b
use same priority order for all navbar items: activeVersion ?? prefer…
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
.../docusaurus-theme-classic/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* 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, { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import useThemeConfig, {DocsVersionPersistence} from '../useThemeConfig'; | ||
|
||
import {useAllDocsData} from '@theme/hooks/useDocs'; | ||
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage'; | ||
|
||
type DocsPreferredVersionName = string | null; | ||
|
||
// State for a single docs plugin instance | ||
type DocsPreferredVersionPluginState = { | ||
preferredVersionName: DocsPreferredVersionName; | ||
}; | ||
|
||
// We need to store in state/storage globally | ||
// one preferred version per docs plugin instance | ||
// pluginId => pluginState | ||
type DocsPreferredVersionState = Record< | ||
string, | ||
DocsPreferredVersionPluginState | ||
>; | ||
|
||
// Initial state is always null as we can't read localstorage from node SSR | ||
function getInitialState(pluginIds: string[]): DocsPreferredVersionState { | ||
const initialState: DocsPreferredVersionState = {}; | ||
pluginIds.forEach((pluginId) => { | ||
initialState[pluginId] = { | ||
preferredVersionName: null, | ||
}; | ||
}); | ||
return initialState; | ||
} | ||
|
||
// Read storage for all docs plugins | ||
// Assign to each doc plugin a preferred version (if found) | ||
function readStorageState({ | ||
pluginIds, | ||
versionPersistence, | ||
allDocsData, | ||
}: { | ||
pluginIds: string[]; | ||
versionPersistence: DocsVersionPersistence; | ||
allDocsData: any; // TODO find a way to type it :( | ||
}): DocsPreferredVersionState { | ||
// The storage value we read might be stale, | ||
// and belong to a version that does not exist in the site anymore | ||
// In such case, we remove the storage value to avoid downstream errors | ||
function restorePluginState( | ||
pluginId: string, | ||
): DocsPreferredVersionPluginState { | ||
const preferredVersionNameUnsafe = DocsPreferredVersionStorage.read( | ||
pluginId, | ||
versionPersistence, | ||
); | ||
const pluginData = allDocsData[pluginId]; | ||
const versionExists = pluginData.versions.some( | ||
(version) => version.name === preferredVersionNameUnsafe, | ||
); | ||
if (versionExists) { | ||
return {preferredVersionName: preferredVersionNameUnsafe}; | ||
} else { | ||
DocsPreferredVersionStorage.clear(pluginId, versionPersistence); | ||
return {preferredVersionName: null}; | ||
} | ||
} | ||
|
||
const initialState: DocsPreferredVersionState = {}; | ||
pluginIds.forEach((pluginId) => { | ||
initialState[pluginId] = restorePluginState(pluginId); | ||
}); | ||
return initialState; | ||
} | ||
|
||
function useVersionPersistence(): DocsVersionPersistence { | ||
return useThemeConfig().docs.versionPersistence; | ||
} | ||
|
||
// Value that will be accessible through context: [state,api] | ||
function useContextValue() { | ||
const allDocsData = useAllDocsData(); | ||
const versionPersistence = useVersionPersistence(); | ||
const pluginIds = useMemo(() => Object.keys(allDocsData), [allDocsData]); | ||
|
||
// Initial state is empty, as we can't read browser storage in node/SSR | ||
const [state, setState] = useState(() => getInitialState(pluginIds)); | ||
|
||
// On mount, we set the state read from browser storage | ||
useEffect(() => { | ||
setState(readStorageState({allDocsData, versionPersistence, pluginIds})); | ||
}, [allDocsData, versionPersistence, pluginIds]); | ||
|
||
// The API that we expose to consumer hooks (memo for constant object) | ||
const api = useMemo(() => { | ||
function savePreferredVersion(pluginId: string, versionName: string) { | ||
DocsPreferredVersionStorage.save( | ||
pluginId, | ||
versionPersistence, | ||
versionName, | ||
); | ||
setState((s) => ({ | ||
...s, | ||
[pluginId]: {preferredVersionName: versionName}, | ||
})); | ||
} | ||
|
||
return { | ||
savePreferredVersion, | ||
}; | ||
}, [setState]); | ||
|
||
return [state, api] as const; | ||
} | ||
|
||
type DocsPreferredVersionContextValue = ReturnType<typeof useContextValue>; | ||
|
||
const Context = createContext<DocsPreferredVersionContextValue | null>(null); | ||
|
||
export default function DocsPreferredVersionContextProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}) { | ||
const contextValue = useContextValue(); | ||
return <Context.Provider value={contextValue}>{children}</Context.Provider>; | ||
} | ||
|
||
export function useDocsPreferredVersionContext(): DocsPreferredVersionContextValue { | ||
const value = useContext(Context); | ||
if (!value) { | ||
throw new Error( | ||
"Can't find docs preferred context, maybe you forgot to use the DocsPreferredVersionContextProvider ?", | ||
); | ||
} | ||
return value; | ||
} |
45 changes: 45 additions & 0 deletions
45
...es/docusaurus-theme-classic/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* 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 {DocsVersionPersistence} from '../useThemeConfig'; | ||
|
||
const storageKey = (pluginId: string) => `docs-preferred-version-${pluginId}`; | ||
|
||
const DocsPreferredVersionStorage = { | ||
save: ( | ||
pluginId: string, | ||
persistence: DocsVersionPersistence, | ||
versionName: string, | ||
): void => { | ||
if (persistence === 'none') { | ||
// noop | ||
} else { | ||
window.localStorage.setItem(storageKey(pluginId), versionName); | ||
} | ||
}, | ||
|
||
read: ( | ||
pluginId: string, | ||
persistence: DocsVersionPersistence, | ||
): string | null => { | ||
if (persistence === 'none') { | ||
return null; | ||
} else { | ||
return window.localStorage.getItem(storageKey(pluginId)); | ||
} | ||
}, | ||
|
||
clear: (pluginId: string, persistence: DocsVersionPersistence): void => { | ||
if (persistence === 'none') { | ||
// noop | ||
} else { | ||
window.localStorage.removeItem(storageKey(pluginId)); | ||
} | ||
}, | ||
}; | ||
|
||
export default DocsPreferredVersionStorage; |
36 changes: 36 additions & 0 deletions
36
packages/docusaurus-theme-classic/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 {useCallback} from 'react'; | ||
import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider'; | ||
import {useDocsData} from '@theme/hooks/useDocs'; | ||
|
||
// TODO why this import does not work! | ||
// import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants'; | ||
const DEFAULT_PLUGIN_ID = 'default'; | ||
|
||
// Note, the preferredVersion attribute will always be null before mount | ||
export default function useDocsPreferredVersion( | ||
pluginId: string | undefined = DEFAULT_PLUGIN_ID, | ||
) { | ||
const docsData = useDocsData(pluginId); | ||
const [state, api] = useDocsPreferredVersionContext(); | ||
|
||
const {preferredVersionName} = state[pluginId]; | ||
|
||
const preferredVersion = preferredVersionName | ||
? docsData.versions.find((version) => version.name === preferredVersionName) | ||
: null; | ||
|
||
const savePreferredVersionName = useCallback( | ||
(versionName: string) => { | ||
api.savePreferredVersion(pluginId, versionName); | ||
}, | ||
[api], | ||
); | ||
|
||
return {preferredVersion, savePreferredVersionName} as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no idea why, but unable to make this work