-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathProjectDownload.tsx
More file actions
151 lines (138 loc) · 6.04 KB
/
Copy pathProjectDownload.tsx
File metadata and controls
151 lines (138 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { faFileArrowDown, faBook } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import React, { useState, useEffect } from 'react';
import { Downloads } from '../types/downloads';
import { Column, Columns } from './Columns';
import { Grid } from './Grid';
interface ProjectDownloadProps {
projectId: string;
description?: React.ReactNode;
setup?: string;
downloadsInfo: {
[key: string]: string | React.ReactNode;
};
additionalDownloads?: {
[key: string]: { url: string, file: string };
};
gridColumns?: number;
warning?: React.ReactNode;
}
export const ProjectDownload: React.FC<ProjectDownloadProps> = ({ projectId, description, setup, downloadsInfo, additionalDownloads, gridColumns, warning }) => {
const [platformInfo, setPlatformInfo] = useState<Downloads.Builds>({
project_id: '',
project_name: '',
version: '',
builds: []
});
const [latestBuild, setLatestBuild] = useState<Downloads.Build>({
project_id: '',
project_name: '',
version: '',
build: 0,
time: new Date().toLocaleDateString(),
channel: '',
promoted: false,
changes: [],
downloads: {}
});
const [changes, setChanges] = useState<Downloads.BaseBuild['changes']>([]);
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
const fetchPlatformInfo = async () => {
try {
const response = await fetch(new URL(`/v2/projects/${projectId}/versions/latest/builds`, 'https://download.geysermc.org'));
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data: Downloads.Builds = await response.json();
setPlatformInfo(data);
setLatestBuild(data.builds[data.builds.length - 1]);
const allChanges: Downloads.BaseBuild['changes'] = [];
data.builds.forEach(build => {
allChanges.push(...build.changes);
});
setChanges(allChanges.reverse().slice(0, 12));
setIsLoaded(true);
} catch (error) {
console.error('Failed to fetch platform info:', error);
}
};
fetchPlatformInfo();
}, [projectId]);
const fadeInStyle = {
opacity: isLoaded ? 1 : 0,
transition: 'opacity 1s ease-in-out',
};
return (
<div style={fadeInStyle}>
<p>{description}</p>
<Columns>
<Column>
<h3>Build #{latestBuild.build} · {new Date(latestBuild.time).toLocaleDateString()}:</h3>
<Grid elementsPerRow={gridColumns || 2} gap="8px">
{ warning &&
<div className='warning-box'>
{ warning }
</div>
}
{ setup &&
<a href={setup} className='no-underline setup-button large-button'>
<b><FontAwesomeIcon icon={faBook}/> Setup Instructions</b>
</a>
}
{
Object.keys(latestBuild.downloads).map((platformId, i) => {
return (
<a href={`https://download.geysermc.org/v2/projects/${projectId}/versions/latest/builds/latest/downloads/${platformId}`} key={i} className='no-underline download-button large-button'>
<b>{downloadsInfo[platformId]}</b>
<div>
<FontAwesomeIcon icon={faFileArrowDown}/> {latestBuild.downloads[platformId].name}
</div>
</a>
)
})
}
{
additionalDownloads && Object.keys(additionalDownloads).map((platformId, i) => {
return (
<a href={additionalDownloads[platformId].url} key={i} className='no-underline download-button large-button'>
<b>{downloadsInfo[platformId]}</b>
<div>
<FontAwesomeIcon icon={faFileArrowDown}/> {additionalDownloads[platformId].file}
</div>
</a>
)
})
}
</Grid>
</Column>
<Column>
<h3>Recent Changes:</h3>
<ul>
{
changes.map((change, i) => {
return (
<li key={i}><b>{
<a className='download-link' href={`https://github.com/GeyserMC/${platformInfo.project_name}/commit/${change.commit}`}>{change.commit.substring(0, 7)}</a>
}</b> · {<LinkedCommitMessage message={change.summary} repo={platformInfo.project_name}/>}</li>
)
})
}
</ul>
</Column>
</Columns>
</div>
);
};
function LinkedCommitMessage({ message, repo }) {
const regex = /#(\d+)/g;
const parts = message.split(/(#\d+)/).filter((part: string) => part);
const linked = parts.map((part, index) => {
if (part.match(regex)) {
const number = part.replace('#', '');
return <a key={index} href={`https://github.com/GeyserMC/${repo}/pull/${number}`}>{part}</a>;
}
return part;
});
return <>{linked}</>;
}