Mobile-responsive personal website, generated using Hugo. https://kevin-mok.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

  1. const log = console.log
  2. const headerKeys = {
  3. "status": "Status",
  4. "url": "URL",
  5. "description": "Description",
  6. "repo": "Repository"
  7. }
  8. const keyOrder = ["status", "url", "description", "repo"]
  9. const generateTableHead = table => {
  10. let thead = table.createTHead();
  11. let row = thead.insertRow();
  12. keyOrder.forEach(key => {
  13. let th = document.createElement("th");
  14. th.textContent = headerKeys[key];
  15. row.appendChild(th);
  16. })
  17. }
  18. const generateTable = (table, data) => {
  19. data.forEach(elem => {
  20. let row = table.insertRow();
  21. keyOrder.forEach(key => {
  22. const cell = row.insertCell();
  23. cell.className = key;
  24. const aElem = document.createElement('a')
  25. aElem.target = '_blank'
  26. switch (key) {
  27. case 'status':
  28. // cell.textContent = ((elem[key] == 'up') ? '🗸' : '✗')
  29. const statusIcon = document.createElement('img')
  30. statusIcon.src = '/img/server/' + ((elem[key] == 'up') ? 'check' : 'x') + '.svg'
  31. cell.appendChild(statusIcon)
  32. break
  33. case 'url':
  34. aElem.href = elem[key]
  35. aElem.textContent = elem['shortUrl']
  36. cell.appendChild(aElem)
  37. break
  38. case 'repo':
  39. aElem.href = elem['repoUrl']
  40. aElem.textContent = elem[key]
  41. cell.appendChild(aElem)
  42. break
  43. default:
  44. cell.textContent = elem[key]
  45. }
  46. })
  47. })
  48. }
  49. const getDurationSince = date => {
  50. const milli = Math.abs(Date.now() - date)
  51. let mins = Math.floor(milli/1000/60)
  52. const days = Math.floor(mins/60/24)
  53. mins %= 60*24
  54. const hours = Math.floor(mins/60)
  55. mins %= 60
  56. let durationString = (days > 0) ? `${days}d ` : ''
  57. durationString += (hours > 0) ? `${hours} hours, ` : ''
  58. durationString += (mins > 0) ? `${mins} minutes` : '0m'
  59. return durationString
  60. }
  61. fetch("/server-apps.json")
  62. .then(response => response.json())
  63. .then(json => {
  64. let table = document.querySelector("table");
  65. generateTable(table, json['sites']);
  66. generateTableHead(table);
  67. document.querySelector('#last-updated-time').textContent =
  68. getDurationSince(new Date(json['updateTime'])) + ' ago.'
  69. document.querySelector('#server-uptime-time').textContent =
  70. getDurationSince(new Date(json['upSince'])) + '.'
  71. });