refs #850 Replace hashtag with typescript

This commit is contained in:
AkiraFukushima 2019-04-16 21:44:46 +09:00
parent 011df4a9a1
commit 153e5c312a
8 changed files with 33 additions and 27 deletions

View File

@ -4,7 +4,7 @@ import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex' import Vuex from 'vuex'
import { ipcMain } from '~/spec/mock/electron' import { ipcMain } from '~/spec/mock/electron'
import SideMenu, { SideMenuState } from '~/src/renderer/store/TimelineSpace/SideMenu' import SideMenu, { SideMenuState } from '~/src/renderer/store/TimelineSpace/SideMenu'
import Hashtag from '~/src/types/hashtag' import LocalTag from '~/src/types/localTag'
jest.mock('megalodon') jest.mock('megalodon')
@ -116,10 +116,10 @@ describe('SideMenu', () => {
describe('listTags', () => { describe('listTags', () => {
it('should be listed', async () => { it('should be listed', async () => {
const tag1: Hashtag = { const tag1: LocalTag = {
tagName: 'tag1' tagName: 'tag1'
} }
const tag2: Hashtag = { const tag2: LocalTag = {
tagName: 'tag2' tagName: 'tag2'
} }
ipcMain.once('list-hashtags', (event: any, _) => { ipcMain.once('list-hashtags', (event: any, _) => {

View File

@ -1,6 +1,6 @@
import i18n from '~/src/config/i18n' import i18n from '~/src/config/i18n'
import Jump, { JumpState, MUTATION_TYPES, Channel } from '@/store/TimelineSpace/Modals/Jump' import Jump, { JumpState, MUTATION_TYPES, Channel } from '@/store/TimelineSpace/Modals/Jump'
import Hashtag from '~/src/types/hashtag' import LocalTag from '~/src/types/localTag'
import { List } from 'megalodon' import { List } from 'megalodon'
describe('TimelineSpace/Modals/Jump', () => { describe('TimelineSpace/Modals/Jump', () => {
@ -87,10 +87,10 @@ describe('TimelineSpace/Modals/Jump', () => {
describe('updateTagChannel', () => { describe('updateTagChannel', () => {
it('should be updated', () => { it('should be updated', () => {
const whalebird: Hashtag = { const whalebird: LocalTag = {
tagName: 'whalebird' tagName: 'whalebird'
} }
const tqrk: Hashtag = { const tqrk: LocalTag = {
tagName: 'tqrk' tagName: 'tqrk'
} }
const channelList = [whalebird, tqrk] const channelList = [whalebird, tqrk]

View File

@ -1,19 +1,24 @@
import Datastore from 'nedb'
import LocalTag from '~/src/types/localTag'
export default class Hashtags { export default class Hashtags {
constructor (db) { private db: Datastore
constructor (db: Datastore) {
this.db = db this.db = db
this.db.ensureIndex({ fieldName: 'tagName', unique: true }, (_) => {}) this.db.ensureIndex({ fieldName: 'tagName', unique: true }, (_) => {})
} }
listTags () { listTags (): Promise<Array<LocalTag>> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.find({}, (err, docs) => { this.db.find<LocalTag>({}, (err, docs) => {
if (err) return reject(err) if (err) return reject(err)
resolve(docs) resolve(docs)
}) })
}) })
} }
insertTag (tag) { insertTag (tag: string): Promise<LocalTag> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.insert({ tagName: tag }, (err, doc) => { this.db.insert({ tagName: tag }, (err, doc) => {
if (err) return reject(err) if (err) return reject(err)
@ -22,11 +27,11 @@ export default class Hashtags {
}) })
} }
removeTag (tag) { removeTag (localTag: LocalTag): Promise<number> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.remove( this.db.remove(
{ {
tagName: tag.tagName tagName: localTag.tagName
}, },
{ multi: true }, { multi: true },
(err, numRemoved) => { (err, numRemoved) => {

View File

@ -21,6 +21,7 @@ import UnreadNotification from './unread_notification'
import i18n from '../config/i18n' import i18n from '../config/i18n'
import Language from '../constants/language' import Language from '../constants/language'
import LocalAccount from '~src/types/localAccount' import LocalAccount from '~src/types/localAccount'
import LocalTag from '~src/types/localTag'
/** /**
* Context menu * Context menu
@ -767,7 +768,7 @@ ipcMain.on('change-language', (event, value) => {
}) })
// hashtag // hashtag
ipcMain.on('save-hashtag', (event, tag) => { ipcMain.on('save-hashtag', (event: Event, tag: string) => {
const hashtags = new Hashtags(hashtagsDB) const hashtags = new Hashtags(hashtagsDB)
hashtags.insertTag(tag) hashtags.insertTag(tag)
.then(() => { .then(() => {
@ -778,7 +779,7 @@ ipcMain.on('save-hashtag', (event, tag) => {
}) })
}) })
ipcMain.on('list-hashtags', (event, _) => { ipcMain.on('list-hashtags', (event: Event, _) => {
const hashtags = new Hashtags(hashtagsDB) const hashtags = new Hashtags(hashtagsDB)
hashtags.listTags() hashtags.listTags()
.then((tags) => { .then((tags) => {
@ -789,7 +790,7 @@ ipcMain.on('list-hashtags', (event, _) => {
}) })
}) })
ipcMain.on('remove-hashtag', (event, tag) => { ipcMain.on('remove-hashtag', (event: Event, tag: LocalTag) => {
const hashtags = new Hashtags(hashtagsDB) const hashtags = new Hashtags(hashtagsDB)
hashtags.removeTag(tag) hashtags.removeTag(tag)
.then(() => { .then(() => {

View File

@ -1,10 +1,10 @@
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import Hashtag from '~/src/types/hashtag' import LocalTag from '~/src/types/localTag'
import { Module, MutationTree, ActionTree } from 'vuex' import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
export interface ListState { export interface ListState {
tags: Array<Hashtag> tags: Array<LocalTag>
} }
const state = (): ListState => ({ const state = (): ListState => ({
@ -16,7 +16,7 @@ export const MUTATION_TYPES = {
} }
const mutations: MutationTree<ListState> = { const mutations: MutationTree<ListState> = {
[MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<Hashtag>) => { [MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<LocalTag>) => {
state.tags = tags state.tags = tags
} }
} }
@ -24,7 +24,7 @@ const mutations: MutationTree<ListState> = {
const actions: ActionTree<ListState, RootState> = { const actions: ActionTree<ListState, RootState> = {
listTags: ({ commit }) => { listTags: ({ commit }) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (_, tags: Array<Hashtag>) => { ipcRenderer.once('response-list-hashtags', (_, tags: Array<LocalTag>) => {
ipcRenderer.removeAllListeners('error-list-hashtags') ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags) commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags) resolve(tags)
@ -36,7 +36,7 @@ const actions: ActionTree<ListState, RootState> = {
ipcRenderer.send('list-hashtags') ipcRenderer.send('list-hashtags')
}) })
}, },
removeTag: ({ dispatch }, tag: Hashtag) => { removeTag: ({ dispatch }, tag: LocalTag) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ipcRenderer.once('response-remove-hashtag', () => { ipcRenderer.once('response-remove-hashtag', () => {
ipcRenderer.removeAllListeners('error-remove-hashtag') ipcRenderer.removeAllListeners('error-remove-hashtag')

View File

@ -2,7 +2,7 @@ import router from '@/router'
import i18n from '~/src/config/i18n' import i18n from '~/src/config/i18n'
import { Module, MutationTree, ActionTree } from 'vuex' import { Module, MutationTree, ActionTree } from 'vuex'
import { List } from 'megalodon' import { List } from 'megalodon'
import Hashtag from '~/src/types/hashtag' import LocalTag from '~/src/types/localTag'
import { RootState } from '@/store' import { RootState } from '@/store'
export interface Channel { export interface Channel {
@ -95,7 +95,7 @@ const mutations: MutationTree<JumpState> = {
return channel return channel
}) })
}, },
[MUTATION_TYPES.UPDATE_TAG_CHANNEL]: (state, tags: Array<Hashtag>) => { [MUTATION_TYPES.UPDATE_TAG_CHANNEL]: (state, tags: Array<LocalTag>) => {
state.tagChannelList = tags.map(t => { state.tagChannelList = tags.map(t => {
const channel: Channel = { const channel: Channel = {
name: `#${t.tagName}`, name: `#${t.tagName}`,

View File

@ -1,7 +1,7 @@
import Mastodon, { List, Response } from 'megalodon' import Mastodon, { List, Response } from 'megalodon'
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree } from 'vuex' import { Module, MutationTree, ActionTree } from 'vuex'
import Hashtag from '~/src/types/hashtag' import LocalTag from '~/src/types/localTag'
import LocalAccount from '~/src/types/localAccount' import LocalAccount from '~/src/types/localAccount'
import { RootState } from '@/store' import { RootState } from '@/store'
@ -13,7 +13,7 @@ export interface SideMenuState {
unreadDirectMessagesTimeline: boolean, unreadDirectMessagesTimeline: boolean,
unreadPublicTimeline: boolean, unreadPublicTimeline: boolean,
lists: Array<List>, lists: Array<List>,
tags: Array<Hashtag>, tags: Array<LocalTag>,
collapse: boolean collapse: boolean
} }
@ -66,7 +66,7 @@ const mutations: MutationTree<SideMenuState> = {
[MUTATION_TYPES.CHANGE_COLLAPSE]: (state, collapse: boolean) => { [MUTATION_TYPES.CHANGE_COLLAPSE]: (state, collapse: boolean) => {
state.collapse = collapse state.collapse = collapse
}, },
[MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<Hashtag>) => { [MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<LocalTag>) => {
state.tags = tags state.tags = tags
} }
} }
@ -105,7 +105,7 @@ const actions: ActionTree<SideMenuState, RootState> = {
}, },
listTags: ({ commit }) => { listTags: ({ commit }) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (_, tags: Array<Hashtag>) => { ipcRenderer.once('response-list-hashtags', (_, tags: Array<LocalTag>) => {
ipcRenderer.removeAllListeners('error-list-hashtags') ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags) commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags) resolve(tags)

View File

@ -1,4 +1,4 @@
export default interface Hasthag { export default interface LocalTag {
tagName: string, tagName: string,
_id?: string _id?: string
} }