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.

158 lines
5.2 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. // This is also used to fix scrolling to sections on Element iOS
  17. const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
  18. class App extends Component {
  19. constructor(props) {
  20. super(props)
  21. this.state = {
  22. packs: [],
  23. loading: true,
  24. error: null,
  25. }
  26. this.imageObserver = null
  27. this.packListRef = null
  28. }
  29. componentDidMount() {
  30. fetch(`${PACKS_BASE_URL}/index.json`).then(async indexRes => {
  31. if (indexRes.status >= 400) {
  32. this.setState({
  33. loading: false,
  34. error: indexRes.status !== 404 ? indexRes.statusText : null,
  35. })
  36. return
  37. }
  38. const indexData = await indexRes.json()
  39. HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL
  40. // TODO only load pack metadata when scrolled into view?
  41. for (const packFile of indexData.packs) {
  42. const packRes = await fetch(`${PACKS_BASE_URL}/${packFile}`)
  43. const packData = await packRes.json()
  44. this.setState({
  45. packs: [...this.state.packs, packData],
  46. loading: false,
  47. })
  48. }
  49. }, error => this.setState({ loading: false, error }))
  50. this.imageObserver = new IntersectionObserver(this.observeImageIntersections, {
  51. rootMargin: "100px",
  52. })
  53. this.sectionObserver = new IntersectionObserver(this.observeSectionIntersections)
  54. }
  55. observeImageIntersections(intersections) {
  56. for (const entry of intersections) {
  57. const img = entry.target.children.item(0)
  58. if (entry.isIntersecting) {
  59. img.setAttribute("src", img.getAttribute("data-src"))
  60. img.classList.add("visible")
  61. } else {
  62. img.removeAttribute("src")
  63. img.classList.remove("visible")
  64. }
  65. }
  66. }
  67. observeSectionIntersections(intersections) {
  68. for (const entry of intersections) {
  69. const packID = entry.target.getAttribute("data-pack-id")
  70. const navElement = document.getElementById(`nav-${packID}`)
  71. if (entry.isIntersecting) {
  72. navElement.classList.add("visible")
  73. } else {
  74. navElement.classList.remove("visible")
  75. }
  76. }
  77. }
  78. componentDidUpdate() {
  79. for (const elem of this.packListRef.getElementsByClassName("sticker")) {
  80. this.imageObserver.observe(elem)
  81. }
  82. for (const elem of this.packListRef.children) {
  83. this.sectionObserver.observe(elem)
  84. }
  85. }
  86. componentWillUnmount() {
  87. this.imageObserver.disconnect()
  88. this.sectionObserver.disconnect()
  89. }
  90. render() {
  91. if (this.state.loading) {
  92. return html`<main class="spinner"><${Spinner} size=${80} green /></main>`
  93. } else if (this.state.error) {
  94. return html`<main class="error">
  95. <h1>Failed to load packs</h1>
  96. <p>${this.state.error}</p>
  97. </main>`
  98. } else if (this.state.packs.length === 0) {
  99. return html`<main class="empty"><h1>No packs found 😿</h1></main>`
  100. }
  101. return html`<main class="has-content">
  102. <nav>
  103. ${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)}
  104. </nav>
  105. <div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${elem => this.packListRef = elem}>
  106. ${this.state.packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack}/>`)}
  107. </div>
  108. </main>`
  109. }
  110. }
  111. // By default we just let the browser handle scrolling to sections, but webviews on Element iOS
  112. // open the link in the browser instead of just scrolling there, so we need to scroll manually:
  113. const scrollToSection = (evt, id) => {
  114. const pack = document.getElementById(`pack-${id}`)
  115. pack.scrollIntoView({ block: "start", behavior: "instant" })
  116. evt.preventDefault()
  117. }
  118. const NavBarItem = ({ pack }) => html`
  119. <a href="#pack-${pack.id}" id="nav-${pack.id}" data-pack-id=${pack.id} title=${pack.title}
  120. onClick=${isMobileSafari ? (evt => scrollToSection(evt, pack.id)) : undefined}>
  121. <div class="sticker">
  122. <img src=${makeThumbnailURL(pack.stickers[0].url)}
  123. alt=${pack.stickers[0].body} class="visible" />
  124. </div>
  125. </a>
  126. `
  127. const Pack = ({ pack }) => html`
  128. <section class="stickerpack" id="pack-${pack.id}" data-pack-id=${pack.id}>
  129. <h1>${pack.title}</h1>
  130. <div class="sticker-list">
  131. ${pack.stickers.map(sticker => html`
  132. <${Sticker} key=${sticker["net.maunium.telegram.sticker"].id} content=${sticker}/>
  133. `)}
  134. </div>
  135. </section>
  136. `
  137. const Sticker = ({ content }) => html`
  138. <div class="sticker" onClick=${() => sendSticker(content)}>
  139. <img data-src=${makeThumbnailURL(content.url)} alt=${content.body} />
  140. </div>
  141. `
  142. render(html`<${App} />`, document.body)