@@ -3,32 +3,35 @@ import { Button, FlareIcon } from '@flareapp/ignition-ui';
3
3
import Checkbox from 'components/ui/Checkbox' ;
4
4
import { IgniteDataContext } from '../contexts/IgniteDataContext' ;
5
5
import CopyableUrl from './ui/CopyableUrl' ;
6
+ import shareClient , { SectionName } from '../shareClient' ;
6
7
7
8
type Props = {
8
9
isOpen : boolean ;
9
10
} ;
10
11
11
- type SectionName = 'stackTrace' | 'context' | 'debug' ;
12
-
13
12
export default function ShareDropdown ( { isOpen } : Props ) {
14
13
const igniteData = useContext ( IgniteDataContext ) ;
15
14
const [ ownerUrl , setOwnerUrl ] = useState < string | null > ( null ) ;
16
15
const [ publicUrl , setPublicUrl ] = useState < string | null > ( null ) ;
17
16
const [ error , setError ] = useState < string | null > ( null ) ;
18
17
const [ isLoading , setIsLoading ] = useState ( false ) ;
19
18
20
- const [ selectedTabs , setSelectedTabs ] = useState < Array < { name : SectionName ; label : string ; selected : boolean } > > ( [
19
+ const [ selectedSections , setSelectedSections ] = useState <
20
+ Array < { name : SectionName ; label : string ; selected : boolean } >
21
+ > ( [
21
22
{ name : 'stackTrace' , label : 'Stack' , selected : true } ,
22
23
{ name : 'context' , label : 'Context' , selected : true } ,
23
24
{ name : 'debug' , label : 'Debug' , selected : true } ,
24
25
] ) ;
25
26
26
- function toggleSelected ( tabName : SectionName ) {
27
- const tab = selectedTabs . find ( ( tab ) => tab . name === tabName ) ;
27
+ function toggleSelected ( sectionName : SectionName ) {
28
+ const section = selectedSections . find ( ( section ) => section . name === sectionName ) ;
28
29
29
- if ( tab ) {
30
- setSelectedTabs (
31
- selectedTabs . map ( ( tab ) => ( tab . name === tabName ? { ...tab , selected : ! tab . selected } : tab ) ) ,
30
+ if ( section ) {
31
+ setSelectedSections (
32
+ selectedSections . map ( ( section ) =>
33
+ section . name === sectionName ? { ...section , selected : ! section . selected } : section ,
34
+ ) ,
32
35
) ;
33
36
}
34
37
}
@@ -38,40 +41,23 @@ export default function ShareDropdown({ isOpen }: Props) {
38
41
return ;
39
42
}
40
43
41
- const selectedTabNames = selectedTabs
42
- . filter ( ( selectedTab ) => selectedTab . selected )
43
- . map ( ( selectedTab ) => selectedTab . name ) ;
44
-
45
- const data = {
46
- selectedTabNames,
47
- tabs : selectedTabNames ,
48
- lineSelection : window . location . hash ,
49
- report : igniteData . shareableReport ,
50
- } ;
51
-
44
+ setError ( null ) ;
52
45
setIsLoading ( true ) ;
53
46
47
+ const selectedSectionNames = selectedSections
48
+ . filter ( ( selectedSection ) => selectedSection . selected )
49
+ . map ( ( selectedSection ) => selectedSection . name ) ;
50
+
54
51
try {
55
- const response = await (
56
- await fetch ( igniteData . config . shareEndpoint , {
57
- method : 'POST' ,
58
- body : JSON . stringify ( data ) ,
59
- headers : {
60
- 'Content-Type' : 'application/json' ,
61
- Accept : 'application/json' ,
62
- } ,
63
- } )
64
- ) . json ( ) ;
52
+ const response = await shareClient ( igniteData , selectedSectionNames ) ;
65
53
66
- if ( response && response . owner_url && response . public_url ) {
67
- setOwnerUrl ( response . owner_url ) ;
68
- setPublicUrl ( response . public_url ) ;
69
- }
70
- } catch ( error ) {
54
+ setOwnerUrl ( response . owner_url ) ;
55
+ setPublicUrl ( response . public_url ) ;
56
+ } catch ( e ) {
71
57
setError ( 'Something went wrong while sharing, please try again.' ) ;
72
- } finally {
73
- setIsLoading ( false ) ;
74
58
}
59
+
60
+ setIsLoading ( false ) ;
75
61
}
76
62
77
63
return (
@@ -97,7 +83,7 @@ export default function ShareDropdown({ isOpen }: Props) {
97
83
{ ! publicUrl && (
98
84
< >
99
85
< ul className = "grid justify-start gap-3" >
100
- { selectedTabs . map ( ( { selected, name, label } ) => (
86
+ { selectedSections . map ( ( { selected, name, label } ) => (
101
87
< li key = { name } >
102
88
< Checkbox onChange = { ( ) => toggleSelected ( name ) } checked = { selected } label = { label } />
103
89
</ li >
@@ -106,7 +92,7 @@ export default function ShareDropdown({ isOpen }: Props) {
106
92
107
93
< div className = "flex items-center gap-4" >
108
94
< Button
109
- disabled = { isLoading }
95
+ disabled = { isLoading || ! selectedSections . some ( ( section ) => section . selected ) }
110
96
className = { 'bg-violet-500 border-violet-500/25 CopyButton text-white' }
111
97
onClick = { onShareError }
112
98
>
0 commit comments