Skip to content

Commit

Permalink
refactor(forum): 更新实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Burial0268 committed Jul 21, 2023
1 parent 1b588c2 commit 8f7e369
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
41 changes: 22 additions & 19 deletions js/src/forum/ProcessData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,33 @@
import { NestedStringArray } from '@askvortsov/rich-icu-message-formatter'
import ipinfo from './Model/IPInfo'

export type Data = {
code: NestedStringArray
region: NestedStringArray
isp: NestedStringArray
} & Record<string, NestedStringArray>

export default class ProcessData {
private region: NestedStringArray
private code: NestedStringArray
private isp: NestedStringArray
private data: Data

constructor(ipInfo: ipinfo) {
this.region = ipInfo.region()
this.code = ipInfo.countryCode()
this.isp = ipInfo.isp()
this.data = {
region: ipInfo.region(),
code: ipInfo.countryCode(),
isp: ipInfo.isp()
}
}

process(errorNotice: NestedStringArray) {
return [this.region, this.code, this.isp].reduce(
(acc, el, index) => {
let count = acc.count
if (el === null || el === undefined || el === '') {
++count
el = errorNotice
}
acc.elements[index] = el
acc.count = count
return acc
},
{ count: 0, elements: [] as NestedStringArray[] }
)
const elements = {} as Data
let errorCount = 0

for (const [key, value] of Object.entries(this.data)) {
if (errorCount > 2) return false
errorCount = value ? errorCount : ++errorCount
elements[key] = value || errorNotice
}

return elements
}
}
11 changes: 5 additions & 6 deletions js/src/forum/components/GeoIpToolBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
*/

import Component, { ComponentAttrs } from 'flarum/common/Component'
import { Data } from '../ProcessData'

export interface GeoIpBarAttrs extends ComponentAttrs {
code: string
region: string
isp: string
elements: Data
}

export default class GeoIpToolBar<
CustomAttrs extends GeoIpBarAttrs = GeoIpBarAttrs
> extends Component<CustomAttrs> {
view() {
const { code, region, isp } = this.attrs
const { elements } = this.attrs
return (
<div className='userIp-container'>
<div className='ip-locate' id='info-country'>
{`${region}, ${code}`}
{`${elements["region"]}, ${elements["code"]}`}
</div>
<div className='ip-locate' id='info-isp'>
{`${isp}`}
{`${elements["isp"]}`}
</div>
</div>
)
Expand Down
7 changes: 1 addition & 6 deletions js/src/forum/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ app.initializers.add('gbcl/userip', () => {
if (!ipInfo) return

const result = new ProcessData(ipInfo).process(errorNotice)
const [reg, code, serv] = result.elements
const errorCount = result.count

if (errorCount < 2) {
items.add('userIp', <GeoIpToolBar region={reg} code={code} isp={serv} />)
}
result && items.add('userIp', <GeoIpToolBar elements={result} />)
})
})

0 comments on commit 8f7e369

Please sign in to comment.