Skip to content

Commit

Permalink
Use local storage for useLastOpenedStory
Browse files Browse the repository at this point in the history
  • Loading branch information
vocksel committed Dec 19, 2024
1 parent e3e1c7b commit 3ac0c22
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Storybook/useLastOpenedStory.luau
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
local React = require("@pkg/React")

local PluginContext = require("@root/Plugin/PluginContext")
local LocalStorageContext = require("@root/Plugin/LocalStorageContext")
local SettingsContext = require("@root/UserSettings/SettingsContext")
local getInstanceFromFullName = require("@root/Common/getInstanceFromFullName")

local useContext = React.useContext
local useCallback = React.useCallback
local useMemo = React.useMemo

local function useLastOpenedStory(): (ModuleScript?, (storyModule: ModuleScript?) -> ())
local plugin = useContext(PluginContext.Context)
local REMEMBER_LAST_OPENED_STORY_KEY = "rememberLastOpenedStory"
local LAST_OPENED_STORY_PATH_KEY = "lastOpenedStoryPath"

local function useLastOpenedStory(): (ModuleScript?, (storyModule: ModuleScript?) -> ())
local localStorage = LocalStorageContext.use()
local settingsContext = SettingsContext.use()
local rememberLastOpenedStory = settingsContext.getSetting("rememberLastOpenedStory")

local setLastOpenedStory = useCallback(function(storyModule: ModuleScript?)
plugin:SetSetting("lastOpenedStoryPath", if storyModule then storyModule:GetFullName() else nil)
end, { plugin })
localStorage.set(LAST_OPENED_STORY_PATH_KEY, if storyModule then storyModule:GetFullName() else nil)
end, { localStorage })

local lastOpenedStory = useMemo(function(): ModuleScript?
local rememberLastOpenedStory = settingsContext.getSetting(REMEMBER_LAST_OPENED_STORY_KEY)

if not rememberLastOpenedStory then
return nil
end

local lastOpenedStoryPath = plugin:GetSetting("lastOpenedStoryPath")
local lastOpenedStoryPath = localStorage.get(LAST_OPENED_STORY_PATH_KEY)

if lastOpenedStoryPath then
local instance = getInstanceFromFullName(lastOpenedStoryPath)
Expand All @@ -34,7 +36,7 @@ local function useLastOpenedStory(): (ModuleScript?, (storyModule: ModuleScript?
end

return nil
end, { rememberLastOpenedStory, plugin })
end, { settingsContext, localStorage })

return lastOpenedStory, setLastOpenedStory
end
Expand Down

0 comments on commit 3ac0c22

Please sign in to comment.