forked from ruiii/plugin-Hairstrength
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
127 lines (113 loc) · 3.48 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React from 'react'
import styled from 'styled-components'
import { basicSelector } from 'views/utils/selectors'
import { magicManager } from './lib/magic'
import { getAchieveData, saveArchiveData } from './lib/util'
import { Archive } from './lib/type'
import { Info } from './views/info'
import { store } from 'views/create-store'
import { Dispatcher, observe, observer } from 'redux-observers'
import { APIBasic } from 'kcsapi/api_port/port/response'
import { PluginState } from './reducers'
import { pick } from 'lodash'
import moment from 'moment-timezone'
import { pluginDataSelector } from './selectors'
import { Calculator } from './views/calculator'
import { Calender } from './views/calendar'
import { InitializeAction } from './reducers/actions'
export { reducer } from './reducers'
const Row = styled.div`
display: flex;
`
const Col = styled.div`
flex: 1;
`
const basicObserverCallback: Dispatcher<APIBasic> = async (dispatch, current, previous) => {
if (current.api_member_id !== previous?.api_member_id) {
const memberId = parseInt(current.api_member_id)
const archive = await getAchieveData(memberId)
magicManager.initialize(archive.magic, current.api_nickname, memberId)
dispatch({
type: '@@poi-plugin-senka-calc/initialize',
archive,
} as InitializeAction)
}
}
const pluginDataObserverCallback: Dispatcher<PluginState> = async (dispatch, current) => {
if (magicManager.isInitialized() && Object.keys(current.experienceHistory || {}).length > 0) {
const archiveData: Archive = {
...pick(
current,
'experienceHistory',
'rank5',
'rank20',
'rank100',
'rank501',
'rankUser',
'exHistory',
'questHistory',
'currentRank',
'targetSenka',
'excludedQuests',
),
magic: magicManager.magic,
date: moment.tz('Asia/Tokyo').format(),
}
const memberId = magicManager.memberId
await saveArchiveData(memberId, archiveData)
}
}
const basicObserver = observer(
basicSelector,
basicObserverCallback
)
const pluginDataObserver = observer(
pluginDataSelector,
pluginDataObserverCallback
)
let unsuscribeObserver: () => void = () => undefined
let timer: ReturnType<typeof setInterval> | null
let endOfMonth = moment.tz('Asia/Tokyo').endOf('month')
export const pluginDidLoad = () => {
unsuscribeObserver = observe(store, [basicObserver, pluginDataObserver], { skipInitialCall: false })
timer = setInterval(async () => {
if (moment.tz('Asia/Tokyo').isAfter(endOfMonth)) {
endOfMonth = moment.tz('Asia/Tokyo').endOf('month')
const archive = await getAchieveData(magicManager.memberId)
store.dispatch({
type: '@@poi-plugin-senka-calc/initialize',
archive,
} as InitializeAction)
}
}, 1000)
}
export const pluginWillUnload = () => {
unsuscribeObserver()
unsuscribeObserver = () => undefined
if (timer) {
clearInterval(timer)
}
timer = null
}
const requiredCSS = [
'@blueprintjs/select/lib/css/blueprint-select.css',
'@blueprintjs/popover2/lib/css/blueprint-popover2.css',
'@blueprintjs/datetime/lib/css/blueprint-datetime.css',
]
export const reactClass: React.FC = () => {
return (
<Row>
{requiredCSS.map(css => <link key={css} href={require.resolve(css)} rel="stylesheet" />)}
<Col>
<Info />
<Calender />
</Col>
<Col>
<Calculator />
</Col>
</Row>
)
}
export const switchPluginPath = [
'/kcsapi/api_req_ranking/mxltvkpyuklh',
]