diff --git a/src/Components/MaterialPanel/init.lua b/src/Components/MaterialPanel/init.lua index ce432f6..90aed0b 100644 --- a/src/Components/MaterialPanel/init.lua +++ b/src/Components/MaterialPanel/init.lua @@ -15,37 +15,28 @@ local TextLabel = require(Components.TextLabel) local useStore = require(Plugin.Hooks.useStore) local useTheme = require(Plugin.Hooks.useTheme) -function CreateMaterialButtons(searchTerm) - local numberAssets = 0 +local function MaterialPanel(props, hooks) + local theme = useTheme(hooks) + local SearchTerm = useStore(hooks, 'SearchTerm') - local assetsToDisplay = { - UIGridLayout = Roact.createElement('UIGridLayout', { - CellPadding = Constants.MATERIAL_GRID_PADDING, - CellSize = Constants.MATERIAL_GRID_SIZE, - FillDirectionMaxCells = 6, - HorizontalAlignment = Enum.HorizontalAlignment.Center - }) - } + local content, assetCount = hooks.useMemo(function() + local numberAssets = 0 + local assetsToDisplay = {} - for _, item in Constants.MATERIALS_TABLE do - if string.find(string.lower(item.enum.Name), string.lower(searchTerm), 1, true) then - assetsToDisplay[item.enum.Name] = Roact.createElement(MaterialItem, { - Id = item.enum, - Image = item.img - }) + for _, item in Constants.MATERIALS_TABLE do + if string.find(string.lower(item.enum.Name), string.lower(SearchTerm), 1, true) then + assetsToDisplay[item.enum.Name] = Roact.createElement(MaterialItem, { + Id = item.enum, + Image = item.img + }) - numberAssets += 1 + numberAssets += 1 + end end - end - return assetsToDisplay, numberAssets -end - -local function MaterialPanel(props, hooks) - local theme = useTheme(hooks) - local SearchTerm = useStore(hooks, 'SearchTerm') + return assetsToDisplay, numberAssets + end, { SearchTerm }) - local content, assetCount = CreateMaterialButtons(SearchTerm) local hasAssets = assetCount ~= 0 return Roact.createElement('Frame', { @@ -57,7 +48,15 @@ local function MaterialPanel(props, hooks) AutomaticCanvasSize = Enum.AutomaticSize.Y, ScrollingDirection = Enum.ScrollingDirection.Y, Size = UDim2.fromScale(1, 1) - }, content), + }, { + UIGridLayout = Roact.createElement('UIGridLayout', { + CellPadding = Constants.MATERIAL_GRID_PADDING, + CellSize = Constants.MATERIAL_GRID_SIZE, + FillDirectionMaxCells = 6, + HorizontalAlignment = Enum.HorizontalAlignment.Center + }), + Items = Roact.createFragment(content) + }), NoResults = not hasAssets and Roact.createElement(TextLabel, { BackgroundTransparency = 1, Size = UDim2.new(1, 0, 0, 30), diff --git a/src/Components/StudioWidget.lua b/src/Components/StudioWidget.lua index fc7ad63..a898280 100644 --- a/src/Components/StudioWidget.lua +++ b/src/Components/StudioWidget.lua @@ -51,10 +51,6 @@ function StudioWidget:init() end end) - if props[Roact.Ref] then - props[Roact.Ref](pluginGui) - end - self.pluginGui = pluginGui end diff --git a/src/Context/StudioTheme.lua b/src/Context/StudioTheme.lua index 1fd1d33..d6d27eb 100644 --- a/src/Context/StudioTheme.lua +++ b/src/Context/StudioTheme.lua @@ -5,20 +5,16 @@ local Plugin = script.Parent.Parent local Roact = require(Plugin.Packages.Roact) local Hooks = require(Plugin.Packages.RoactHooks) -local Context = Roact.createContext() +local useEventConnection = require(Plugin.Hooks.useEventConnection) + +local Context = Roact.createContext(nil) local function StudioThemeProvider(props, hooks) local theme, setTheme = hooks.useState(Studio.Theme) - hooks.useEffect(function() - local themeConnection = Studio.ThemeChanged:Connect(function() - setTheme(Studio.Theme) - end) - - return function() - themeConnection:Disconnect() - end - end, {}) + useEventConnection(hooks, Studio.ThemeChanged, function() + setTheme(Studio.Theme) + end) return Roact.createElement(Context.Provider, { value = theme diff --git a/src/Hooks/useStore.lua b/src/Hooks/useStore.lua index 0c20205..a2c4055 100644 --- a/src/Hooks/useStore.lua +++ b/src/Hooks/useStore.lua @@ -1,17 +1,12 @@ +local useEventConnection = require(script.Parent.useEventConnection) local Store = require(script.Parent.Parent.Util.Store) local function useStore(hooks, key: string) local value, setValue = hooks.useState(Store:Get(key)) - hooks.useEffect(function() - local connection = Store:GetChangedSignal(key):Connect(function(newValue) - setValue(newValue) - end) - - return function() - connection:Disconnect() - end - end, {}) + useEventConnection(hooks, Store:GetChangedSignal(key), function(newValue) + setValue(newValue) + end) return value end diff --git a/src/Util/Constants.lua b/src/Util/Constants.lua index 65dd561..2a79f80 100644 --- a/src/Util/Constants.lua +++ b/src/Util/Constants.lua @@ -20,8 +20,8 @@ Constants.DEV_UPDATE_CHECKER_ID = 4685764627 ---------------------------------------- -- Material related ---------------------------------------- -Constants.MATERIAL_GRID_PADDING = UDim2.new(0, 5, 0, 5) -Constants.MATERIAL_GRID_SIZE = UDim2.new(0, 50, 0, 50) +Constants.MATERIAL_GRID_PADDING = UDim2.fromOffset(5, 5) +Constants.MATERIAL_GRID_SIZE = UDim2.fromOffset(50, 50) Constants.MATERIALS_TABLE = { { enum = Enum.Material.Air, img = 'rbxasset://textures/TerrainTools/mtrl_air.png' },