generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
ShareButton.tsx
151 lines (136 loc) · 7.21 KB
/
ShareButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useState } from 'react';
import Downshift from 'downshift';
import { ErrorOccurrence, SharePostData, Tabname } from '../types';
import CopyableCode from 'resources/js/shared/components/CopyableCode';
import Button from 'resources/js/shared/components/Button';
import CheckboxField from 'resources/js/shared/components/CheckboxField';
import axios from 'axios';
import Icon from 'resources/js/shared/components/Icon';
type Props = {
children: React.ReactChild | Array<React.ReactChild>;
errorOccurrence: ErrorOccurrence;
disabled?: boolean;
manageSharesUrl?: string;
};
/* @todo share button is different in ignition than in flare */
export default function ShareButton({ children, errorOccurrence, disabled = false, manageSharesUrl }: Props) {
const [sharedUrl, setSharedUrl] = useState('');
const [error, setError] = useState('');
const [selectedTabs, setSelectedTabs] = useState<Array<{ name: Tabname; prettyName: string; selected: boolean }>>([
{ name: 'stackTraceTab', prettyName: 'Stack trace', selected: true },
{ name: 'requestTab', prettyName: 'Request', selected: true },
{ name: 'appTab', prettyName: 'App', selected: true },
{ name: 'userTab', prettyName: 'User', selected: true },
{ name: 'contextTab', prettyName: 'Context', selected: true },
{ name: 'debugTab', prettyName: 'Debug', selected: true },
]);
function toggleTabSelected(
tabName: 'stackTraceTab' | 'requestTab' | 'appTab' | 'userTab' | 'contextTab' | 'debugTab',
) {
const tab = selectedTabs.find((tab) => tab.name === tabName);
if (tab) {
setSelectedTabs(
selectedTabs.map((tab) => (tab.name === tabName ? { ...tab, selected: !tab.selected } : tab)),
);
}
}
async function onShareError() {
const endpoint = errorOccurrence.links.share;
const selectedTabNames = selectedTabs
.filter((selectedTab) => selectedTab.selected)
.map((selectedTab) => selectedTab.name);
const data: SharePostData = { selectedTabNames, lineSelection: window.location.hash };
try {
const { data: response } = await axios.post(endpoint, data);
if (response && response.shared_error && response.shared_error.links && response.shared_error.links.show) {
setSharedUrl(response.shared_error.links.show);
}
} catch (error) {
setError('Something went wrong while sharing, please try again.');
}
}
return (
<Downshift>
{({ getItemProps, getMenuProps, getLabelProps, isOpen, toggleMenu }) => (
<div>
<label {...getLabelProps({ className: 'hidden', htmlFor: undefined })}>Share options menu</label>
<div>
<button
disabled={disabled}
className={`tab flex items-center ${isOpen ? 'tab-active' : ''}`}
onClick={() => toggleMenu()}
>
{children}
<Icon name="share" className="ml-2" />
</button>
{isOpen && (
<ul
{...getMenuProps()}
className="dropdown z-10 right-0 top-full bg-gray-700 text-white p-4 overflow-visible"
style={{ minWidth: '18rem', marginRight: '-1px' }}
>
<h5 className="mb-3 text-left text-gray-500 font-semibold uppercase tracking-wider whitespace-nowrap">
Share publicly
</h5>
<div className="grid grid-cols-2 justify-start gap-x-6 gap-y-2">
{selectedTabs.map(({ selected, name, prettyName }) => (
<CheckboxField
{...getItemProps({ item: name, key: name })}
labelClassName="text-gray-200 hover:text-white"
onChange={() =>
toggleTabSelected(
name as
| 'stackTraceTab'
| 'requestTab'
| 'appTab'
| 'userTab'
| 'contextTab'
| 'debugTab',
)
}
checked={selected}
label={prettyName}
/>
))}
</div>
<div className="grid grid-cols-auto grid-flow-col justify-between items-center mt-3">
{!sharedUrl && (
<Button
secondary
className="bg-tint-600 text-white"
size="sm"
onClick={onShareError}
>
Create share
</Button>
)}
{manageSharesUrl && (
<a
className="link-dimmed-invers underline"
target="_blank"
href={manageSharesUrl}
>
Manage shares
</a>
)}
</div>
{error && <p className="mt-3 text-red-400">{error}</p>}
{sharedUrl && (
<div className="mt-3 flex">
<CopyableCode className="text-white max-w-xs sm:max-w-md overflow-x-auto overflow-y-hidden scrollbar">
{sharedUrl}
</CopyableCode>
<i
className="cursor-pointer p-2 pr-0 fas fa-external-link-alt text-xs"
onClick={() => window.open(sharedUrl)}
/>
</div>
)}
</ul>
)}
</div>
</div>
)}
</Downshift>
);
}