circle-ihk
Advanced tools
Comparing version 1.0.0-6 to 1.0.0-7
@@ -8,2 +8,5 @@ import { App, Plugin } from '../interface'; | ||
container: HTMLElement; | ||
dependencies: { | ||
[index: string]: any; | ||
}; | ||
}; |
@@ -5,2 +5,3 @@ interface IProps { | ||
disabled?: boolean; | ||
monitorFields?: boolean; | ||
} | ||
@@ -7,0 +8,0 @@ export default function useOption(props: IProps): { |
import useApp from './useApp'; | ||
import { useState, useEffect } from 'react'; | ||
import { isUndefined, isFunction } from '../utils/is'; | ||
import { isUndefined, isFunction, isObject } from '../utils/is'; | ||
export default function useOption(props) { | ||
const { id, disabled, defaultValue } = props; | ||
const { id, disabled, defaultValue, monitorFields = true } = props; | ||
const { app, me, container } = useApp(); | ||
@@ -37,2 +37,23 @@ const [value, setValue] = useState(defaultValue); | ||
useEffect(refetch, [id]); | ||
useEffect(() => { | ||
if (!monitorFields || !isObject(defaultValue)) { | ||
return; | ||
} | ||
const keysToMonitor = Object.keys(defaultValue); | ||
if (keysToMonitor.length <= 0) { | ||
return; | ||
} | ||
const hooksToMonitor = []; | ||
keysToMonitor.forEach((key) => { | ||
hooksToMonitor.push(app.on(`${['option', 'display'].includes(id) ? me.id : id}_${key}`, refetch)); | ||
}); | ||
return () => { | ||
if (hooksToMonitor.length <= 0) { | ||
return; | ||
} | ||
hooksToMonitor.forEach((hookToMonitor) => { | ||
hookToMonitor(); | ||
}); | ||
}; | ||
}, [me.id, monitorFields, defaultValue, refetch]); | ||
return { | ||
@@ -39,0 +60,0 @@ me, |
@@ -35,10 +35,13 @@ import { useState } from 'react'; | ||
return getToken() | ||
.then((token) => { | ||
return app.fetch('user/current', { | ||
headers: { | ||
'X-CSRF-Token': token, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
.then((token) => app | ||
.fetch('user/current', { | ||
headers: { | ||
'X-CSRF-Token': token, | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
.then((current) => Promise.resolve({ | ||
...current, | ||
token, | ||
}))) | ||
.then((result) => { | ||
@@ -49,3 +52,2 @@ return app | ||
...result, | ||
member: result && result.roles && result.roles.includes('member'), | ||
})) | ||
@@ -58,31 +60,2 @@ .then(() => { | ||
}); | ||
}) | ||
.catch(() => { | ||
if (user.auth) { | ||
return app | ||
.fetch('user/new_token', { | ||
method: 'POST', | ||
headers: { | ||
token: user.auth, | ||
'Content-Type': 'application/json', | ||
}, | ||
query: { | ||
id: user.uid, | ||
}, | ||
}) | ||
.then((token) => { | ||
return app | ||
.set('user', btoa({ | ||
...user, | ||
token, | ||
})) | ||
.then(() => { | ||
return app.syncUser().then((val) => { | ||
setUser(val); | ||
return Promise.resolve(true); | ||
}); | ||
}); | ||
}); | ||
} | ||
return Promise.resolve(false); | ||
}); | ||
@@ -121,3 +94,2 @@ }; | ||
// ...result, | ||
// member: result && result.roles && result.roles.includes('member'), | ||
// }; | ||
@@ -146,3 +118,5 @@ // if (!remeberMe && loginData.auth) { | ||
.finally(() => { | ||
return app.set('user', btoa({ uid: user.uid })).then(() => { | ||
return app | ||
.set('user', btoa({ uid: user.uid, roles: [] })) | ||
.then(() => { | ||
return app.syncUser().then((val) => { | ||
@@ -149,0 +123,0 @@ setUser(val); |
@@ -16,2 +16,2 @@ export { default as AppContext } from './hook'; | ||
export { User, App, Plugin, IData, Match, Query, Pager } from './interface'; | ||
export { isType, isChinese, isUndefined, isString, isElement, isNumber, isFunction, isBoolean, isObject, isAsyncFunction, } from './utils/is'; | ||
export { isType, isTextNode, isChinese, isUndefined, isString, isElement, isNumber, isFunction, isBoolean, isObject, isAsyncFunction, } from './utils/is'; |
@@ -15,2 +15,2 @@ export { default as AppContext } from './hook'; | ||
export { default as keyboard } from './utils/keyboard'; | ||
export { isType, isChinese, isUndefined, isString, isElement, isNumber, isFunction, isBoolean, isObject, isAsyncFunction, } from './utils/is'; | ||
export { isType, isTextNode, isChinese, isUndefined, isString, isElement, isNumber, isFunction, isBoolean, isObject, isAsyncFunction, } from './utils/is'; |
export interface User { | ||
auth?: string; | ||
token?: string; | ||
member: boolean; | ||
uid?: string; | ||
@@ -10,2 +9,3 @@ name?: string; | ||
mail?: string; | ||
roles: Array<string>; | ||
} | ||
@@ -24,4 +24,3 @@ export interface Plugin { | ||
author?: string; | ||
debug?: boolean; | ||
isPro?: boolean; | ||
pro?: boolean; | ||
homepage?: string; | ||
@@ -110,4 +109,4 @@ dependencies?: string[]; | ||
remove: (id: string | Array<string>, table?: string) => Promise<any>; | ||
list: (query?: Query, pager?: Pager) => Promise<any>; | ||
option: (id: string, value?: any) => Promise<any>; | ||
list: (query?: Query, pager?: Pager, table?: string) => Promise<any>; | ||
option: (id?: string, value?: any) => Promise<any>; | ||
log: (...args: any) => string; | ||
@@ -131,3 +130,3 @@ action: (id?: 'ready' | 'enable' | 'disable' | 'force') => void; | ||
tabs: (action: 'create' | 'update' | 'remove' | 'captureVisibleTab' | 'query', value?: any) => Promise<any>; | ||
windows: (action: 'current' | 'create' | 'get' | 'update' | 'remove', value: number | { | ||
windows: (action: 'current' | 'create' | 'get' | 'update' | 'remove', value?: number | { | ||
id?: number; | ||
@@ -143,4 +142,4 @@ url?: string; | ||
syncUser: () => Promise<User>; | ||
syncColorScheme: () => void; | ||
cron: (force?: boolean) => Promise<boolean>; | ||
getApp(id: string | Plugin): Promise<Plugin>; | ||
listApp: (match?: Match, pager?: Pager) => Promise<Array<Plugin>>; | ||
@@ -152,2 +151,3 @@ enable: (id: string | Plugin) => Promise<boolean>; | ||
apply: (runAt: string, scope?: string) => Promise<boolean>; | ||
dynamicRun(id: string | Plugin): Promise<boolean>; | ||
i18n: (...args: Array<string>) => string; | ||
@@ -154,0 +154,0 @@ fetch: (url: string, options?: { |
export declare function isType(value: any, type: string): boolean; | ||
export declare function isUndefined(value: any): boolean; | ||
export declare function isString(value: any): boolean; | ||
export declare function isTextNode(value: any): boolean; | ||
export declare function isElement(value: any): any; | ||
@@ -5,0 +6,0 @@ export declare function isNumber(value: any): boolean; |
@@ -10,2 +10,5 @@ export function isType(value, type) { | ||
} | ||
export function isTextNode(value) { | ||
return isType(value, 'Text'); | ||
} | ||
export function isElement(value) { | ||
@@ -12,0 +15,0 @@ return value && value.nodeType && value.nodeType === 1; |
{ | ||
"name": "circle-ihk", | ||
"version": "1.0.0-6", | ||
"version": "1.0.0-7", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Circle Reader Component Library", |
33402
57
1014