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.

148 lines
4.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. // Copyright (c) 2020 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. import { html, render, Component } from "https://unpkg.com/htm/preact/index.mjs?module"
  7. import { Spinner } from "./spinner.js"
  8. import { sendSticker } from "./widget-api.js"
  9. // The base URL for fetching packs. The app will first fetch ${PACK_BASE_URL}/index.json,
  10. // then ${PACK_BASE_URL}/${packFile} for each packFile in the packs object of the index.json file.
  11. const PACKS_BASE_URL = "packs"
  12. // This is updated from packs/index.json
  13. let HOMESERVER_URL = "https://matrix-client.matrix.org"
  14. const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${mxc.substr(6)}?height=128&width=128&method=scale`
  15. // We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs
  16. const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
  17. class App extends Component {
  18. constructor(props) {
  19. super(props)
  20. this.state = {
  21. packs: [],
  22. loading: true,
  23. error: null,
  24. }
  25. this.imageObserver = null
  26. this.packListRef = null
  27. }
  28. componentDidMount() {
  29. fetch(`${PACKS_BASE_URL}/index.json`).then(async indexRes => {
  30. if (indexRes.status >= 400) {
  31. this.setState({
  32. loading: false,
  33. error: indexRes.status !== 404 ? indexRes.statusText : null,
  34. })
  35. return
  36. }
  37. const indexData = await indexRes.json()
  38. HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL
  39. // TODO only load pack metadata when scrolled into view?
  40. for (const packFile of indexData.packs) {
  41. const packRes = await fetch(`${PACKS_BASE_URL}/${packFile}`)
  42. const packData = await packRes.json()
  43. this.setState({
  44. packs: [...this.state.packs, packData],
  45. loading: false,
  46. })
  47. }
  48. }, error => this.setState({ loading: false, error }))
  49. this.imageObserver = new IntersectionObserver(this.observeImageIntersections, {
  50. rootMargin: "100px",
  51. })
  52. this.sectionObserver = new IntersectionObserver(this.observeSectionIntersections)
  53. }
  54. observeImageIntersections(intersections) {
  55. for (const entry of intersections) {
  56. const img = entry.target.children.item(0)
  57. if (entry.isIntersecting) {
  58. img.setAttribute("src", img.getAttribute("data-src"))
  59. img.classList.add("visible")
  60. } else {
  61. img.removeAttribute("src")
  62. img.classList.remove("visible")
  63. }
  64. }
  65. }
  66. observeSectionIntersections(intersections) {
  67. for (const entry of intersections) {
  68. const packID = entry.target.getAttribute("data-pack-id")
  69. const navElement = document.getElementById(`nav-${packID}`)
  70. if (entry.isIntersecting) {
  71. navElement.classList.add("visible")
  72. } else {
  73. navElement.classList.remove("visible")
  74. }
  75. }
  76. }
  77. componentDidUpdate() {
  78. for (const elem of this.packListRef.getElementsByClassName("sticker")) {
  79. this.imageObserver.observe(elem)
  80. }
  81. for (const elem of this.packListRef.children) {
  82. this.sectionObserver.observe(elem)
  83. }
  84. }
  85. componentWillUnmount() {
  86. this.imageObserver.disconnect()
  87. this.sectionObserver.disconnect()
  88. }
  89. render() {
  90. if (this.state.loading) {
  91. return html`<main class="spinner"><${Spinner} size=${80} green /></main>`
  92. } else if (this.state.error) {
  93. return html`<main class="error">
  94. <h1>Failed to load packs</h1>
  95. <p>${this.state.error}</p>
  96. </main>`
  97. } else if (this.state.packs.length === 0) {
  98. return html`<main class="empty"><h1>No packs found 😿</h1></main>`
  99. }
  100. return html`<main class="has-content">
  101. <nav>
  102. ${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)}
  103. </nav>
  104. <div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${elem => this.packListRef = elem}>
  105. ${this.state.packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack}/>`)}
  106. </div>
  107. </main>`
  108. }
  109. }
  110. const NavBarItem = ({ pack }) => html`
  111. <a href="#pack-${pack.id}" id="nav-${pack.id}" data-pack-id=${pack.id} title=${pack.title}>
  112. <div class="sticker">
  113. <img src=${makeThumbnailURL(pack.stickers[0].url)}
  114. alt=${pack.stickers[0].body} class="visible" />
  115. </div>
  116. </a>
  117. `
  118. const Pack = ({ pack }) => html`
  119. <section class="stickerpack" id="pack-${pack.id}" data-pack-id=${pack.id}>
  120. <h1>${pack.title}</h1>
  121. <div class="sticker-list">
  122. ${pack.stickers.map(sticker => html`
  123. <${Sticker} key=${sticker["net.maunium.telegram.sticker"].id} content=${sticker}/>
  124. `)}
  125. </div>
  126. </section>
  127. `
  128. const Sticker = ({ content }) => html`
  129. <div class="sticker" onClick=${() => sendSticker(content)}>
  130. <img data-src=${makeThumbnailURL(content.url)} alt=${content.body} />
  131. </div>
  132. `
  133. render(html`<${App} />`, document.body)