| 1 |
import { Gif } from "@mail/core/common/gif"; |
| 2 |
import { useOnBottomScrolled, useSequential } from "@mail/utils/common/hooks"; |
| 3 |
|
| 4 |
import { Component, onWillStart, useState, useEffect } from "@odoo/owl"; |
| 5 |
import { user } from "@web/core/user"; |
| 6 |
import { useService, useAutofocus } from "@web/core/utils/hooks"; |
| 7 |
import { useDebounced } from "@web/core/utils/timing"; |
| 8 |
import { rpc } from "@web/core/network/rpc"; |
| 9 |
import { PICKER_PROPS, usePicker } from "@web/core/emoji_picker/emoji_picker"; |
| 10 |
|
| 11 |
export function useGifPicker(...args) { |
| 12 |
return usePicker(GifPicker, ...args); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @typedef {Object} TenorCategory |
| 17 |
* @property {string} searchterm |
| 18 |
* @property {string} path |
| 19 |
* @property {string} image |
| 20 |
* @property {string} name |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* @typedef {Object} TenorMediaFormat |
| 25 |
* @property {string} url |
| 26 |
* @property {number} duration |
| 27 |
* @property {string} preview |
| 28 |
* @property {number[]} dims |
| 29 |
* @property {number} size |
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* @typedef {Object} TenorGif |
| 34 |
* @property {string} id |
| 35 |
* @property {string} title |
| 36 |
* @property {number} created |
| 37 |
* @property {string} content_description |
| 38 |
* @property {string} itemurl |
| 39 |
* @property {string} url |
| 40 |
* @property {string[]} tags |
| 41 |
* @property {string[]} flags |
| 42 |
* @property {boolean} hasaudio |
| 43 |
* @property {{ tinygif: TenorMediaFormat }} media_formats |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
* @typedef {Object} Props |
| 48 |
* @property {function} onSelect Callback to use when the gif is selected |
| 49 |
* @property {string} [className] |
| 50 |
* @property {function} [close] |
| 51 |
* @property {Object} [state] |
| 52 |
* @extends {Component<Props, Env>} |
| 53 |
*/ |
| 54 |
|
| 55 |
export class GifPicker extends Component { |
| 56 |
static template = "discuss.GifPicker"; |
| 57 |
static props = PICKER_PROPS; |
| 58 |
static components = { Gif }; |
| 59 |
|
| 60 |
setup() { |
| 61 |
super.setup(); |
| 62 |
this.orm = useService("orm"); |
| 63 |
this.store = useService("mail.store"); |
| 64 |
this.sequential = useSequential(); |
| 65 |
this.inputRef = useAutofocus(); |
| 66 |
useOnBottomScrolled( |
| 67 |
"scroller", |
| 68 |
() => { |
| 69 |
if (!this.state.showCategories) { |
| 70 |
if (!this.showFavorite) { |
| 71 |
this.search(); |
| 72 |
} else { |
| 73 |
this.loadFavoritesDebounced(this.offset); |
| 74 |
} |
| 75 |
} |
| 76 |
}, |
| 77 |
300 |
| 78 |
); |
| 79 |
this.next = ""; |
| 80 |
this.showFavorite = false; |
| 81 |
this.offset = 0; |
| 82 |
this.state = useState({ |
| 83 |
favorites: { |
| 84 |
/** @type {TenorGif[]} */ |
| 85 |
gifs: [], |
| 86 |
offset: 0, |
| 87 |
}, |
| 88 |
searchTerm: "", |
| 89 |
showCategories: true, |
| 90 |
/** @type {TenorCategory[]} */ |
| 91 |
categories: [], |
| 92 |
loadingGif: false, |
| 93 |
loadingError: false, |
| 94 |
evenGif: { |
| 95 |
/** @type {Map<Number, TenorGif>} */ |
| 96 |
gifs: new Map(), |
| 97 |
/** Size, in pixel, of the column. */ |
| 98 |
columnSize: 0, |
| 99 |
}, |
| 100 |
oddGif: { |
| 101 |
/** @type {Map<Number, TenorGif>} */ |
| 102 |
gifs: new Map(), |
| 103 |
/** Size, in pixel, of the column. */ |
| 104 |
columnSize: 0, |
| 105 |
}, |
| 106 |
focused: false, |
| 107 |
}); |
| 108 |
this.loadFavoritesDebounced = useDebounced(this.loadFavorites, 200); |
| 109 |
onWillStart(() => { |
| 110 |
this.loadCategories(); |
| 111 |
}); |
| 112 |
if (this.store.self_partner) { |
| 113 |
onWillStart(() => { |
| 114 |
this.loadFavorites(); |
| 115 |
}); |
| 116 |
} |
| 117 |
useEffect( |
| 118 |
() => { |
| 119 |
if (this.props.state?.picker !== this.props.PICKERS?.GIF) { |
| 120 |
return; |
| 121 |
} |
| 122 |
this.clear(); |
| 123 |
this.search(); |
| 124 |
if (this.searchTerm) { |
| 125 |
this.closeCategories(); |
| 126 |
} else { |
| 127 |
this.openCategories(); |
| 128 |
} |
| 129 |
}, |
| 130 |
() => [this.searchTerm, this.props.state?.picker] |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
get style() { |
| 135 |
return ""; |
| 136 |
} |
| 137 |
|
| 138 |
get searchTerm() { |
| 139 |
return this.props.state ? this.props.state.searchTerm : this.state.searchTerm; |
| 140 |
} |
| 141 |
|
| 142 |
set searchTerm(value) { |
| 143 |
if (this.props.state) { |
| 144 |
this.props.state.searchTerm = value; |
| 145 |
} else { |
| 146 |
this.state.searchTerm = value; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
async loadCategories() { |
| 151 |
if (!this.store.hasGifPickerFeature) { |
| 152 |
return; |
| 153 |
} |
| 154 |
try { |
| 155 |
let { language, region } = new Intl.Locale(user.lang); |
| 156 |
if (!region && language === "sr") { |
| 157 |
region = "RS"; |
| 158 |
} |
| 159 |
const { tags } = await rpc( |
| 160 |
"/discuss/gif/categories", |
| 161 |
{ |
| 162 |
country: region, |
| 163 |
locale: `${language}_${region}`, |
| 164 |
}, |
| 165 |
{ silent: true } |
| 166 |
); |
| 167 |
if (tags) { |
| 168 |
this.state.categories = tags; |
| 169 |
} |
| 170 |
} catch { |
| 171 |
this.state.loadingError = true; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
openCategories() { |
| 176 |
this.showFavorite = false; |
| 177 |
this.state.showCategories = true; |
| 178 |
this.searchTerm = ""; |
| 179 |
this.clear(); |
| 180 |
} |
| 181 |
|
| 182 |
closeCategories() { |
| 183 |
this.state.showCategories = false; |
| 184 |
} |
| 185 |
|
| 186 |
async search() { |
| 187 |
if (!this.searchTerm) { |
| 188 |
return; |
| 189 |
} |
| 190 |
try { |
| 191 |
let { language, region } = new Intl.Locale(user.lang); |
| 192 |
if (!region && language === "sr") { |
| 193 |
region = "RS"; |
| 194 |
} |
| 195 |
const params = { |
| 196 |
country: region, |
| 197 |
locale: `${language}_${region}`, |
| 198 |
search_term: this.searchTerm, |
| 199 |
}; |
| 200 |
if (this.next) { |
| 201 |
params.position = this.next; |
| 202 |
} |
| 203 |
const res = await this.sequential(() => { |
| 204 |
this.state.loadingGif = true; |
| 205 |
const res = rpc("/discuss/gif/search", params, { |
| 206 |
silent: true, |
| 207 |
}); |
| 208 |
this.state.loadingGif = false; |
| 209 |
return res; |
| 210 |
}); |
| 211 |
if (res) { |
| 212 |
const { next, results } = res; |
| 213 |
this.next = next; |
| 214 |
for (const gif of results) { |
| 215 |
this.pushGif(gif); |
| 216 |
} |
| 217 |
this.state.loadingError = false; |
| 218 |
} |
| 219 |
} catch { |
| 220 |
this.state.loadingError = true; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @param {TenorGif} gif |
| 226 |
*/ |
| 227 |
pushGif(gif) { |
| 228 |
if (this.state.evenGif.columnSize <= this.state.oddGif.columnSize) { |
| 229 |
this.state.evenGif.gifs.set(gif.id, gif); |
| 230 |
this.state.evenGif.columnSize += gif.media_formats.tinygif.dims[1]; |
| 231 |
} else { |
| 232 |
this.state.oddGif.gifs.set(gif.id, gif); |
| 233 |
this.state.oddGif.columnSize += gif.media_formats.tinygif.dims[1]; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @param {TenorGif} gif |
| 239 |
*/ |
| 240 |
onClickGif(gif) { |
| 241 |
this.props.onSelect(gif, true); |
| 242 |
this.props.close?.(); |
| 243 |
} |
| 244 |
|
| 245 |
clear() { |
| 246 |
this.state.evenGif.gifs.clear(); |
| 247 |
this.state.evenGif.columnSize = 0; |
| 248 |
this.state.oddGif.gifs.clear(); |
| 249 |
this.state.oddGif.columnSize = 0; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* @param {TenorCategory} category |
| 254 |
*/ |
| 255 |
async onClickCategory(category) { |
| 256 |
this.clear(); |
| 257 |
this.searchTerm = category.searchterm; |
| 258 |
this.inputRef.el?.focus(); |
| 259 |
this.closeCategories(); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* @param {TenorGif} gif |
| 264 |
*/ |
| 265 |
async onClickFavorite(gif) { |
| 266 |
if (!this.isFavorite(gif)) { |
| 267 |
this.state.favorites.gifs.push(gif); |
| 268 |
await this.orm.silent.create("discuss.gif.favorite", [{ tenor_gif_id: gif.id }]); |
| 269 |
} else { |
| 270 |
const index = this.state.favorites.gifs.findIndex(({ id }) => id === gif.id); |
| 271 |
if (index >= 0) { |
| 272 |
this.state.favorites.gifs.splice(index, 1); |
| 273 |
} |
| 274 |
await rpc("/discuss/gif/remove_favorite", { tenor_gif_id: gif.id }, { silent: true }); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
async loadFavorites() { |
| 279 |
if (!this.store.hasGifPickerFeature) { |
| 280 |
return; |
| 281 |
} |
| 282 |
this.state.loadingGif = true; |
| 283 |
try { |
| 284 |
const [results] = await rpc( |
| 285 |
"/discuss/gif/favorites", |
| 286 |
{ offset: this.offset }, |
| 287 |
{ silent: true } |
| 288 |
); |
| 289 |
this.offset += 20; |
| 290 |
this.state.favorites.gifs.push(...results); |
| 291 |
} catch { |
| 292 |
this.state.loadingError = true; |
| 293 |
} |
| 294 |
this.state.loadingGif = false; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* @param {TenorGif} gif |
| 299 |
*/ |
| 300 |
isFavorite(gif) { |
| 301 |
return this.state.favorites.gifs.map((favorite) => favorite.id).includes(gif.id); |
| 302 |
} |
| 303 |
|
| 304 |
onClickFavoritesCategory() { |
| 305 |
this.showFavorite = true; |
| 306 |
for (const gif of this.state.favorites.gifs) { |
| 307 |
this.pushGif(gif); |
| 308 |
} |
| 309 |
this.closeCategories(); |
| 310 |
} |
| 311 |
} |