11import { useEffect , Dispatch , SetStateAction , useState } from "react" ;
22import { Box , Button , CircularProgress , Link , Typography } from "@mui/material" ;
3+ import OpenInNewIcon from "@mui/icons-material/OpenInNew" ;
34import { useMutation } from "react-query" ;
45
56import { Spacer } from "@atoms" ;
67import { useTranslation } from "@hooks" ;
78import { VoteContextWrapper } from "@organisms" ;
89import { postIpfs } from "@services" ;
9- import { downloadTextFile } from "@utils" ;
10+ import { downloadTextFile , openInNewTab } from "@utils" ;
1011import { NodeObject } from "jsonld" ;
1112import { UseFormSetValue } from "react-hook-form" ;
1213import { VoteContextFormValues } from "@hooks" ;
14+ import { LINKS } from "@/consts/links" ;
15+ import { ICONS } from "@/consts/icons" ;
16+ import { useSnackbar } from "@context" ;
17+ import { copyToClipboard } from "@utils" ;
1318
1419interface PostIpfsResponse {
15- ipfsHash : string ;
20+ ipfsCid : string ;
1621}
1722
1823type VoteContextGovToolProps = {
@@ -34,24 +39,29 @@ export const VoteContextGovTool = ({
3439 metadataHash,
3540 setValue,
3641} : VoteContextGovToolProps ) => {
37- const [ apiResponse , setApiResponse ] = useState < string | null > ( null ) ;
42+ const [ apiResponse , setApiResponse ] = useState < PostIpfsResponse | null > ( null ) ;
43+ const [ uploadInitiated , setUploadInitiated ] = useState ( false ) ; // New state to track upload
3844 const { t } = useTranslation ( ) ;
45+ const { addSuccessAlert } = useSnackbar ( ) ;
46+
47+ const openLink = ( ) => openInNewTab ( LINKS . STORING_INFORMATION_OFFLINE ) ;
3948
4049 const { mutate, isLoading } = useMutation < PostIpfsResponse , Error , { content : string } > ( {
4150 mutationFn : postIpfs ,
4251 onSuccess : ( data ) => {
43- const ipfsUrl = `ipfs://${ data . ipfsHash } ` ;
52+ const ipfsUrl = `ipfs://${ data . ipfsCid } ` ;
4453 setValue ( "storingURL" , ipfsUrl ) ;
4554 setSavedHash ( metadataHash ) ; // Set savedHash to metadataHash
46- setApiResponse ( JSON . stringify ( data , null , 2 ) ) ;
55+ setApiResponse ( data ) ;
4756 } ,
4857 } ) ;
4958
5059 useEffect ( ( ) => {
51- if ( jsonldContent ) {
60+ if ( jsonldContent && ! uploadInitiated ) {
5261 mutate ( { content : JSON . stringify ( jsonldContent , null , 2 ) } ) ;
62+ setUploadInitiated ( true ) ; // Set flag after initiating upload
5363 }
54- } , [ jsonldContent , mutate ] ) ;
64+ } , [ jsonldContent , mutate , uploadInitiated ] ) ;
5565
5666 const handleDownload = ( ) => {
5767 if ( jsonldContent ) {
@@ -66,7 +76,33 @@ export const VoteContextGovTool = ({
6676 onCancel = { onCancel }
6777 >
6878 < Typography sx = { { textAlign : "center" } } variant = "h4" >
69- { t ( "createGovernanceAction.letGovToolStore" ) }
79+ { t ( "createGovernanceAction.rationalePinnedToIPFS" ) }
80+ </ Typography >
81+ < Link
82+ onClick = { openLink }
83+ sx = { {
84+ cursor : "pointer" ,
85+ fontSize : 16 ,
86+ fontWeight : 500 ,
87+ fontFamily : "Poppins" ,
88+ my : 4 ,
89+ textAlign : "center" ,
90+ textDecoration : "none" ,
91+ } }
92+ >
93+ { t ( "createGovernanceAction.readFullGuide" ) }
94+ < OpenInNewIcon
95+ sx = { {
96+ color : "primary" ,
97+ height : 17 ,
98+ width : 17 ,
99+ ml : 0.5 ,
100+ verticalAlign : "middle" ,
101+ } }
102+ />
103+ </ Link >
104+ < Typography sx = { { textAlign : "center" } } variant = "h5" >
105+ { t ( "createGovernanceAction.recommendations" ) }
70106 </ Typography >
71107 < Spacer y = { 4 } />
72108 { isLoading ? (
@@ -75,13 +111,38 @@ export const VoteContextGovTool = ({
75111 </ Box >
76112 ) : apiResponse ? (
77113 < >
78- < Typography sx = { { whiteSpace : "pre-wrap" , textAlign : "left" } } >
79- { apiResponse }
114+ < Typography fontWeight = { 400 } sx = { { textAlign : "center" } } variant = "body1" >
115+ { t ( "createGovernanceAction.downloadAndStoreMetadataFile" ) }
116+ </ Typography >
117+ < Button
118+ data-testid = "metadata-download-button"
119+ onClick = { handleDownload }
120+ size = "large"
121+ startIcon = { < img alt = "download" src = { ICONS . download } /> }
122+ sx = { { width : "fit-content" , alignSelf : "center" , my : 4 } }
123+ variant = "outlined"
124+ >
125+ { t ( "govActions.voteContextFileName" ) }
126+ </ Button >
127+ < Typography sx = { { textAlign : "center" } } variant = "body1" >
128+ { t ( "createGovernanceAction.rePinYourFile" ) }
80129 </ Typography >
81- < Spacer y = { 2 } />
82- < Link onClick = { handleDownload } sx = { { cursor : "pointer" } } >
83- { t ( "createGovernanceAction.downloadJsonLd" ) }
84- </ Link >
130+ < Box sx = { { display : "flex" , alignItems : "center" , justifyContent : "center" , gap : 1 } } >
131+ < Typography sx = { { textAlign : "center" } } variant = "body1" >
132+ { apiResponse . ipfsCid ? `ipfs://${ apiResponse . ipfsCid } ` : "[URI]" }
133+ </ Typography >
134+ { apiResponse . ipfsCid && (
135+ < Link
136+ onClick = { ( ) => {
137+ copyToClipboard ( `ipfs://${ apiResponse . ipfsCid } ` ) ;
138+ addSuccessAlert ( t ( "alerts.copiedToClipboard" ) ) ;
139+ } }
140+ sx = { { cursor : "pointer" , display : "flex" , alignItems : "center" } }
141+ >
142+ < img alt = "copy" src = { ICONS . copyBlueIcon } style = { { width : 16 , height : 16 } } />
143+ </ Link >
144+ ) }
145+ </ Box >
85146 </ >
86147 ) : (
87148 < Typography sx = { { textAlign : "center" } } variant = "body1" >
0 commit comments