Skip to content

Commit 8ce141f

Browse files
committed
feat: new copy button in case of raw record view in AccessionView
1 parent 3eb6b96 commit 8ce141f

1 file changed

Lines changed: 78 additions & 50 deletions

File tree

web-frontend/src/elements/routes/pages/accession/AccessionView.tsx

Lines changed: 78 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { JSX, useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import RecordView from '../../../record/RecordView';
33
import Record from '../../../../types/record/Record';
4-
import { Spin } from 'antd';
4+
import { Button, Spin } from 'antd';
55
import useContainerDimensions from '../../../../utils/useContainerDimensions';
66
import { Content } from 'antd/es/layout/layout';
77
import { useSearchParams } from 'react-router-dom';
88
import { usePropertiesContext } from '../../../../context/properties/properties';
99
import getRecord from '../../../../utils/request/fetchRecord';
1010
import Text from 'antd/es/typography/Text';
11+
import { CopyOutlined } from '@ant-design/icons';
1112
import fetchRawMassBankRecord from '../../../../utils/request/fetchRawMassBankRecord';
13+
import copyTextToClipboard from '../../../../utils/copyTextToClipboard';
14+
15+
const toolButtonStyle = {
16+
width: '40px',
17+
border: 'none',
18+
boxShadow: 'none',
19+
backgroundColor: 'rgb(225, 231, 245)',
20+
marginLeft: 5,
21+
marginRight: 5,
22+
overallWidth: '50px',
23+
};
1224

1325
function AccessionView() {
1426
const ref = useRef(null);
@@ -18,7 +30,7 @@ function AccessionView() {
1830
const [isRequesting, setIsRequesting] = useState<boolean>(false);
1931
const [requestedAccession, setRequestedAccession] = useState<string>('');
2032
const [record, setRecord] = useState<Record | undefined>();
21-
const [rawTextElements, setRawTextElements] = useState<JSX.Element[]>([]);
33+
const [rawText, setRawText] = useState<string | null>(null);
2234
const [searchParams] = useSearchParams();
2335

2436
const accession = searchParams.get('id');
@@ -50,37 +62,30 @@ function AccessionView() {
5062
setRequestedAccession(acc);
5163

5264
if (raw) {
53-
let _rawTextElements: JSX.Element[] = [];
65+
let _rawText: string | null = null;
5466
try {
5567
const rawMassBankRecordText = await fetchRawMassBankRecord(
5668
exportServiceUrl,
5769
acc,
5870
);
5971
if (rawMassBankRecordText) {
60-
_rawTextElements = (rawMassBankRecordText as string)
61-
.split(/\n/g)
62-
.map((line: string, i: number) => (
63-
<label key={line + '_' + i}>
64-
{line}
65-
<br />
66-
</label>
67-
));
72+
_rawText = rawMassBankRecordText as string;
6873
} else {
69-
_rawTextElements = [notFoundElement];
74+
_rawText = null;
7075
}
7176
// eslint-disable-next-line @typescript-eslint/no-unused-vars
7277
} catch (error) {
7378
// console.error('Error fetching raw MassBank record:', error);
74-
_rawTextElements = [notFoundElement];
79+
_rawText = null;
7580
}
76-
setRawTextElements(_rawTextElements);
81+
setRawText(_rawText);
7782
} else {
7883
setRecord(await getRecord(acc, backendUrl));
7984
}
8085

8186
setIsRequesting(false);
8287
},
83-
[backendUrl, exportServiceUrl, notFoundElement],
88+
[backendUrl, exportServiceUrl],
8489
);
8590

8691
useEffect(() => {
@@ -93,45 +98,68 @@ function AccessionView() {
9398
}
9499
}, [accession, handleOnSearch, showRaw]);
95100

96-
const recordView = useMemo(
97-
() =>
98-
showRaw && rawTextElements.length > 0 ? (
99-
<Content
101+
const handleOnCopy = useCallback(() => {
102+
copyTextToClipboard('Record Text', rawText ? rawText : '');
103+
}, [rawText]);
104+
105+
const recordView = useMemo(() => {
106+
let rawTextElements: JSX.Element[] = [];
107+
if (rawText) {
108+
rawTextElements = (rawText as string)
109+
.split(/\n/g)
110+
.map((line: string, i: number) => (
111+
<label key={line + '_' + i}>
112+
{line}
113+
<br />
114+
</label>
115+
));
116+
} else {
117+
rawTextElements = [notFoundElement];
118+
}
119+
return showRaw && rawText && rawText.length > 0 ? (
120+
<Content
121+
style={{
122+
width,
123+
height,
124+
display: 'flex',
125+
justifyContent: 'center',
126+
alignItems: 'center',
127+
}}
128+
>
129+
<Button
130+
children={
131+
<CopyOutlined title="Copy MassBank record data to clipboard" />
132+
}
133+
onClick={() => handleOnCopy()}
134+
style={toolButtonStyle}
135+
/>
136+
<Text
100137
style={{
101-
width,
138+
width: `calc(100% - ${toolButtonStyle.overallWidth})`,
102139
height,
103-
display: 'flex',
104-
justifyContent: 'center',
105-
alignItems: 'center',
140+
overflow: 'scroll',
141+
padding: 10,
142+
backgroundColor: 'white',
106143
}}
107144
>
108-
<Text
109-
style={{
110-
width,
111-
height,
112-
overflow: 'scroll',
113-
padding: 10,
114-
backgroundColor: 'white',
115-
}}
116-
>
117-
{rawTextElements}
118-
</Text>
119-
</Content>
120-
) : record ? (
121-
<RecordView record={record} width={width} height={height} />
122-
) : requestedAccession !== '' ? (
123-
notFoundElement
124-
) : undefined,
125-
[
126-
showRaw,
127-
rawTextElements,
128-
width,
129-
height,
130-
record,
131-
requestedAccession,
132-
notFoundElement,
133-
],
134-
);
145+
{rawTextElements}
146+
</Text>
147+
</Content>
148+
) : record ? (
149+
<RecordView record={record} width={width} height={height} />
150+
) : requestedAccession !== '' ? (
151+
notFoundElement
152+
) : undefined;
153+
}, [
154+
rawText,
155+
showRaw,
156+
width,
157+
height,
158+
record,
159+
requestedAccession,
160+
notFoundElement,
161+
handleOnCopy,
162+
]);
135163

136164
return useMemo(
137165
() => (

0 commit comments

Comments
 (0)