Skip to content

Commit

Permalink
feat: present filters and selected icons in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 1, 2024
1 parent 811ac8e commit 24a3a59
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
31 changes: 12 additions & 19 deletions src/components/IconSet.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<!-- eslint-disable no-console -->
<script setup lang='ts'>
import { useRoute, useRouter } from 'vue-router'
import { activeMode, bags, drawerCollapsed, getSearchResults, iconSize, isCurrentCollectionLoading, listType, showHelp, toggleBag } from '../store'
import { isLocalMode } from '../env'
import { cacheCollection, specialTabs } from '../data'
import { getIconSnippet } from '../utils/icons'
import {cleanupQuery} from '../utils/query'

Check failure on line 7 in src/components/IconSet.vue

View workflow job for this annotation

GitHub Actions / lint

A space is required after '{'

Check failure on line 7 in src/components/IconSet.vue

View workflow job for this annotation

GitHub Actions / lint

A space is required before '}'

const route = useRoute()
const router = useRouter()

const showBag = ref(false)
const copied = ref(false)
const current = ref('')
const current = computed({
get() {
return (route.query.icon as string) || ''
},
set(value) {
router.replace({ query: cleanupQuery({ ...route.query, icon: value }) })
},
})
const max = ref(isLocalMode ? 500 : 200)
const searchbar = ref<{ input: HTMLElement }>()

const route = useRoute()
const router = useRouter()

Check failure on line 25 in src/components/IconSet.vue

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
const { search, icons, category, collection, variant } = getSearchResults()
const loading = isCurrentCollectionLoading()
Expand Down Expand Up @@ -102,21 +110,6 @@ watch(
() => max.value = maxMap.get(namespace.value) || 200,
)

onMounted(() => {
search.value = route.query.s as string || ''
watch([search], () => {
synchronizeSearchQuery()
})
watch([collection], () => {
if (search.value)
synchronizeSearchQuery()
})
})

function synchronizeSearchQuery() {
router.replace({ query: { s: search.value } })
}

function focusSearch() {
searchbar.value?.input.focus()
}
Expand Down
31 changes: 28 additions & 3 deletions src/hooks/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,36 @@ import { computed, markRaw, ref, watch } from 'vue'
import type { CollectionMeta } from '../data'
import { specialTabs } from '../data'
import { searchAlias } from '../data/search-alias'
import { cleanupQuery } from '../utils/query'

export function useSearch(collection: Ref<CollectionMeta | null>) {
const category = ref('')
const variant = ref('')
const search = ref('')
const route = useRoute()
const router = useRouter()

const category = computed({
get() {
return route.query.category as string || ''
},
set(value: string) {
router.replace({ query: cleanupQuery({ ...route.query, category: value }) })
},
})
const variant = computed({
get() {
return route.query.variant as string || ''
},
set(value: string) {
router.replace({ query: cleanupQuery({ ...route.query, variant: value }) })
},
})
const search = computed({
get() {
return route.query.s as string || ''
},
set(value: string) {
router.replace({ query: cleanupQuery({ ...route.query, s: value }) })
},
})

const isAll = computed(() => collection.value && specialTabs.includes(collection.value.id))
const searchParts = computed(() => search.value.trim().toLowerCase().split(' ').filter(Boolean))
Expand Down
7 changes: 3 additions & 4 deletions src/store/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ const currentCollectionId = ref('')
const loaded = ref(false)
const installed = ref(false)
const collection = shallowRef<CollectionMeta | null>(null)
const searchResult = useSearch(collection)

export function getSearchResults() {
return searchResult
}
export const getSearchResults = createSharedComposable(() => {
return useSearch(collection)
})

export function useCurrentCollection() {
return collection
Expand Down
8 changes: 8 additions & 0 deletions src/utils/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Check failure on line 1 in src/utils/query.ts

View workflow job for this annotation

GitHub Actions / lint

Too many blank lines at the beginning of file. Max of 0 allowed
export function cleanupQuery(query: Record<string, string | undefined | null>) {
for (const key of Object.keys(query)) {
if (!query[key])
delete query[key]
}
return query
}

Check failure on line 8 in src/utils/query.ts

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found

0 comments on commit 24a3a59

Please sign in to comment.