-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new field to vote details with long text form IPFS based on…
… CID in vote metadata
- Loading branch information
Showing
9 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getUrlFromCID } from 'modules/shared/utils/getUrlFromCID' | ||
|
||
export const DEFAULT_PARAMS = { | ||
method: 'GET', | ||
headers: { | ||
'Content-type': 'text/plain', | ||
}, | ||
} | ||
|
||
type FetcherIPFS = (cid: string, params?: RequestInit) => Promise<string> | ||
|
||
export const fetcherIPFS: FetcherIPFS = async ( | ||
cid, | ||
params = DEFAULT_PARAMS, | ||
) => { | ||
const response = await fetch(getUrlFromCID(cid), params) | ||
|
||
if (!response.ok) { | ||
throw new Error('An error occurred while fetching the data.') | ||
} | ||
|
||
const data = await response.text() | ||
return data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// could be moved to env if needed | ||
const CIDPrefix = 'https://' | ||
const CIDSuffix = '.ipfs.w3s.link' | ||
|
||
export const getUrlFromCID = (cid: string) => `${CIDPrefix}${cid}${CIDSuffix}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* IPFS has 2 cid formats v0 and v1, v1 supports different encoding options: | ||
* CIDv0: | ||
* base58btc Qm | ||
* CIDv1: | ||
* base16 f | ||
* base16upper F | ||
* hexadecimal base32 b | ||
* base32upper B | ||
* base58btc z | ||
* base64 m | ||
* base64url u | ||
* base64urlpad U | ||
*/ | ||
|
||
export const REGEX_CID = | ||
/(Qm[1-9A-HJ-NP-Za-km-z]{44,128}|b[A-Za-z2-7]{58,128}|B[A-Z2-7]{58,128}|z[1-9A-HJ-NP-Za-km-z]{48,128}|F[0-9A-F]{50,128})$/g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
modules/votes/ui/VoteMetadataIPFSDescription/VoteMetadataIPFSDescription.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DescriptionText } from './VoteMetadataIPFSDescriptionStyle' | ||
import { replaceJsxElements } from 'modules/shared/utils/replaceLinksWithComponents' | ||
import { useSWR } from 'modules/network/hooks/useSwr' | ||
import { fetcherIPFS } from 'modules/network/utils/fetcherIPFS' | ||
import { InlineLoader } from '@lidofinance/lido-ui' | ||
|
||
type Props = { | ||
cid: string | ||
} | ||
|
||
export function VoteMetadataIPFSDescription({ cid }: Props) { | ||
const { data = '', error, initialLoading } = useSWR(cid, fetcherIPFS) | ||
if (initialLoading) { | ||
return <InlineLoader /> | ||
} | ||
if (error) { | ||
return ( | ||
<DescriptionText color="red"> | ||
⚠️ Failed to load vote content, please try again later. | ||
</DescriptionText> | ||
) | ||
} | ||
return <DescriptionText>{replaceJsxElements(data)}</DescriptionText> | ||
} |
8 changes: 8 additions & 0 deletions
8
modules/votes/ui/VoteMetadataIPFSDescription/VoteMetadataIPFSDescriptionStyle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import styled from 'styled-components' | ||
|
||
export const DescriptionText = styled.div` | ||
hyphens: auto; | ||
overflow-wrap: anywhere; | ||
word-break: break-word; | ||
white-space: pre-wrap; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './VoteMetadataIPFSDescription' |