// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
import { html, render, Component } from "../lib/htm/preact.js"
import { Spinner } from "./spinner.js"
import { SearchBox } from "./search-box.js"
import * as widgetAPI from "./widget-api.js"
import * as frequent from "./frequently-used.js"
// import GiphyAPI from "./GiphySearch.js"
// The base URL for fetching packs. The app will first fetch ${PACK_BASE_URL}/index.json,
// then ${PACK_BASE_URL}/${packFile} for each packFile in the packs object of the index.json file.
const PACKS_BASE_URL = "packs"
let INDEX = `${PACKS_BASE_URL}/index.json`
const params = new URLSearchParams(document.location.search)
if (params.has('config')) {
INDEX = params.get("config")
}
// This is updated from packs/index.json
let HOMESERVER_URL = "https://matrix-client.matrix.org"
let GIPHY_API_KEY = ""
let GIPHY_HOMESERVER = "mxc://giphy.mau.dev/"
const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${mxc.substr(6)}?height=128&width=128&method=scale`
// We need to detect iOS webkit because it has a bug related to scrolling non-fixed divs
// This is also used to fix scrolling to sections on Element iOS
const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
const supportedThemes = ["light", "dark", "black"]
const defaultState = {
packs: [],
filtering: {
searchTerm: "",
packs: [],
},
}
class GiphySearchTab extends Component {
constructor(props) {
super(props);
this.state = {
searchTerm: "",
gifs: [],
loading: false,
GIFById: {},
};
this.handleSearchChange = this.handleSearchChange.bind(this);
this.searchGifs = this.searchGifs.bind(this);
this.handleGifClick = this.handleGifClick.bind(this);
}
async searchGifs() {
this.setState({ loading: true });
try {
const apiKey = GIPHY_API_KEY;
const gif_homeserver = GIPHY_HOMESERVER;
const url = `https://api.giphy.com/v1/gifs/search?q=${this.state.searchTerm}&api_key=${apiKey}`;
this.setState({ loading: true });
const response = await fetch(url);
const data = await response.json();
this.setState({ gifs: data.data, loading: false });
data.data.forEach((jsonElement) => {
const id = jsonElement.id;
const updatedItem = {
"body": jsonElement.title,
"info": {
"h": jsonElement.images.original.height,
"w": jsonElement.images.original.width,
"size": jsonElement.images.original.size,
"mimetype": "image/gif",
"thumbnail_info": {
"h": jsonElement.images.fixed_width_still.height,
"mimetype": "image/jpg",
"size": jsonElement.images.fixed_width_still.size,
"w": jsonElement.images.fixed_width_still.width
},
},
"msgtype": "m.image",
"url": gif_homeserver+jsonElement.id+".gif"
};
this.setState((prevState) => ({
GIFById: {...prevState.GIFById, [id]: updatedItem}}));
});
} catch (error) {
this.setState({ error: "Error fetching GIFs", loading: false });
this.setState({ loading: false });
}
}
handleSearchChange(event) {
this.setState({ searchTerm: event.target.value });
}
handleGifClick(gif) {
console.log(this.state.GIFById[gif.id]);
widgetAPI.sendGIF(this.state.GIFById[gif.id]);
}
async searchGiphy(searchTerm) {
if (!searchTerm) return;
};
render() {
const { searchTerm, gifs, loading } = this.state;
return html`
GIF Search is not enabled. Please enable it in the config.
`}
`;
}
}
const Settings = ({ app }) => html`
Settings
app.setStickersPerRow(evt.target.value)} />
`
// By default we just let the browser handle scrolling to sections, but webviews on Element iOS
// open the link in the browser instead of just scrolling there, so we need to scroll manually:
const scrollToSection = (evt, id) => {
const pack = document.getElementById(`pack-${id}`)
pack.scrollIntoView({ block: "start", behavior: "instant" })
evt.preventDefault()
}
const NavBarItem = ({ pack, iconOverride = null }) => html`
scrollToSection(evt, pack.id)) : undefined}>