-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThumbnail.lua
79 lines (61 loc) · 1.76 KB
/
Thumbnail.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
--[[
Provides a player's thumbnail to other components.
Usage:
Roact.createElement(Thumbnail, {
userId = "-1", -- Must be a string
render = function(thumbnail)
return Roact.createElement("ImageLabel", {
Image = thumbnail.image
})
end
})
--]]
local Players = game:GetService("Players")
local Promise = require(script.Parent.Parent.Lib.Promise)
local Roact = require(script.Parent.Parent.Lib.Roact)
local t = require(script.Parent.Parent.Lib.t)
-- Must be one of the values from here:
-- http://wiki.roblox.com/index.php?title=API:Enum/ThumbnailSize
local AVATAR_SIZE = 420
local function fetchUserThumbnail(userId, thumbnailType)
thumbnailType = thumbnailType or Enum.ThumbnailType.AvatarBust
local size = ("Size{size}x{size}"):gsub("{size}", AVATAR_SIZE)
return Promise.new(function(resolve, reject)
spawn(function()
local ok, result = pcall(function()
return Players:GetUserThumbnailAsync(tonumber(userId),
thumbnailType, Enum.ThumbnailSize[size])
end)
if ok then
resolve(result)
else
reject(result)
end
end)
end)
end
local Thumbnail = Roact.Component:extend("Thumbnail")
local Props = t.interface({
userId = t.string,
render = t.callback,
type = t.optional(t.enum(Enum.ThumbnailType))
})
function Thumbnail:init()
self.state = {
image = ""
}
end
function Thumbnail:render()
assert(Props(self.props))
return self.props.render({
image = self.state.image,
size = Vector2.new(AVATAR_SIZE, AVATAR_SIZE)
})
end
function Thumbnail:didMount()
fetchUserThumbnail(self.props.userId, self.props.thumbnailType)
:andThen(function(image)
self:setState({ image = image })
end)
end
return Thumbnail