-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.Toast--info { | ||
background-color: var(--aqua-800) !important; | ||
} | ||
|
||
.Toast--warn { | ||
background-color: var(--orange-800) !important; | ||
} | ||
|
||
.Toast--error { | ||
background-color: var(--red-800) !important; | ||
} |
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,113 @@ | ||
import React, { useEffect, useState } from 'react' | ||
|
||
import { Toast, ToastType, Toasts } from 'decentraland-ui' | ||
import fetch from 'isomorphic-fetch' | ||
|
||
import { getQueryTimestamp } from '../../clients/SnapshotGraphql' | ||
import { SNAPSHOT_SPACE } from '../../entities/Snapshot/constants' | ||
import Time from '../../utils/date/Time' | ||
|
||
import './SnapshotPing.css' | ||
|
||
async function pingSnapshot(): Promise<number> { | ||
const startTime = new Date().getTime() | ||
try { | ||
const query = ` | ||
query getVotes($space: String!, $start: Int!, $end: Int!, $first: Int!) { | ||
votes(where: {space: $space, created_gte: $start, created_lt: $end}, orderBy: "created", orderDirection: asc, first: $first) { | ||
id | ||
voter | ||
created | ||
vp | ||
choice | ||
proposal { | ||
id | ||
choices | ||
} | ||
} | ||
} | ||
` | ||
|
||
await fetch('https://hub.snapshot.org/graphql/', { | ||
method: 'post', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
query, | ||
variables: { | ||
space: SNAPSHOT_SPACE, | ||
first: 10, | ||
start: 1684534478, | ||
end: getQueryTimestamp(Time().getTime()), | ||
}, | ||
}), | ||
}) | ||
|
||
const endTime = new Date().getTime() | ||
const responseTime = endTime - startTime | ||
console.log('responseTime', responseTime) | ||
return responseTime // Calculate the response time in milliseconds | ||
} catch (error) { | ||
return -1 // Return -1 to indicate API failure | ||
} | ||
} | ||
|
||
const getToastType = (serviceStatus: ServiceStatus) => { | ||
switch (serviceStatus) { | ||
case 'slow': | ||
return ToastType.WARN | ||
case 'failing': | ||
return ToastType.ERROR | ||
default: | ||
return ToastType.INFO | ||
} | ||
} | ||
|
||
type ServiceStatus = 'normal' | 'slow' | 'failing' | ||
|
||
function getMessage(serviceStatus: ServiceStatus) { | ||
switch (serviceStatus) { | ||
case 'slow': | ||
return 'Voting and creating proposals might take some time' | ||
case 'failing': | ||
return 'Voting and creating proposals is currently unavailable' | ||
default: | ||
return 'All good!' | ||
} | ||
} | ||
|
||
export default function SnapshotPing() { | ||
const [serviceStatus, setServiceStatus] = useState<ServiceStatus>('normal') | ||
const [showToast, setShowToast] = useState(false) | ||
|
||
const updateServiceStatus = async () => { | ||
const responseTime = await pingSnapshot() | ||
if (responseTime === -1) { | ||
setServiceStatus('failing') | ||
} else if (responseTime > 200) { | ||
setServiceStatus('slow') | ||
} else { | ||
setServiceStatus('failing') | ||
} | ||
setShowToast(true) | ||
} | ||
|
||
useEffect(() => { | ||
// const intervalId = setInterval(updateServiceStatus, PING_INTERVAL) | ||
// return () => clearInterval(intervalId) | ||
updateServiceStatus() | ||
}, []) | ||
|
||
return ( | ||
<Toasts position="top right"> | ||
{showToast && ( | ||
<Toast | ||
title={`Snapshot is ${serviceStatus}`} | ||
className={`Toast--${getToastType(serviceStatus)}`} | ||
body={getMessage(serviceStatus)} | ||
closable | ||
onClose={() => setShowToast(false)} | ||
/> | ||
)} | ||
</Toasts> | ||
) | ||
} |
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