web/src/components/things/GitHubDetailedInfo.astro

287 lines · 7417 bytes

  1---
  2import type { GitHubStatsResponse } from '@utils/fetch-repo-info'
  3import { formatDate, timeAgo } from '@utils/dates-n-stuff'; 
  4
  5import FontAwesome from "@components/form/FontAwesome.svelte"
  6
  7interface Props {
  8  gitHubData: GitHubStatsResponse;
  9  repo: string;
 10}
 11
 12const { gitHubData, repo } = Astro.props;
 13
 14
 15function generateChartData(languages: Record<string, number>): string {
 16  const colorMap: { [key: string]: string } = {
 17    'TypeScript': '#2b7489', 
 18    'Objective-C++': '#6866fb', 
 19    'Jupyter Notebook': '#DA5B0B', 
 20    'Dart': '#00B4AB', 
 21    'Shell': '#89e051',  
 22    'Elixir': '#6e4a7e', 
 23    'Kotlin': '#7F52FF', 
 24    'Objective-C': '#438eff', 
 25    'Ruby': '#CC342D', 
 26    'Go': '#00ADD8', 
 27    'PHP': '#4F5D95', 
 28    'Java': '#b07219', 
 29    'Scala': '#DC322F', 
 30    'Makefile': '#427819', 
 31    'Perl': '#39457E', 
 32    'Lua': '#2C2D72', 
 33    'Vue': '#4FC08D', 
 34    'HTML': '#E34F26', 
 35    'Swift': '#F05138', 
 36    'C': '#A8B9CC',
 37    'C#': '#097c01',
 38    'Clojure': '#db5855', 
 39    'Rust': '#dea584', 
 40    'Assembly': '#6E4C13', 
 41    'Arduino': '#bd79d1', 
 42    'C Sharp': '#512BD4', 
 43    'CSS': '#563d7c', 
 44    'TeX': '#3D6117', 
 45    'OCaml': '#3be133', 
 46    'Pascal': '#b0ce4e', 
 47    'F#': '#b845fc', 
 48    'JavaScript': '#f1e05a', 
 49    'VimL': '#199f4b', 
 50    'R': '#198ce7', 
 51    'Erlang': '#B83998', 
 52    'Scheme': '#1e4aec', 
 53    'Python': '#3572A5', 
 54    'Common Lisp': '#3fb68b', 
 55    'Groovy': '#e69f56', 
 56    'Julia': '#a270ba', 
 57    'cpp': '#00599C',
 58    'C++': '#00599C',
 59    'Dockerfile': '#2496ED',
 60  };
 61
 62  // Arrays to hold the data, labels, and colors
 63  const data: number[] = [];
 64  const labels: string[] = [];
 65  const colors: string[] = [];
 66  
 67  // Default colors to use if the language is not in the colorMap
 68  const defaultColors = ['#ffdf60', '#5f53f4', '#28dffd', '#f45397'];
 69  let defaultColorIndex = 0;
 70
 71  // Populate the data, labels, and colors arrays
 72  for (const [language, lines] of Object.entries(languages)) {
 73    data.push(lines);
 74    labels.push(language);
 75    
 76    // Assign a color from the colorMap if available; otherwise, use a default color
 77    if (colorMap[language]) {
 78      colors.push(colorMap[language]);
 79    } else {
 80      colors.push(defaultColors[defaultColorIndex % defaultColors.length]);
 81      defaultColorIndex++;
 82    }
 83  }
 84
 85  // Construct the chart data object as a string
 86  const chartData = JSON.stringify({
 87    type: 'doughnut',
 88    data: {
 89      datasets: [{
 90        data: data,
 91        backgroundColor: colors,
 92        borderWidth: 0,
 93        label: 'Language Usage',
 94      }],
 95      labels: labels,
 96    },
 97    options: {
 98      cutoutPercentage: 80,
 99      legend: {
100        display: true,
101      },
102      plugins: {
103        datalabels: {
104          display: false,
105        },
106      },
107      aspectRatio: 1,
108      maintainAspectRatio: false,
109    },
110  });
111
112  // Encode the chart data for use in a URL
113  return encodeURIComponent(chartData);
114}
115
116const formatBigNumber = (num: number): string => {
117  if (!num) return 'None';
118  return num.toLocaleString();
119};
120
121
122
123
124const languageChart = `https://quickchart.io/chart?c=${generateChartData(gitHubData.languages)}`;
125
126
127---
128
129<div class="gh-info-wrap">
130
131  <div class="left">
132    <h4>Author</h4>
133    <p>
134      <img src={gitHubData.info.ownerAvatar} width="20" height="20" />
135      <a target="_blank" href={`https://github.com/${gitHubData.info.ownerUsername}`}>
136        {gitHubData.info.ownerUsername}
137      </a>
138    </p>
139    <h4>Description</h4>
140    <p>
141      {gitHubData.info.description}
142    </p>
143    <span class="tags">
144      {gitHubData.info.topics.map((topic) => (
145        <span class="tag">#{topic}</span>
146      ))}
147    </span>
148    <h4>Homepage</h4>
149    <a href={gitHubData.info.homepage}>{gitHubData.info.homepage}</a>
150    <h4>License</h4>
151    <p>{gitHubData.info.license}</p>
152    <h4>Created</h4>
153    <p title={timeAgo(gitHubData.info.createdAt)}>{formatDate(gitHubData.info.createdAt)}</p>
154    <h4>Last Updated</h4>
155    <p title={timeAgo(gitHubData.info.updatedAt)}>{formatDate(gitHubData.info.updatedAt)}</p>
156    { gitHubData.versions.length > 0 && (
157      <h4>Latest version</h4>
158      <p title={gitHubData.versions[0].commit}>
159        {gitHubData.versions[0].name}
160        <a href={gitHubData.versions[0].zipball}><FontAwesome iconName="download" /></a>
161      </p>
162    )}
163    <h4>Primary Language</h4>
164    <p>{gitHubData.info.language}</p>
165    <h4>Size</h4>
166    <p>{formatBigNumber(gitHubData.info.size)} KB</p>
167    <h4>Stars</h4>
168    <p>{formatBigNumber(gitHubData.info.scarCount)}</p>
169    <h4>Forks</h4>
170    <p>{formatBigNumber(gitHubData.info.forksCount)}</p>
171    <h4>Watchers</h4>
172    <p>{formatBigNumber(gitHubData.info.watchersCount)}</p>
173
174  </div>
175  <div class="right">
176    <h4>Language Usage</h4>
177    <img class="languages-img" width="600" src={languageChart} alt="Language Usage" />
178    <h4>Star History</h4>
179    <img class="languages-img" width="600" src={`https://api.star-history.com/svg?repos=${repo}&type=Date`} alt="Star History" />
180  </div>
181  <div class="long-list-data">
182    <div class="left list-wrap">
183      <h4>Top Contributors</h4>
184      <ul>
185        {gitHubData.contributors.map((contributor) => (
186          <li>
187            <img src={contributor.avatar} width="20" height="20" />
188            <a href={`https://github.com/${contributor.username}`} target="_blank">
189              @{contributor.username}
190              </a> ({contributor.contributions})
191          </li>
192        ))}
193      </ul>
194    </div>
195    <div class="right list-wrap">
196      <h4>Recent Commits</h4>
197      <ul>
198        {gitHubData.commits.map((commit) => (
199          <li>
200            <img src={commit.authorAvatar} width="20" height="20" />
201            <a href={`https://github.com/${commit.authorUsername}`} target="_blank">
202              {commit.authorName}
203            </a>
204            <span class="date" title={timeAgo(commit.authorDate)}>({formatDate(commit.authorDate)})</span>
205            <p class="commit-msg">{commit.message}</p>
206          </li>
207        ))}
208      </ul>
209    </div>
210  </div>
211</div>
212
213<style lang="scss">
214  .gh-info-wrap {
215    display: flex;
216    flex-wrap: wrap;
217    justify-content: space-between;
218    .left, .right {
219      width: 50%;
220      @media (max-width: 768px) {
221        width: 100%;
222      }
223    }
224    .long-list-data {
225      width: 100%;
226      display: flex;
227      flex-wrap: wrap;
228      justify-content: space-between;
229      .list-wrap {
230        .date {
231          font-size: 0.8rem;
232          opacity: 0.6;
233        }
234        .commit-msg {
235          font-size: 0.8rem;
236          opacity: 0.8;
237          display: -webkit-box;
238          -webkit-line-clamp: 2;
239          -webkit-box-orient: vertical;  
240          overflow: hidden;
241        }
242      }
243    }
244  }
245
246  h4 {
247    margin: 1rem 0 0 0;
248    font-size: 1.2rem;
249  }
250  p {
251    margin: 0;
252    display: flex;
253    align-items: center;
254    gap: 0.25rem;
255    :global(svg) {
256      width: 1rem;
257    }
258    img {
259      border-radius: var(--curve-sm);
260    }
261  }
262  .tags {
263    display: flex;
264    flex-wrap: wrap;
265    .tag {
266      margin: 0.1rem;
267      background: #5f53f440;
268      border-radius: var(--curve-sm);
269      padding: 0.05rem 0.2rem;
270      font-size: 0.8rem;
271      opacity: 0.8;
272    }
273  }
274  ul {
275    padding-left: 1rem;
276    list-style: circle;
277    max-height: 400px;
278    overflow: auto;
279    img {
280      border-radius: var(--curve-sm);
281    }
282  }
283  .languages-img {
284    width: 100%;
285    border-radius: var(--curve-sm);
286  }
287</style>