Skip to content

Commit

Permalink
[refactor] Remove search store
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Jan 30, 2023
1 parent d2a1c4a commit b362185
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 258 deletions.
98 changes: 74 additions & 24 deletions src/renderer/components/TimelineSpace/Contents/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,45 @@
</el-form>
</div>
<div class="search-result">
<search-account v-if="target === 'account'"></search-account>
<search-tag v-else-if="target === 'tag'"></search-tag>
<search-toots v-else-if="target === 'toot'"></search-toots>
<search-account v-if="target === 'account'" :results="accounts"></search-account>
<search-tag v-else-if="target === 'tag'" :results="tags"></search-tag>
<search-toots v-else-if="target === 'toot'" :results="statuses"></search-toots>
</div>
</div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { defineComponent, ref, onMounted, computed, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useI18next } from 'vue3-i18next'
import generator, { MegalodonInterface } from 'megalodon'
import { useStore } from '@/store'
import SearchAccount from './Search/Account.vue'
import SearchTag from './Search/Tag.vue'
import SearchToots from './Search/Toots.vue'
import { ACTION_TYPES as TAG_ACTION } from '@/store/TimelineSpace/Contents/Search/Tag'
import { ACTION_TYPES as ACCOUNT_ACTION } from '@/store/TimelineSpace/Contents/Search/Account'
import { ACTION_TYPES as TOOTS_ACTION } from '@/store/TimelineSpace/Contents/Search/Toots'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
import { MyWindow } from '~/src/types/global'
import { useRoute } from 'vue-router'
export default defineComponent({
name: 'search',
components: { SearchAccount, SearchTag, SearchToots },
setup() {
const space = 'TimelineSpace/Contents/Search'
const store = useStore()
const i18n = useI18next()
const route = useRoute()
const win = (window as any) as MyWindow
const id = computed(() => parseInt(route.params.id as string))
const target = ref<string>('account')
const query = ref<string>('')
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
const client = ref<MegalodonInterface | null>(null)
const searchTargets = [
{
target: 'account',
Expand All @@ -53,32 +63,69 @@ export default defineComponent({
label: i18n.t('search.toot')
}
]
const userAgent = computed(() => store.state.App.userAgent)
const accounts = ref<Array<Entity.Account>>([])
const tags = ref<Array<Entity.Tag>>([])
const statuses = ref<Array<Entity.Status>>([])
onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
client.value = generator(s.sns, s.baseURL, a.accessToken, userAgent.value)
})
const clear = () => {
accounts.value = []
tags.value = []
statuses.value = []
}
const search = () => {
clear()
switch (target.value) {
case 'account':
store.dispatch(`${space}/Account/${ACCOUNT_ACTION.SEARCH}`, query.value).catch(() => {
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
client.value
?.searchAccount(query.value, { resolve: true })
.then(res => {
accounts.value = res.data
})
.catch(err => {
console.error(err)
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
})
})
})
break
case 'tag':
store.dispatch(`${space}/Tag/${TAG_ACTION.SEARCH}`, `#${query.value}`).catch(() => {
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
client.value
?.search(`#${query.value}`, 'hashtags', { resolve: true })
.then(res => {
tags.value = res.data.hashtags
})
.catch(err => {
console.error(err)
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
})
})
})
break
case 'toot':
store.dispatch(`${space}/Toots/${TOOTS_ACTION.SEARCH}`, query.value).catch(() => {
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
client.value
?.search(query.value, 'statuses', { resolve: true })
.then(res => {
statuses.value = res.data.statuses
})
.catch(err => {
console.error(err)
ElMessage({
message: i18n.t('message.search_error'),
type: 'error'
})
})
})
break
default:
break
Expand All @@ -89,7 +136,10 @@ export default defineComponent({
target,
searchTargets,
query,
search
search,
accounts,
tags,
statuses
}
}
})
Expand Down
21 changes: 5 additions & 16 deletions src/renderer/components/TimelineSpace/Contents/Search/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,15 @@
</template>

<script lang="ts">
import { computed, defineComponent, onUnmounted } from 'vue'
import { useStore } from '@/store'
import { MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Search/Account'
import { defineComponent, PropType } from 'vue'
import { Entity } from 'megalodon'
import User from '@/components/molecules/User.vue'
export default defineComponent({
name: 'search-account',
components: { User },
setup() {
const store = useStore()
const results = computed(() => store.state.TimelineSpace.Contents.Search.Account.results)
onUnmounted(() => {
store.commit(`TimelineSpace/Contents/Search/Account/${MUTATION_TYPES.UPDATE_RESULTS}`, [])
})
return {
results
}
},
unmounted() {}
props: {
results: Object as PropType<Array<Entity.Account>>
}
})
</script>
18 changes: 4 additions & 14 deletions src/renderer/components/TimelineSpace/Contents/Search/Tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@
</template>

<script lang="ts">
import { defineComponent, onUnmounted, computed } from 'vue'
import { useStore } from '@/store'
import { defineComponent, PropType } from 'vue'
import { Entity } from 'megalodon'
import Tag from '@/components/molecules/Tag.vue'
import { MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Search/Tag'
export default defineComponent({
name: 'search-tag',
components: { Tag },
setup() {
const store = useStore()
const results = computed(() => store.state.TimelineSpace.Contents.Search.Tag.results)
onUnmounted(() => {
store.commit(`TimelineSpace/Contents/Search/Tag/${MUTATION_TYPES.UPDATE_RESULTS}`, [])
})
return {
results
}
props: {
results: Object as PropType<Array<Entity.Account>>
}
})
</script>
18 changes: 4 additions & 14 deletions src/renderer/components/TimelineSpace/Contents/Search/Toots.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@
</template>

<script lang="ts">
import { computed, defineComponent, onUnmounted } from 'vue'
import { useStore } from '@/store'
import { defineComponent, PropType } from 'vue'
import { Entity } from 'megalodon'
import Toot from '@/components/organisms/Toot.vue'
import { MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Search/Toots'
export default defineComponent({
name: 'search-account',
components: { Toot },
setup() {
const store = useStore()
const results = computed(() => store.state.TimelineSpace.Contents.Search.Toots.results)
onUnmounted(() => {
this.$store.commit(`TimelineSpace/Contents/Search/Toots/${MUTATION_TYPES.UPDATE_RESULTS}`, [])
})
return {
results
}
props: {
results: Object as PropType<Array<Entity.Account>>
}
})
</script>
5 changes: 1 addition & 4 deletions src/renderer/store/TimelineSpace/Contents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Home, { HomeState } from './Contents/Home'
import Notifications, { NotificationsState } from './Contents/Notifications'
import Local, { LocalState } from './Contents/Local'
import Search, { SearchModuleState } from './Contents/Search'
import DirectMessages, { DirectMessagesState } from './Contents/DirectMessages'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
Expand All @@ -15,7 +14,6 @@ type ContentsModule = {
Notifications: NotificationsState
DirectMessages: DirectMessagesState
Local: LocalState
Search: SearchModuleState
}

export type ContentsModuleState = ContentsModule & ContentsState
Expand Down Expand Up @@ -51,8 +49,7 @@ const Contents: Module<ContentsState, RootState> = {
Home,
Notifications,
Local,
DirectMessages,
Search
DirectMessages
},
mutations: mutations,
actions: actions
Expand Down
25 changes: 0 additions & 25 deletions src/renderer/store/TimelineSpace/Contents/Search.ts

This file was deleted.

53 changes: 0 additions & 53 deletions src/renderer/store/TimelineSpace/Contents/Search/Account.ts

This file was deleted.

Loading

0 comments on commit b362185

Please sign in to comment.