Skip to content

Commit 34dc95e

Browse files
committed
fix: add custom meta descriptions
1 parent a9f04ca commit 34dc95e

File tree

3 files changed

+84
-14
lines changed

3 files changed

+84
-14
lines changed

src/pages/light-item-details/index.js

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { LIGHT_ITEM_DETAILS_QUERY } from 'utils/graphql'
2222
import { useQuery } from '@apollo/client'
2323
import SearchBar from 'components/light-search-bar'
2424
import { parseIpfs } from 'utils/ipfs-parse'
25+
import { itemToStatusCode, STATUS_CODE } from 'utils/item-status'
26+
import { truncateAtWord } from 'utils/truncate-at-word'
2527
import { fetchMetaEvidence } from 'hooks/tcr-view'
2628

2729
export const ITEM_TOUR_DISMISSED = 'ITEM_TOUR_DISMISSED'
@@ -78,7 +80,9 @@ const ItemDetails = ({ itemID, search }) => {
7880
const [ipfsItemData, setIpfsItemData] = useState()
7981
const { timestamp } = useContext(WalletContext)
8082
const [modalOpen, setModalOpen] = useState()
81-
const { tcrError, metaEvidence } = useContext(LightTCRViewContext)
83+
const { tcrError, metaEvidence, challengePeriodDuration } = useContext(
84+
LightTCRViewContext
85+
)
8286
const [appealCost, setAppealCost] = useState()
8387

8488
// subgraph item entities have id "<itemID>@<listaddress>"
@@ -114,7 +118,7 @@ const ItemDetails = ({ itemID, search }) => {
114118
return ordered
115119
}
116120

117-
const result = {
121+
return {
118122
...item, // Spread to convert from array to object.
119123
errors: [],
120124
columns: metaEvidence.metadata.columns,
@@ -123,11 +127,62 @@ const ItemDetails = ({ itemID, search }) => {
123127
ipfsItemData.values
124128
)
125129
}
126-
return result
127130
}, [item, metaEvidence, ipfsItemData])
128131

129132
const { metadata } = metaEvidence || {}
130133
const { decodedData } = decodedItem || {}
134+
const { tcrTitle, itemName } = metadata || {}
135+
136+
const statusCode = useMemo(() => {
137+
if (!item || !timestamp || !challengePeriodDuration) return null
138+
return itemToStatusCode(item, timestamp, challengePeriodDuration)
139+
}, [item, timestamp, challengePeriodDuration])
140+
141+
const getStatusPhrase = statusCode => {
142+
switch (statusCode) {
143+
case STATUS_CODE.REGISTERED:
144+
return 'is verified to be safe'
145+
case STATUS_CODE.SUBMITTED:
146+
return 'is pending verification'
147+
case STATUS_CODE.REMOVAL_REQUESTED:
148+
return 'has removal requested'
149+
case STATUS_CODE.CHALLENGED:
150+
return 'is under challenge'
151+
case STATUS_CODE.CROWDFUNDING:
152+
return 'is crowdfunding appeal'
153+
case STATUS_CODE.CROWDFUNDING_WINNER:
154+
return 'won crowdfunding appeal'
155+
case STATUS_CODE.PENDING_SUBMISSION:
156+
return 'awaits submission'
157+
case STATUS_CODE.PENDING_REMOVAL:
158+
return 'awaits removal'
159+
case STATUS_CODE.WAITING_ARBITRATOR:
160+
return 'awaits arbitrator ruling'
161+
case STATUS_CODE.ABSENT:
162+
return 'is not listed'
163+
default:
164+
return 'has unknown status'
165+
}
166+
}
167+
168+
const capitalizeFirst = s => s?.charAt(0).toUpperCase() + s?.slice(1)
169+
170+
const fullSeoTitle =
171+
decodedItem && metadata
172+
? `${capitalizeFirst(itemName)} - ${tcrTitle} - Kleros · Curate`
173+
: 'Kleros · Curate'
174+
const truncatedSeoTitle = truncateAtWord(fullSeoTitle, 160)
175+
176+
const fullSeoMetaDescription =
177+
decodedItem && metadata && statusCode !== null
178+
? `${decodedData.join(' ')} - ${getStatusPhrase(statusCode)} on ${
179+
metadata.tcrTitle
180+
} in Kleros Curate`
181+
: 'View item details on Kleros Curate.'
182+
const truncatedSeoMetaDescription = truncateAtWord(
183+
fullSeoMetaDescription,
184+
160
185+
)
131186

132187
// If this is a TCR in a TCR of TCRs, we fetch its metadata as well
133188
// to build a better item details card.
@@ -197,17 +252,11 @@ const ItemDetails = ({ itemID, search }) => {
197252
/>
198253
)
199254

200-
const { tcrTitle, itemName } = metadata || {}
201-
const capitalizeFirst = s => s.charAt(0).toUpperCase() + s.slice(1)
202-
const title =
203-
decodedItem && metadata
204-
? `${capitalizeFirst(itemName)} - ${tcrTitle} - Kleros · Curate`
205-
: 'Kleros · Curate'
206-
207255
return (
208256
<>
209257
<Helmet>
210-
<title>{title}</title>
258+
<title>{truncatedSeoTitle}</title>
259+
<meta name="description" content={truncatedSeoMetaDescription} />
211260
</Helmet>
212261
<StyledBanner>
213262
<Breadcrumb separator=">">

src/pages/light-items/banner.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useWeb3Context } from 'web3-react'
1010
import ContractExplorerUrl from 'components/contract-explorer-url'
1111
import { defaultTcrAddresses } from 'config/tcr-addresses'
1212
import { parseIpfs } from 'utils/ipfs-parse'
13+
import { truncateAtWord } from 'utils/truncate-at-word'
1314

1415
export const StyledBanner = styled.div`
1516
display: flex;
@@ -121,12 +122,24 @@ const Banner = ({
121122
: `${tcrDescription}.`
122123
: ''
123124

125+
const fullSeoTitle = tcrTitle
126+
? `${tcrTitle} - Kleros · Curate`
127+
: 'Kleros · Curate'
128+
const truncatedSeoTitle = truncateAtWord(fullSeoTitle, 60)
129+
130+
const fullSeoMetaDescription = metadata
131+
? `Explore the ${tcrTitle} list on Kleros Curate: ${normalizedDescription}`
132+
: 'Explore curated lists on Kleros Curate.'
133+
const truncatedSeoMetaDescription = truncateAtWord(
134+
fullSeoMetaDescription,
135+
160
136+
)
137+
124138
return (
125139
<>
126140
<Helmet>
127-
<title>
128-
{tcrTitle ? `${tcrTitle} - Kleros · Curate` : 'Kleros · Curate'}
129-
</title>
141+
<title> {truncatedSeoTitle} </title>
142+
<meta name="description" content={truncatedSeoMetaDescription} />
130143
</Helmet>
131144
<StyledBanner>
132145
<TCRInfoColumn id="tcr-info-column">

src/utils/truncate-at-word.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const truncateAtWord = (text, maxLength) => {
2+
if (text.length <= maxLength) return text
3+
const truncated = text.substring(0, maxLength)
4+
const lastSpace = truncated.lastIndexOf(' ')
5+
return lastSpace > 0
6+
? `${truncated.substring(0, lastSpace)}...`
7+
: `${truncated}...`
8+
}

0 commit comments

Comments
 (0)