@@ -3,37 +3,30 @@ import type { Key } from 'react-aria';
3
3
import { OpenAPIEmptyExample } from './OpenAPIExample' ;
4
4
import { OpenAPISelect , OpenAPISelectItem , useSelectState } from './OpenAPISelect' ;
5
5
import { StaticSection } from './StaticSection' ;
6
- import { createStateKey } from './utils' ;
7
6
8
- type OpenAPIResponseMediaTypeItem = OpenAPISelectItem & {
7
+ type OpenAPIMediaTypeItem = OpenAPISelectItem & {
9
8
body : React . ReactNode ;
10
- examples ?: OpenAPIResponseMediaTypeItem [ ] ;
9
+ examples ?: OpenAPIMediaTypeItem [ ] ;
11
10
} ;
12
11
13
12
/**
14
13
* Get the state of the response examples select.
15
14
*/
16
- export function useResponseMediaTypesState (
17
- blockKey : string | undefined ,
18
- initialKey : Key = 'default'
19
- ) {
20
- return useSelectState ( getResponseMediaTypeStateKey ( blockKey ) , initialKey ) ;
15
+ export function useMediaTypesState ( stateKey : string | undefined , initialKey : Key = 'default' ) {
16
+ return useSelectState ( stateKey , initialKey ) ;
21
17
}
22
18
23
- function useResponseMediaTypeExamplesState (
24
- blockKey : string | undefined ,
25
- initialKey : Key = 'default'
26
- ) {
27
- return useSelectState ( getResponseMediaTypeExamplesStateKey ( blockKey ) , initialKey ) ;
19
+ function useMediaTypeExamplesState ( stateKey : string | undefined , initialKey : Key = 'default' ) {
20
+ return useSelectState ( stateKey , initialKey ) ;
28
21
}
29
22
30
- export function OpenAPIResponseMediaTypeContent ( props : {
31
- items : OpenAPIResponseMediaTypeItem [ ] ;
32
- blockKey ?: string ;
23
+ export function OpenAPIMediaTypeContent ( props : {
24
+ items : OpenAPIMediaTypeItem [ ] ;
33
25
selectIcon ?: React . ReactNode ;
26
+ stateKey : string ;
34
27
} ) {
35
- const { blockKey , items, selectIcon } = props ;
36
- const state = useResponseMediaTypesState ( blockKey , items [ 0 ] ?. key ) ;
28
+ const { stateKey , items, selectIcon } = props ;
29
+ const state = useMediaTypesState ( stateKey , items [ 0 ] ?. key ) ;
37
30
38
31
const examples = items . find ( ( item ) => item . key === state . key ) ?. examples ?? [ ] ;
39
32
@@ -45,36 +38,36 @@ export function OpenAPIResponseMediaTypeContent(props: {
45
38
< StaticSection
46
39
footer = {
47
40
items . length > 1 || examples . length > 1 ? (
48
- < OpenAPIResponseMediaTypeFooter
49
- blockKey = { blockKey }
41
+ < OpenAPIMediaTypeFooter
50
42
items = { items }
51
43
examples = { examples }
52
44
selectIcon = { selectIcon }
45
+ stateKey = { stateKey }
53
46
/>
54
47
) : null
55
48
}
56
49
className = "openapi-response-media-types-examples"
57
50
>
58
- < OpenAPIResponseMediaTypeBody blockKey = { blockKey } items = { items } examples = { examples } />
51
+ < OpenAPIMediaTypeBody stateKey = { stateKey } items = { items } examples = { examples } />
59
52
</ StaticSection >
60
53
) ;
61
54
}
62
55
63
- function OpenAPIResponseMediaTypeFooter ( props : {
64
- items : OpenAPIResponseMediaTypeItem [ ] ;
65
- examples ?: OpenAPIResponseMediaTypeItem [ ] ;
66
- blockKey ?: string ;
56
+ function OpenAPIMediaTypeFooter ( props : {
57
+ items : OpenAPIMediaTypeItem [ ] ;
58
+ examples ?: OpenAPIMediaTypeItem [ ] ;
67
59
selectIcon ?: React . ReactNode ;
60
+ stateKey : string ;
68
61
} ) {
69
- const { items, examples, blockKey , selectIcon } = props ;
62
+ const { items, examples, stateKey , selectIcon } = props ;
70
63
71
64
return (
72
65
< >
73
66
{ items . length > 1 && (
74
67
< OpenAPISelect
75
68
icon = { selectIcon }
76
69
items = { items }
77
- stateKey = { getResponseMediaTypeStateKey ( blockKey ) }
70
+ stateKey = { stateKey }
78
71
placement = "bottom start"
79
72
>
80
73
{ items . map ( ( item ) => (
@@ -89,7 +82,7 @@ function OpenAPIResponseMediaTypeFooter(props: {
89
82
< OpenAPISelect
90
83
icon = { selectIcon }
91
84
items = { examples }
92
- stateKey = { getResponseMediaTypeExamplesStateKey ( blockKey ) }
85
+ stateKey = { ` ${ stateKey } -examples` }
93
86
placement = "bottom start"
94
87
>
95
88
{ examples . map ( ( example ) => (
@@ -103,18 +96,18 @@ function OpenAPIResponseMediaTypeFooter(props: {
103
96
) ;
104
97
}
105
98
106
- function OpenAPIResponseMediaTypeBody ( props : {
107
- items : OpenAPIResponseMediaTypeItem [ ] ;
108
- examples ?: OpenAPIResponseMediaTypeItem [ ] ;
109
- blockKey ? : string ;
99
+ function OpenAPIMediaTypeBody ( props : {
100
+ items : OpenAPIMediaTypeItem [ ] ;
101
+ examples ?: OpenAPIMediaTypeItem [ ] ;
102
+ stateKey : string ;
110
103
} ) {
111
- const { blockKey , items, examples } = props ;
112
- const state = useResponseMediaTypesState ( blockKey , items [ 0 ] ?. key ) ;
104
+ const { stateKey , items, examples } = props ;
105
+ const state = useMediaTypesState ( stateKey , items [ 0 ] ?. key ) ;
113
106
114
107
const selectedItem = items . find ( ( item ) => item . key === state . key ) ?? items [ 0 ] ;
115
108
116
- const exampleState = useResponseMediaTypeExamplesState (
117
- blockKey ,
109
+ const exampleState = useMediaTypeExamplesState (
110
+ ` ${ stateKey } -examples` ,
118
111
selectedItem ?. examples ?. [ 0 ] ?. key
119
112
) ;
120
113
@@ -135,14 +128,3 @@ function OpenAPIResponseMediaTypeBody(props: {
135
128
136
129
return selectedItem . body ;
137
130
}
138
-
139
- /**
140
- * Return the state key for the response media types.
141
- */
142
- function getResponseMediaTypeStateKey ( blockKey : string | undefined ) {
143
- return createStateKey ( 'response-media-types' , blockKey ) ;
144
- }
145
-
146
- function getResponseMediaTypeExamplesStateKey ( blockKey : string | undefined ) {
147
- return createStateKey ( 'response-media-types-examples' , blockKey ) ;
148
- }
0 commit comments