@@ -3,25 +3,22 @@ import kebabCase from 'lodash/kebabCase';
3
3
4
4
import { useLocation } from 'sentry/utils/useLocation' ;
5
5
import { useNavigate } from 'sentry/utils/useNavigate' ;
6
+ import { useParams } from 'sentry/utils/useParams' ;
6
7
7
8
import { useStoryBookFilesByCategory } from './storySidebar' ;
8
- import type { StoryCategory } from './storyTree' ;
9
+ import type { StoryCategory , StoryTreeNode } from './storyTree' ;
9
10
10
11
type LegacyStoryQuery = {
11
12
name : string ;
12
- category ?: never ;
13
- topic ?: never ;
14
13
} ;
15
- type NewStoryQuery = {
16
- category : StoryCategory ;
17
- topic : string ;
18
- name ?: never ;
19
- } ;
20
-
21
- type StoryQuery = LegacyStoryQuery | NewStoryQuery ;
14
+ interface StoryParams {
15
+ storySlug : string ;
16
+ storyType : StoryCategory ;
17
+ }
22
18
23
19
export function useStoryRedirect ( ) {
24
- const location = useLocation < StoryQuery > ( ) ;
20
+ const location = useLocation < LegacyStoryQuery > ( ) ;
21
+ const params = useParams < StoryParams > ( ) ;
25
22
const navigate = useNavigate ( ) ;
26
23
const stories = useStoryBookFilesByCategory ( ) ;
27
24
@@ -33,69 +30,60 @@ export function useStoryRedirect() {
33
30
if ( ! location . pathname . startsWith ( '/stories' ) ) {
34
31
return ;
35
32
}
36
- const story = getStoryMeta ( location . query , stories ) ;
33
+ const story = getStory ( stories , { query : location . query , params } ) ;
37
34
if ( ! story ) {
38
35
return ;
39
36
}
40
- if ( story . category === 'shared' ) {
41
- navigate (
42
- { pathname : `/stories/` , search : `?name=${ encodeURIComponent ( story . path ) } ` } ,
43
- { replace : true , state : { storyPath : story . path } }
44
- ) ;
45
- } else {
46
- navigate (
47
- { pathname : `/stories/${ story . category } /${ kebabCase ( story . label ) } ` } ,
48
- { replace : true , state : { storyPath : story . path } }
49
- ) ;
50
- }
51
- } , [ location , navigate , stories ] ) ;
37
+ const { state, ...to } = story . location ;
38
+ navigate ( to , { replace : true , state} ) ;
39
+ } , [ location , params , navigate , stories ] ) ;
52
40
}
53
41
54
- interface StoryMeta {
55
- category : StoryCategory ;
56
- label : string ;
57
- path : string ;
42
+ interface StoryRouteContext {
43
+ params : StoryParams ;
44
+ query : LegacyStoryQuery ;
58
45
}
59
46
60
- function getStoryMeta (
61
- query : StoryQuery ,
62
- stories : ReturnType < typeof useStoryBookFilesByCategory >
47
+ function getStory (
48
+ stories : ReturnType < typeof useStoryBookFilesByCategory > ,
49
+ context : StoryRouteContext
63
50
) {
64
- if ( query . name ) {
65
- return legacyGetStoryMetaFromQuery ( query , stories ) ;
51
+ if ( context . params . storyType && context . params . storySlug ) {
52
+ return getStoryFromParams ( stories , context ) ;
66
53
}
67
- if ( query . category && query . topic ) {
68
- return getStoryMetaFromQuery ( query , stories ) ;
54
+ if ( context . query . name ) {
55
+ return legacyGetStoryFromQuery ( stories , context ) ;
69
56
}
70
57
return undefined ;
71
58
}
72
59
73
- function legacyGetStoryMetaFromQuery (
74
- query : LegacyStoryQuery ,
75
- stories : ReturnType < typeof useStoryBookFilesByCategory >
76
- ) : StoryMeta | undefined {
60
+ function legacyGetStoryFromQuery (
61
+ stories : ReturnType < typeof useStoryBookFilesByCategory > ,
62
+ context : StoryRouteContext
63
+ ) : StoryTreeNode | undefined {
77
64
for ( const category of Object . keys ( stories ) as StoryCategory [ ] ) {
78
65
const nodes = stories [ category ] ;
79
66
for ( const node of nodes ) {
80
- const match = node . find ( n => n . filesystemPath === query . name ) ;
67
+ const match = node . find ( n => n . filesystemPath === context . query . name ) ;
81
68
if ( match ) {
82
- return { category , label : match . label , path : match . filesystemPath } ;
69
+ return match ;
83
70
}
84
71
}
85
72
}
86
73
return undefined ;
87
74
}
88
75
89
- function getStoryMetaFromQuery (
90
- query : NewStoryQuery ,
91
- stories : ReturnType < typeof useStoryBookFilesByCategory >
92
- ) : StoryMeta | undefined {
93
- const { category, topic} = query ;
94
- const nodes = category in stories ? stories [ category ] : [ ] ;
76
+ function getStoryFromParams (
77
+ stories : ReturnType < typeof useStoryBookFilesByCategory > ,
78
+ context : StoryRouteContext
79
+ ) : StoryTreeNode | undefined {
80
+ const { storyType : category , storySlug} = context . params ;
81
+ const nodes =
82
+ category && category in stories ? stories [ category as keyof typeof stories ] : [ ] ;
95
83
for ( const node of nodes ) {
96
- const match = node . find ( n => kebabCase ( n . label ) === topic ) ;
84
+ const match = node . find ( n => kebabCase ( n . label ) === storySlug ) ;
97
85
if ( match ) {
98
- return { category , label : match . label , path : match . filesystemPath } ;
86
+ return match ;
99
87
}
100
88
}
101
89
return undefined ;
0 commit comments