import axios from 'axios' import React, { useEffect, useState } from 'react' import spacetime from 'spacetime' import deployIcon from './netlify-icon' import { Button, Badge, Box, Card, Flex, Inline, Label, Spinner, Text, } from '@sanity/ui' import DeployStatus from './deploy-status' import type { Deployments, SanityDeploySchema } from './types' import { EyeOpenIcon } from '@sanity/icons' interface DeployHistoryProps extends Omit {} const DeployHistory: React.FC = ({ siteId, accessToken, onlyShowProductionDeploys }) => { const [deployments, setDeployments] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(false) useEffect(() => { setLoading(true) const params: any = {} if (onlyShowProductionDeploys) params.production = true; axios .get( `https://api.netlify.com/api/v1/sites/${siteId}/deploys`, { headers: { 'content-type': 'application/json', Authorization: `Bearer ${accessToken}`, }, params } ) .then(({ data }) => { setDeployments(data) setLoading(false) setError(false) }) .catch((e) => { setLoading(false) setError(true) console.warn(e) }) }, [siteId, accessToken]) const formatDeployTime = (totalSeconds: number) => { const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return `${minutes}m ${seconds}s` } if (loading) { return ( loading deployment history... ) } if (error) { return ( Could not load deployments for {siteId} ) } return ( {deployments?.map((deployment) => ( {deployment.branch} { (deployment.commit_url) && ( {deployment.commit_ref?.substring(0, 7)} ) || '–' } { (deployment.deploy_time && formatDeployTime(deployment.deploy_time) ) || '–' } {spacetime.now().since(spacetime(deployment.created_at)).rounded}