web/src/components/things/GitHubDetailedInfo.astro

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