MCPcopy Create free account
hub / github.com/PageAI-Pro/page-ui / useCopyBrickCode

Function useCopyBrickCode

components/bricks/useCopyBrickCode.tsx:4–43  ·  view source on GitHub ↗
({ demo, brick })

Source from the content-addressed store, hash-verified

2import { useCallback, useEffect, useState } from 'react';
3
4export const useCopyBrickCode = ({ demo, brick }) => {
5 const [formattedCodeString, setFormattedCodeString] = useState('');
6 const [loading, setLoading] = useState(false);
7 const [errorString, setErrorString] = useState('');
8
9 const getCode = useCallback(
10 async (signal?: AbortSignal) => {
11 try {
12 setLoading(true);
13 setErrorString('');
14
15 const response = await axios.get(
16 `/api/get-brick?demo=${encodeURIComponent(demo)}&brick=${brick}`,
17 ...(signal ? [{ signal }] : []),
18 );
19
20 if (response.status === 200) {
21 setFormattedCodeString(response.data.code);
22 } else {
23 setErrorString('Oops! Something went wrong. Please try again.');
24 }
25
26 return response.data.code;
27 } catch (error) {
28 if (!axios.isCancel(error)) {
29 setErrorString(
30 'There was an error fetching the code. Please try again.',
31 );
32 }
33
34 return '';
35 }
36
37 setLoading(false);
38 },
39 [demo, setFormattedCodeString, setLoading, setErrorString, brick],
40 );
41
42 return { formattedCodeString, loading, errorString, getCode };
43};

Callers 2

BrickCopyCodeFunction · 0.90
BrickCodeEditorFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected