-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(stories): support pretty URLs #95365
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
300f999
feat(stories): allow deep /stories routes
natemoo-re 123d8bc
feat(stories): configure /stories redirects
natemoo-re 8736808
fix(stories): support new URL scheme in tree nodes
natemoo-re 382b486
fix(stories): use kebab-case for URLs
natemoo-re eeaa29b
ref(stories): remove typecast
natemoo-re f1249eb
fix(stories): avoid redirect flicker
natemoo-re 581ff39
fix(stories): protect against unknown categories
natemoo-re f67384f
Merge branch 'master' into stories/nm/routes
natemoo-re 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
import {useEffect} from 'react'; | ||
import kebabCase from 'lodash/kebabCase'; | ||
|
||
import {useStoryBookFilesByCategory} from 'sentry/stories/view/storySidebar'; | ||
import {useLocation} from 'sentry/utils/useLocation'; | ||
import {useNavigate} from 'sentry/utils/useNavigate'; | ||
|
||
type LegacyStoryQuery = { | ||
name: string; | ||
category?: never; | ||
topic?: never; | ||
}; | ||
type NewStoryQuery = { | ||
category: StoryCategory; | ||
topic: string; | ||
name?: never; | ||
}; | ||
|
||
type StoryQuery = LegacyStoryQuery | NewStoryQuery; | ||
|
||
export function useStoryRedirect() { | ||
const location = useLocation<StoryQuery>(); | ||
const navigate = useNavigate(); | ||
const stories = useStoryBookFilesByCategory(); | ||
|
||
useEffect(() => { | ||
// If we already have a `storyPath` in state, bail out | ||
if (location.state?.storyPath ?? location.query.name) { | ||
return; | ||
} | ||
if (!location.pathname.startsWith('/stories')) { | ||
return; | ||
} | ||
const story = getStoryMeta(location.query, stories); | ||
if (!story) { | ||
return; | ||
} | ||
if (story.category === 'shared') { | ||
navigate( | ||
{pathname: `/stories/`, search: `?name=${encodeURIComponent(story.path)}`}, | ||
{replace: true, state: {storyPath: story.path}} | ||
); | ||
} else { | ||
navigate( | ||
{pathname: `/stories/${story.category}/${kebabCase(story.label)}`}, | ||
{replace: true, state: {storyPath: story.path}} | ||
); | ||
} | ||
}, [location, navigate, stories]); | ||
} | ||
|
||
type StoryCategory = keyof ReturnType<typeof useStoryBookFilesByCategory>; | ||
interface StoryMeta { | ||
category: StoryCategory; | ||
label: string; | ||
path: string; | ||
} | ||
|
||
function getStoryMeta( | ||
query: StoryQuery, | ||
stories: ReturnType<typeof useStoryBookFilesByCategory> | ||
) { | ||
if (query.name) { | ||
return legacyGetStoryMetaFromQuery(query, stories); | ||
} | ||
if (query.category && query.topic) { | ||
return getStoryMetaFromQuery(query, stories); | ||
} | ||
return undefined; | ||
} | ||
|
||
function legacyGetStoryMetaFromQuery( | ||
query: LegacyStoryQuery, | ||
stories: ReturnType<typeof useStoryBookFilesByCategory> | ||
): StoryMeta | undefined { | ||
for (const category of Object.keys(stories) as StoryCategory[]) { | ||
const nodes = stories[category]; | ||
for (const node of nodes) { | ||
const match = node.find(n => n.filesystemPath === query.name); | ||
if (match) { | ||
return {category, label: match.label, path: match.filesystemPath}; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
function getStoryMetaFromQuery( | ||
query: NewStoryQuery, | ||
stories: ReturnType<typeof useStoryBookFilesByCategory> | ||
): StoryMeta | undefined { | ||
const {category, topic} = query; | ||
const nodes = category in stories ? stories[category] : []; | ||
for (const node of nodes) { | ||
const match = node.find(n => kebabCase(n.label) === topic); | ||
if (match) { | ||
return {category, label: match.label, path: match.filesystemPath}; | ||
} | ||
} | ||
return undefined; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.