web/src/components/things/GitHubDetailedInfo.astro

288 lines · 7440 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    'SCSS': '#CC6699',
 61  };
 62
 63  // Arrays to hold the data, labels, and colors
 64  const data: number[] = [];
 65  const labels: string[] = [];
 66  const colors: string[] = [];
 67  
 68  // Default colors to use if the language is not in the colorMap
 69  const defaultColors = ['#ffdf60', '#5f53f4', '#28dffd', '#f45397'];
 70  let defaultColorIndex = 0;
 71
 72  // Populate the data, labels, and colors arrays
 73  for (const [language, lines] of Object.entries(languages)) {
 74    data.push(lines);
 75    labels.push(language);
 76    
 77    // Assign a color from the colorMap if available; otherwise, use a default color
 78    if (colorMap[language]) {
 79      colors.push(colorMap[language]);
 80    } else {
 81      colors.push(defaultColors[defaultColorIndex % defaultColors.length]);
 82      defaultColorIndex++;
 83    }
 84  }
 85
 86  // Construct the chart data object as a string
 87  const chartData = JSON.stringify({
 88    type: 'doughnut',
 89    data: {
 90      datasets: [{
 91        data: data,
 92        backgroundColor: colors,
 93        borderWidth: 0,
 94        label: 'Language Usage',
 95      }],
 96      labels: labels,
 97    },
 98    options: {
 99      cutoutPercentage: 80,
100      legend: {
101        display: true,
102      },
103      plugins: {
104        datalabels: {
105          display: false,
106        },
107      },
108      aspectRatio: 1,
109      maintainAspectRatio: false,
110    },
111  });
112
113  // Encode the chart data for use in a URL
114  return encodeURIComponent(chartData);
115}
116
117const formatBigNumber = (num: number): string => {
118  if (!num) return 'None';
119  return num.toLocaleString();
120};
121
122
123
124
125const languageChart = `https://quickchart.io/chart?c=${generateChartData(gitHubData.languages)}`;
126
127
128---
129
130<div class="gh-info-wrap">
131
132  <div class="left">
133    <h4>Author</h4>
134    <p>
135      <img src={gitHubData.info.ownerAvatar} width="20" height="20" />
136      <a target="_blank" href={`https://github.com/${gitHubData.info.ownerUsername}`}>
137        {gitHubData.info.ownerUsername}
138      </a>
139    </p>
140    <h4>Description</h4>
141    <p>
142      {gitHubData.info.description}
143    </p>
144    <span class="tags">
145      {gitHubData.info.topics.map((topic) => (
146        <span class="tag">#{topic}</span>
147      ))}
148    </span>
149    <h4>Homepage</h4>
150    <a href={gitHubData.info.homepage}>{gitHubData.info.homepage}</a>
151    <h4>License</h4>
152    <p>{gitHubData.info.license}</p>
153    <h4>Created</h4>
154    <p title={timeAgo(gitHubData.info.createdAt)}>{formatDate(gitHubData.info.createdAt)}</p>
155    <h4>Last Updated</h4>
156    <p title={timeAgo(gitHubData.info.updatedAt)}>{formatDate(gitHubData.info.updatedAt)}</p>
157    { gitHubData.versions.length > 0 && (
158      <h4>Latest version</h4>
159      <p title={gitHubData.versions[0].commit}>
160        {gitHubData.versions[0].name}
161        <a href={gitHubData.versions[0].zipball}><FontAwesome iconName="download" /></a>
162      </p>
163    )}
164    <h4>Primary Language</h4>
165    <p>{gitHubData.info.language}</p>
166    <h4>Size</h4>
167    <p>{formatBigNumber(gitHubData.info.size)} KB</p>
168    <h4>Stars</h4>
169    <p>{formatBigNumber(gitHubData.info.scarCount)}</p>
170    <h4>Forks</h4>
171    <p>{formatBigNumber(gitHubData.info.forksCount)}</p>
172    <h4>Watchers</h4>
173    <p>{formatBigNumber(gitHubData.info.watchersCount)}</p>
174
175  </div>
176  <div class="right">
177    <h4>Language Usage</h4>
178    <img class="languages-img" width="600" src={languageChart} alt="Language Usage" />
179    <h4>Star History</h4>
180    <img class="languages-img" width="600" src={`https://api.star-history.com/svg?repos=${repo}&type=Date`} alt="Star History" />
181  </div>
182  <div class="long-list-data">
183    <div class="left list-wrap">
184      <h4>Top Contributors</h4>
185      <ul>
186        {gitHubData.contributors.map((contributor) => (
187          <li>
188            <img src={contributor.avatar} width="20" height="20" />
189            <a href={`https://github.com/${contributor.username}`} target="_blank">
190              @{contributor.username}
191              </a> ({contributor.contributions})
192          </li>
193        ))}
194      </ul>
195    </div>
196    <div class="right list-wrap">
197      <h4>Recent Commits</h4>
198      <ul>
199        {gitHubData.commits.map((commit) => (
200          <li>
201            <img src={commit.authorAvatar} width="20" height="20" />
202            <a href={`https://github.com/${commit.authorUsername}`} target="_blank">
203              {commit.authorName}
204            </a>
205            <span class="date" title={timeAgo(commit.authorDate)}>({formatDate(commit.authorDate)})</span>
206            <p class="commit-msg">{commit.message}</p>
207          </li>
208        ))}
209      </ul>
210    </div>
211  </div>
212</div>
213
214<style lang="scss">
215  .gh-info-wrap {
216    display: flex;
217    flex-wrap: wrap;
218    justify-content: space-between;
219    .left, .right {
220      width: 50%;
221      @media (max-width: 768px) {
222        width: 100%;
223      }
224    }
225    .long-list-data {
226      width: 100%;
227      display: flex;
228      flex-wrap: wrap;
229      justify-content: space-between;
230      .list-wrap {
231        .date {
232          font-size: 0.8rem;
233          opacity: 0.6;
234        }
235        .commit-msg {
236          font-size: 0.8rem;
237          opacity: 0.8;
238          display: -webkit-box;
239          -webkit-line-clamp: 2;
240          -webkit-box-orient: vertical;  
241          overflow: hidden;
242        }
243      }
244    }
245  }
246
247  h4 {
248    margin: 1rem 0 0 0;
249    font-size: 1.2rem;
250  }
251  p {
252    margin: 0;
253    display: flex;
254    align-items: center;
255    gap: 0.25rem;
256    :global(svg) {
257      width: 1rem;
258    }
259    img {
260      border-radius: var(--curve-sm);
261    }
262  }
263  .tags {
264    display: flex;
265    flex-wrap: wrap;
266    .tag {
267      margin: 0.1rem;
268      background: #5f53f440;
269      border-radius: var(--curve-sm);
270      padding: 0.05rem 0.2rem;
271      font-size: 0.8rem;
272      opacity: 0.8;
273    }
274  }
275  ul {
276    padding-left: 1rem;
277    list-style: circle;
278    max-height: 400px;
279    overflow: auto;
280    img {
281      border-radius: var(--curve-sm);
282    }
283  }
284  .languages-img {
285    width: 100%;
286    border-radius: var(--curve-sm);
287  }
288</style>