pleroma-fe/src/components/search/search.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2026-06-03 02:19:25 +03:00
import { map, uniqBy } from 'lodash'
2026-01-08 17:26:52 +02:00
import Conversation from 'src/components/conversation/conversation.vue'
import FollowCard from 'src/components/follow_card/follow_card.vue'
2026-06-04 21:59:09 +03:00
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
2026-08-20 21:00:36 +03:00
import { useSearchStore } from 'src/stores/search.js'
2026-08-10 22:26:22 +03:00
import { useStatusesStore } from 'src/stores/statuses.js'
2026-08-10 15:00:59 +03:00
import { useUsersStore } from 'src/stores/users.js'
2026-01-08 17:26:52 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import { faCircleNotch, faSearch } from '@fortawesome/free-solid-svg-icons'
2026-01-06 16:22:52 +02:00
library.add(faCircleNotch, faSearch)
2019-07-15 16:42:27 +00:00
const Search = {
components: {
FollowCard,
Conversation,
2026-01-06 16:22:52 +02:00
TabSwitcher,
2019-07-15 16:42:27 +00:00
},
2026-01-06 16:22:52 +02:00
props: ['query'],
data() {
2019-07-15 16:42:27 +00:00
return {
loaded: false,
loading: false,
searchTerm: this.query || '',
userIds: [],
statuses: [],
hashtags: [],
currenResultTab: 'statuses',
statusesOffset: 0,
lastStatusFetchCount: 0,
2026-01-06 16:22:52 +02:00
lastQuery: '',
2019-07-15 16:42:27 +00:00
}
},
computed: {
2026-01-06 16:22:52 +02:00
users() {
2026-08-10 15:00:59 +03:00
return this.userIds.map((userId) => useUsersStore().findUser(userId))
2019-07-15 16:42:27 +00:00
},
2026-01-06 16:22:52 +02:00
visibleStatuses() {
2026-08-10 22:26:22 +03:00
const allStatuses = useStatusesStore().allStatuses
2019-07-15 16:42:27 +00:00
2026-01-06 16:22:52 +02:00
return this.statuses.filter(
(status) =>
2026-08-10 22:26:22 +03:00
allStatuses.has(status.id) && !allStatuses.get(status.id).deleted,
2019-07-15 16:42:27 +00:00
)
2026-01-06 16:22:52 +02:00
},
2019-07-15 16:42:27 +00:00
},
2026-01-06 16:22:52 +02:00
mounted() {
2019-07-15 16:42:27 +00:00
this.search(this.query)
},
watch: {
2026-01-06 16:22:52 +02:00
query(newValue) {
2019-07-15 16:42:27 +00:00
this.searchTerm = newValue
this.search(newValue)
2026-01-06 16:22:52 +02:00
},
2019-07-15 16:42:27 +00:00
},
methods: {
2026-01-06 16:22:52 +02:00
newQuery(query) {
2019-07-15 16:42:27 +00:00
this.$router.push({ name: 'search', query: { query } })
this.$refs.searchInput.focus()
},
2026-01-06 16:22:52 +02:00
search(query, searchType = null) {
2019-07-15 16:42:27 +00:00
if (!query) {
this.loading = false
return
}
this.loading = true
this.$refs.searchInput.blur()
if (this.lastQuery !== query) {
this.userIds = []
this.hashtags = []
this.statuses = []
this.statusesOffset = 0
this.lastStatusFetchCount = 0
}
2019-07-15 16:42:27 +00:00
2026-08-20 21:00:36 +03:00
useSearchStore()
.search({
2026-01-06 16:22:52 +02:00
q: query,
resolve: true,
offset: this.statusesOffset,
type: searchType,
})
.then((data) => {
2019-07-15 16:42:27 +00:00
this.loading = false
2022-11-21 22:17:33 +02:00
const oldLength = this.statuses.length
// Always append to old results. If new results are empty, this doesn't change anything
this.userIds = this.userIds.concat(map(data.accounts, 'id'))
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
this.hashtags = this.hashtags.concat(data.hashtags)
2019-07-15 16:42:27 +00:00
this.currenResultTab = this.getActiveTab()
this.loaded = true
// Offset from whatever we already have
this.statusesOffset = this.statuses.length
// Because the amount of new statuses can actually be zero, compare to old lenght instead
this.lastStatusFetchCount = this.statuses.length - oldLength
this.lastQuery = query
2019-07-15 16:42:27 +00:00
})
},
2026-01-06 16:22:52 +02:00
resultCount(tabName) {
2019-07-15 16:42:27 +00:00
const length = this[tabName].length
return length === 0 ? '' : ` (${length})`
},
2026-01-06 16:22:52 +02:00
onResultTabSwitch(key) {
this.currenResultTab = key
2019-07-15 16:42:27 +00:00
},
2026-01-06 16:22:52 +02:00
getActiveTab() {
2019-07-15 16:42:27 +00:00
if (this.visibleStatuses.length > 0) {
return 'statuses'
} else if (this.users.length > 0) {
return 'people'
} else if (this.hashtags.length > 0) {
return 'hashtags'
}
return 'statuses'
},
2026-01-06 16:22:52 +02:00
lastHistoryRecord(hashtag) {
2026-08-04 18:04:20 +03:00
return hashtag.history?.[0]
2026-01-06 16:22:52 +02:00
},
},
2019-07-15 16:42:27 +00:00
}
export default Search