Skip to content

Commit

Permalink
Remember search term between themes
Browse files Browse the repository at this point in the history
  • Loading branch information
vocksel committed Jul 20, 2024
1 parent 6d21b05 commit d98ac05
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions plugin/src/components/App.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Props = {
local function App(_props: Props)
local view, setView = useState("Home" :: View)
local extension, setExtension = useState(nil :: PublishedExtension?)
local searchTerm, setSearchTerm = useState(nil :: string?)

local onBack = useCallback(function()
setExtension(nil)
Expand All @@ -35,7 +36,9 @@ local function App(_props: Props)
}, {
Home = if view == "Home"
then React.createElement(HomeWrapper, {
onSearch = setSearchTerm,
onViewExtension = onViewExtension,
searchTerm = searchTerm,
})
else nil,

Expand Down
13 changes: 7 additions & 6 deletions plugin/src/components/HomeWrapper.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ local useEffect = React.useEffect
local useState = React.useState

export type Props = {
onSearch: (searchTerm: string) -> (),
onViewExtension: (extension: PublishedExtension) -> (),
searchTerm: string?,
}

local function HomeWrapper(props: Props)
local page, setPage = useState(1)
local extensions, setExtensions = useState({} :: { PublishedExtension })
local searchTerm, setSearchTerm = useState(nil :: string?)
local err, setErr = useState(nil :: string?)

local onFetchMore = useCallback(function()
Expand All @@ -28,14 +29,14 @@ local function HomeWrapper(props: Props)

useEffect(function()
setPage(1)
end, { searchTerm })
end, { props.searchTerm })

useEffect(function()
setErr(nil)
fetchVisualStudioExtensions({
page = page,
pageSize = 20,
searchTerm = if searchTerm then searchTerm else "theme",
searchTerm = if props.searchTerm then props.searchTerm else "theme",
})
:andThen(function(newExtensions)
setExtensions(function(prev)
Expand All @@ -49,13 +50,13 @@ local function HomeWrapper(props: Props)
:catch(function()
setErr(`No extensions found. Please try again later`)
end)
end, { searchTerm, page })
end, { props.searchTerm, page })

return React.createElement(Home, {
extensions = extensions,
err = err,
searchTerm = searchTerm,
onSearch = setSearchTerm,
searchTerm = props.searchTerm,
onSearch = props.onSearch,
onFetchMore = onFetchMore,
onViewExtension = props.onViewExtension,
})
Expand Down
1 change: 1 addition & 0 deletions plugin/src/components/HomeWrapper.story.luau
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ return {
story = function()
return React.createElement(HomeWrapper, {
onViewExtension = print,
onSearch = print,
})
end,
}

0 comments on commit d98ac05

Please sign in to comment.