Skip to content

Commit

Permalink
feat(cmdk): persist search type
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Sep 26, 2024
1 parent 89eda0f commit 8e60217
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions apps/renderer/src/modules/panel/cmdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useI18n, useInputComposition } from "~/hooks/common"
import type { FeedViewType } from "~/lib/enum"
import { cn } from "~/lib/utils"
import { getFeedById } from "~/store/feed"
import { searchActions, useSearchStore } from "~/store/search"
import { searchActions, useSearchStore, useSearchType } from "~/store/search"
import { SearchType } from "~/store/search/constants"
import type { SearchInstance } from "~/store/search/types"
import { getSubscriptionByFeedId } from "~/store/subscription"
Expand Down Expand Up @@ -286,7 +286,7 @@ const SearchResultCount: FC<{
const t = useI18n()
const searchInstance = React.useContext(SearchCmdKContext)
const hasKeyword = useSearchStore((s) => !!s.keyword)
const searchType = useSearchStore((s) => s.searchType)
const searchType = useSearchType()

const recordCountPromise = useMemo(async () => {
let count = 0
Expand Down Expand Up @@ -334,7 +334,7 @@ const SearchResultCount: FC<{
}
const SearchOptions: Component = memo(({ children }) => {
const { t } = useTranslation()
const searchType = useSearchStore((s) => s.searchType)
const searchType = useSearchType()

const searchInstance = React.useContext(SearchCmdKContext)

Expand Down
20 changes: 15 additions & 5 deletions apps/renderer/src/store/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { IFuseOptions } from "fuse.js"
import Fuse from "fuse.js"
import { useAtomValue } from "jotai"
import { atomWithStorage } from "jotai/utils"

import { jotaiStore } from "~/lib/jotai"
import { getStorageNS } from "~/lib/ns"
import type { EntryModel } from "~/models"
import { EntryService, FeedService, SubscriptionService } from "~/services"

Expand All @@ -10,12 +14,17 @@ import { SearchType } from "./constants"
import { defineSearchInstance } from "./helper"
import type { SearchResult, SearchState } from "./types"

const searchTypeAtom = atomWithStorage<SearchType>(
getStorageNS("search-type"),
SearchType.All,
undefined,
{ getOnInit: true },
)
const createState = (): SearchState => ({
feeds: [],
entries: [],
subscriptions: [],
keyword: "",
searchType: SearchType.All,
})
export const useSearchStore = createZustandStore<SearchState>("search")(createState)

Expand Down Expand Up @@ -55,7 +64,7 @@ class SearchActions {
subscriptions: subscriptions.length,
},
search(keyword: string) {
const type = get().searchType
const type = jotaiStore.get(searchTypeAtom)
const entries = type & SearchType.Entry ? entriesFuse.search(keyword) : []
const feeds = type & SearchType.Feed ? feedsFuse.search(keyword) : []

Expand Down Expand Up @@ -86,21 +95,22 @@ class SearchActions {
entries: processedEntries,
feeds,
subscriptions: processedSubscriptions,
searchType: type,
})

jotaiStore.set(searchTypeAtom, type)

return get()
},
})
}

setSearchType(type: SearchType) {
set({ searchType: type })
jotaiStore.set(searchTypeAtom, type)
}

getCurrentKeyword() {
return get().keyword
}
}

export const useSearchType = () => useAtomValue(searchTypeAtom)
export const searchActions = new SearchActions()
2 changes: 0 additions & 2 deletions apps/renderer/src/store/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EntryModel, FeedOrListRespModel } from "~/models"

import type { SubscriptionFlatModel } from "../subscription"
import type { SearchType } from "./constants"

// @ts-expect-error
export interface SearchResult<T extends object, A extends object = object> extends A {
Expand All @@ -14,7 +13,6 @@ export interface SearchState {
subscriptions: SearchResult<SubscriptionFlatModel, { feedId: string }>[]

keyword: string
searchType: SearchType
}
export interface SearchInstance {
search: (keyword: string) => SearchState
Expand Down

0 comments on commit 8e60217

Please sign in to comment.