@intra-mart/smartlime 2.0.0-dev.20241125 → 2.0.0-dev.20251017
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/eslint.config.mjs +50 -0
- package/lib/_shared/renderTarget.d.ts +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/packages/Copilot/IMCopilotError.d.ts +4 -0
- package/lib/packages/Copilot/IMCopilotError.js +10 -0
- package/lib/packages/Copilot/hooks/index.d.ts +1 -0
- package/lib/packages/Copilot/hooks/index.js +1 -0
- package/lib/packages/Copilot/hooks/useAssistant/index.d.ts +47 -0
- package/lib/packages/Copilot/hooks/useAssistant/index.js +194 -0
- package/lib/packages/Copilot/hooks/useAssistant/jsonParse.d.ts +2 -0
- package/lib/packages/Copilot/hooks/useAssistant/jsonParse.js +18 -0
- package/lib/packages/Copilot/hooks/useAssistant/types.d.ts +52 -0
- package/lib/packages/Copilot/hooks/useAssistant/types.js +1 -0
- package/lib/packages/Fetch/index.d.ts +4 -2
- package/lib/packages/Fetch/index.js +1 -0
- package/lib/packages/Me/Context.d.ts +0 -1
- package/lib/packages/Me/index.d.ts +3 -3
- package/lib/packages/Me/index.js +1 -1
- package/lib/packages/Me/type.d.ts +1 -0
- package/lib/packages/OAuth/Context.d.ts +0 -1
- package/lib/packages/OAuth/hooks/index.d.ts +1 -1
- package/lib/packages/OAuth/hooks/index.js +1 -1
- package/lib/packages/OAuth/hooks/useAuthInitError.d.ts +3 -0
- package/lib/packages/OAuth/hooks/useAuthInitError.js +24 -0
- package/lib/packages/OAuth/index.d.ts +2 -2
- package/lib/packages/OAuth/index.js +10 -10
- package/lib/packages/Search/Context.d.ts +0 -1
- package/lib/packages/Search/hooks/useDepartmentPostSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useDepartmentSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useDepartmentTopSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useIMSearch.d.ts +1 -1
- package/lib/packages/Search/hooks/usePostSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/usePublicGroupRoleSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/usePublicGroupSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/usePublicGroupTopSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useRoleSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useUserAdditionalInformationSearch.d.ts +2 -1
- package/lib/packages/Search/hooks/useUserSearch.d.ts +2 -1
- package/lib/packages/Search/index.d.ts +3 -3
- package/lib/packages/Search/index.js +1 -1
- package/lib/packages/Search/types.d.ts +2 -1
- package/lib/packages/Session/Context.d.ts +0 -1
- package/lib/packages/Session/hooks/useSession.d.ts +0 -1
- package/lib/packages/Session/hooks/useSessionState.d.ts +0 -1
- package/lib/packages/Session/index.d.ts +2 -2
- package/lib/packages/Session/index.js +29 -24
- package/lib/packages/Smartlime/NotSearchPropError.d.ts +0 -1
- package/lib/packages/Smartlime/index.d.ts +2 -2
- package/lib/packages/Tenant/Context.d.ts +13 -6
- package/lib/packages/Tenant/constants.d.ts +2 -0
- package/lib/packages/Tenant/constants.js +4 -0
- package/lib/packages/Tenant/hooks/index.d.ts +2 -2
- package/lib/packages/Tenant/hooks/index.js +2 -2
- package/lib/packages/Tenant/hooks/useIMModules.d.ts +6 -0
- package/lib/packages/Tenant/hooks/useIMModules.js +79 -0
- package/lib/packages/Tenant/hooks/useIMModulesState.d.ts +2 -0
- package/lib/packages/Tenant/hooks/{useIMFetchModuleState.js → useIMModulesState.js} +3 -4
- package/lib/packages/Tenant/index.d.ts +4 -16
- package/lib/packages/Tenant/index.js +16 -83
- package/lib/packages/Tenant/modulesType.d.ts +1522 -2
- package/lib/packages/Tenant/modulesType.js +1522 -2
- package/lib/packages/WebView/InternalIMWebView.d.ts +1 -1
- package/lib/packages/WebView/InternalIMWebView.js +6 -6
- package/lib/packages/WebView/hooks/useHackSearchUI.d.ts +1 -2
- package/lib/packages/WebView/index.js +8 -8
- package/lib/utils/mergeRef.d.ts +1 -2
- package/package.json +29 -34
- package/lib/packages/OAuth/hooks/useAuthError.d.ts +0 -2
- package/lib/packages/OAuth/hooks/useAuthError.js +0 -15
- package/lib/packages/Tenant/hooks/useIMFetchModuleState.d.ts +0 -2
- package/lib/packages/Tenant/hooks/useIMModuleInfo.d.ts +0 -6
- package/lib/packages/Tenant/hooks/useIMModuleInfo.js +0 -13
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { DepartmentSearcResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const useDepartmentSearch: (paramString: string) => (keyword: string, init?: FetchRequestInit) => Promise<DepartmentSearcResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { DepartmentTopSearcResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const useDepartmentTopSearch: (paramString: string) => (init?: FetchRequestInit) => Promise<DepartmentTopSearcResult>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Context as DefaultContext } from '../Context';
|
|
2
2
|
export declare const useIMSearch: (Context?: typeof DefaultContext) => {
|
|
3
|
-
startSearch: ({ target, type, paramString, }: import("..").StartSearchArg) => Promise<import("..").StartSearchResult<
|
|
3
|
+
startSearch: ({ target, type, paramString, }: import("..").StartSearchArg) => Promise<import("..").StartSearchResult<typeof type>>;
|
|
4
4
|
};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { DepartmentSet, PostSearcResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const usePostSearch: (paramString: string) => (departmentSet: DepartmentSet, init?: FetchRequestInit) => Promise<PostSearcResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { PublicGroupRoleSearchResult, PublicGroupRoleSet } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const usePublicGroupRoleSearch: (paramString: string) => (publicGroupSet: PublicGroupRoleSet, init?: FetchRequestInit) => Promise<PublicGroupRoleSearchResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { PublicGroupSearcResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const usePublicGroupSearch: (paramString: string) => (keyword: string, init?: FetchRequestInit) => Promise<PublicGroupSearcResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { PublicGroupTopSearchResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const usePublicGroupTopSearch: (paramString: string) => (init?: FetchRequestInit) => Promise<PublicGroupTopSearchResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { PublicGroupSet, RoleSearchResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const useRoleSearch: (paramString: string) => (publicGroupSet: PublicGroupSet, init?: FetchRequestInit) => Promise<RoleSearchResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { UserAdditionalInformationSearchResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const useUserAdditionalInformationSearch: (paramString: string) => (userCd: string, init?: FetchRequestInit) => Promise<UserAdditionalInformationSearchResult>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { UserSearchResult } from '../types';
|
|
2
|
-
|
|
2
|
+
import { FetchRequestInit } from 'expo/fetch';
|
|
3
|
+
export declare const useUserSearch: (paramString: string) => (keyword: string, init?: FetchRequestInit) => Promise<UserSearchResult>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { JSX } from 'react';
|
|
2
2
|
import { Context as DefaultContext } from './Context';
|
|
3
3
|
import { DepartmentPostSearchRecord, DepartmentSearchRecord, PostSearchRecord, PublicGroupSearchRecord, RoleSearchRecord, UserSearchRecord } from './types';
|
|
4
4
|
export type SearchTarget = 'jp.co.intra_mart.master.search.user' | 'jp.co.intra_mart.master.search.company' | 'jp.co.intra_mart.master.search.company_post' | 'jp.co.intra_mart.master.search.department' | 'jp.co.intra_mart.master.search.public_group' | 'jp.co.intra_mart.master.search.private_group' | 'jp.co.intra_mart.master.search.public_group_role' | 'jp.co.intra_mart.master.search.role' | 'jp.co.intra_mart.master.search.attached_department_post' | 'jp.co.intra_mart.master.search.attached_public_group_role' | undefined;
|
|
5
5
|
export type SearchType = 'single' | 'multiple';
|
|
6
|
-
declare const useLock: () => import("react").
|
|
6
|
+
declare const useLock: () => import("react").RefObject<boolean>;
|
|
7
7
|
declare const useVisible: () => [boolean, import("react").Dispatch<import("react").SetStateAction<boolean>>];
|
|
8
8
|
declare const useSearchTarget: () => [SearchTarget, import("react").Dispatch<import("react").SetStateAction<SearchTarget>>];
|
|
9
9
|
declare const useSearchType: () => [SearchType, import("react").Dispatch<import("react").SetStateAction<SearchType>>];
|
|
@@ -45,5 +45,5 @@ interface IMSearchProps {
|
|
|
45
45
|
searchContext?: typeof DefaultContext;
|
|
46
46
|
SearchComponent: (props: SelectSearchProps) => JSX.Element;
|
|
47
47
|
}
|
|
48
|
-
export declare const IMSearch: ({ children, searchContext, SearchComponent, }: IMSearchProps) =>
|
|
48
|
+
export declare const IMSearch: ({ children, searchContext, SearchComponent, }: IMSearchProps) => JSX.Element;
|
|
49
49
|
export {};
|
|
@@ -53,7 +53,7 @@ const useSearch = (setIsVisible, setSearchTarget, setSearchType, setParamString,
|
|
|
53
53
|
};
|
|
54
54
|
export const IMSearch = ({ children, searchContext, SearchComponent, }) => {
|
|
55
55
|
const Context = searchContext || DefaultContext;
|
|
56
|
-
const contextRef = useRef();
|
|
56
|
+
const contextRef = useRef(null);
|
|
57
57
|
const lockRef = useLock();
|
|
58
58
|
const [isVisible, setIsVisible] = useVisible();
|
|
59
59
|
const [searchTarget, setSearchTarget] = useSearchTarget();
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FetchResponse } from 'expo/build/winter/fetch/FetchResponse';
|
|
1
2
|
export interface SuccessResponse<T> {
|
|
2
3
|
error: false;
|
|
3
4
|
message: string;
|
|
@@ -10,7 +11,7 @@ export interface Success<T> {
|
|
|
10
11
|
export interface Error {
|
|
11
12
|
readonly type: 'error';
|
|
12
13
|
readonly detail: unknown;
|
|
13
|
-
readonly response:
|
|
14
|
+
readonly response: FetchResponse;
|
|
14
15
|
}
|
|
15
16
|
export interface Exception {
|
|
16
17
|
readonly type: 'exception';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { JSX } from 'react';
|
|
2
2
|
import { Context as DefaultContext } from './Context';
|
|
3
3
|
export interface SessionResponse {
|
|
4
4
|
error: boolean;
|
|
@@ -14,4 +14,4 @@ export interface IMSessionProps {
|
|
|
14
14
|
children: JSX.Element;
|
|
15
15
|
sessionContext?: typeof DefaultContext;
|
|
16
16
|
}
|
|
17
|
-
export declare const IMSession: ({ children, sessionContext }: IMSessionProps) =>
|
|
17
|
+
export declare const IMSession: ({ children, sessionContext }: IMSessionProps) => JSX.Element;
|
|
@@ -37,26 +37,33 @@ const useSessionStateRef = (renderTarget) => {
|
|
|
37
37
|
};
|
|
38
38
|
const useLogout = () => {
|
|
39
39
|
const imFetch = useIMFetch({ noValidate: true });
|
|
40
|
-
return useCallback((cookie) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
return useCallback(async (cookie) => {
|
|
41
|
+
const headers = cookie
|
|
42
|
+
? { Cookie: cookie }
|
|
43
|
+
: {};
|
|
44
|
+
await imFetch('/logout', {
|
|
45
|
+
credentials: 'include',
|
|
46
|
+
headers: headers,
|
|
45
47
|
method: 'GET',
|
|
46
48
|
});
|
|
47
|
-
}, []);
|
|
49
|
+
}, [imFetch]);
|
|
48
50
|
};
|
|
49
51
|
const makeEncodeParams = (clientTypeId = 'pc') => {
|
|
50
52
|
return `${`clientTypeId=${encodeURIComponent(clientTypeId)}`}`;
|
|
51
53
|
};
|
|
52
|
-
const useRequest = (setter, getter) => {
|
|
54
|
+
const useRequest = (setter, getter, logout) => {
|
|
53
55
|
const imFetch = useIMFetch();
|
|
54
|
-
const logout = useLogout();
|
|
55
56
|
const request = useCallback(async (clientTypeId) => {
|
|
56
57
|
try {
|
|
57
58
|
const currentCookies = getter()?.cookies;
|
|
59
|
+
const cookie = currentCookies ? makeCookie(currentCookies) : null;
|
|
60
|
+
// previous session may still be active, so execute the logout process
|
|
61
|
+
await logout();
|
|
62
|
+
if (cookie)
|
|
63
|
+
await logout(cookie);
|
|
58
64
|
const params = makeEncodeParams(clientTypeId);
|
|
59
65
|
const response = await imFetch(`api/bearer/smacolow/session?${params}`, {
|
|
66
|
+
credentials: 'include',
|
|
60
67
|
headers: {
|
|
61
68
|
Accept: 'application/json',
|
|
62
69
|
'X-Intramart-Session': 'keep', // https://document.intra-mart.jp/library/iap/public/im_web_api_maker/im_web_api_maker_programming_guide/texts/how_to_use_api/index.html#session-management
|
|
@@ -69,8 +76,6 @@ const useRequest = (setter, getter) => {
|
|
|
69
76
|
const cookie = response.headers.get('Set-Cookie');
|
|
70
77
|
if (cookie) {
|
|
71
78
|
setter(cookie);
|
|
72
|
-
if (currentCookies)
|
|
73
|
-
logout(makeCookie(currentCookies));
|
|
74
79
|
return { status: 'success' };
|
|
75
80
|
}
|
|
76
81
|
throw new IMSessionError('could not find sessionid from Set-Cookie in response header.');
|
|
@@ -84,20 +89,18 @@ const useRequest = (setter, getter) => {
|
|
|
84
89
|
}, [imFetch]);
|
|
85
90
|
return request;
|
|
86
91
|
};
|
|
87
|
-
const useDestroy = (renderTarget, setter, getter) => {
|
|
88
|
-
|
|
89
|
-
return useCallback(() => {
|
|
92
|
+
const useDestroy = (renderTarget, setter, getter, logout) => {
|
|
93
|
+
return useCallback(async () => {
|
|
90
94
|
const { cookies } = getter();
|
|
91
95
|
if (cookies) {
|
|
92
|
-
logout(makeCookie(cookies));
|
|
96
|
+
await logout(makeCookie(cookies));
|
|
93
97
|
}
|
|
94
98
|
setter(null);
|
|
95
99
|
renderTarget.dispatch();
|
|
96
100
|
}, []);
|
|
97
101
|
};
|
|
98
|
-
const useGetSessionAsync = (request, destroy, getter) => {
|
|
102
|
+
const useGetSessionAsync = (request, destroy, getter, logout) => {
|
|
99
103
|
const imFetch = useIMFetch({ noValidate: true });
|
|
100
|
-
const logout = useLogout();
|
|
101
104
|
const reRequest = async (count) => {
|
|
102
105
|
const { status } = await request();
|
|
103
106
|
if (status === 'success') {
|
|
@@ -114,8 +117,10 @@ const useGetSessionAsync = (request, destroy, getter) => {
|
|
|
114
117
|
const { cookies } = getter();
|
|
115
118
|
if (cookies) {
|
|
116
119
|
const response = await imFetch('api/smacolow/session', {
|
|
120
|
+
credentials: 'include',
|
|
117
121
|
headers: {
|
|
118
122
|
Cookie: makeCookie(cookies),
|
|
123
|
+
'X-Intramart-Session': 'keep', // https://document.intra-mart.jp/library/iap/public/im_web_api_maker/im_web_api_maker_programming_guide/texts/how_to_use_api/index.html#session-management
|
|
119
124
|
},
|
|
120
125
|
method: 'GET',
|
|
121
126
|
});
|
|
@@ -123,7 +128,7 @@ const useGetSessionAsync = (request, destroy, getter) => {
|
|
|
123
128
|
return getter();
|
|
124
129
|
}
|
|
125
130
|
else {
|
|
126
|
-
logout(makeCookie(cookies));
|
|
131
|
+
await logout(makeCookie(cookies));
|
|
127
132
|
}
|
|
128
133
|
}
|
|
129
134
|
const status = await reRequest(0);
|
|
@@ -133,8 +138,7 @@ const useGetSessionAsync = (request, destroy, getter) => {
|
|
|
133
138
|
throw new IMSessionError('failed to get session.');
|
|
134
139
|
}, [request]);
|
|
135
140
|
};
|
|
136
|
-
const unmountEffect = (getter) => {
|
|
137
|
-
const logout = useLogout();
|
|
141
|
+
const unmountEffect = (getter, logout) => {
|
|
138
142
|
const { cookies } = getter();
|
|
139
143
|
useEffect(() => {
|
|
140
144
|
return () => {
|
|
@@ -146,13 +150,14 @@ const unmountEffect = (getter) => {
|
|
|
146
150
|
};
|
|
147
151
|
export const IMSession = ({ children, sessionContext }) => {
|
|
148
152
|
const Context = sessionContext || DefaultContext;
|
|
149
|
-
const contextRef = useRef();
|
|
153
|
+
const contextRef = useRef(null);
|
|
150
154
|
const sessionStateRenderTarget = useRenderTarget();
|
|
151
155
|
const [, setSession, getSession] = useSessionStateRef(sessionStateRenderTarget);
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
+
const logout = useLogout();
|
|
157
|
+
const request = useRequest(setSession, getSession, logout);
|
|
158
|
+
const destroy = useDestroy(sessionStateRenderTarget, setSession, getSession, logout);
|
|
159
|
+
const getSessionAsync = useGetSessionAsync(request, destroy, getSession, logout);
|
|
160
|
+
unmountEffect(getSession, logout);
|
|
156
161
|
contextRef.current = {
|
|
157
162
|
getSession,
|
|
158
163
|
getSessionAsync,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import { IMMe } from '../Me';
|
|
3
2
|
import { IMOAuth } from '../OAuth';
|
|
4
3
|
import { IMSession } from '../Session';
|
|
5
4
|
import { IMSearch } from '../Search';
|
|
6
5
|
import { IMTenant } from '../Tenant';
|
|
6
|
+
import { JSX } from 'react';
|
|
7
7
|
interface SmartlimeProps {
|
|
8
8
|
children: JSX.Element;
|
|
9
9
|
baseUrl: string;
|
|
@@ -17,5 +17,5 @@ interface SmartlimeProps {
|
|
|
17
17
|
imsession?: Omit<React.ComponentProps<typeof IMSession>, 'children'>;
|
|
18
18
|
imseach?: Omit<React.ComponentProps<typeof IMSearch>, 'children'>;
|
|
19
19
|
}
|
|
20
|
-
export declare const Smartlime: ({ children, baseUrl, tenantId, imtenant, imoauth, imme, imsession, imseach, }: SmartlimeProps) =>
|
|
20
|
+
export declare const Smartlime: ({ children, baseUrl, tenantId, imtenant, imoauth, imme, imsession, imseach, }: SmartlimeProps) => JSX.Element;
|
|
21
21
|
export {};
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import { FetchModuleState, Moudles } from '.';
|
|
3
1
|
import { RenderTarget } from '../../_shared/renderTarget';
|
|
2
|
+
import { ModuleIds } from './modulesType';
|
|
3
|
+
export interface IMModule {
|
|
4
|
+
moduleId: ModuleIds[number];
|
|
5
|
+
version: string;
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
others: unknown;
|
|
8
|
+
}
|
|
9
|
+
export type IMModules = {
|
|
10
|
+
[key in ModuleIds[number]]: IMModule;
|
|
11
|
+
};
|
|
4
12
|
export interface TenantContext {
|
|
5
13
|
getBaseUrl: () => string;
|
|
6
14
|
getTenantId: () => string | undefined;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
fetchModuleInfo: () => Promise<void>;
|
|
15
|
+
getModules: () => IMModules | undefined;
|
|
16
|
+
setModules: (state: IMModules, silent?: boolean) => void;
|
|
17
|
+
destroyModule: () => Promise<void>;
|
|
11
18
|
modulesStateRenderTarget: RenderTarget;
|
|
12
19
|
}
|
|
13
20
|
export declare const Context: import("react").Context<TenantContext | null>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './useIMBaseUrl';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
2
|
+
export * from './useIMModules';
|
|
3
|
+
export * from './useIMModulesState';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from './useIMBaseUrl';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
2
|
+
export * from './useIMModules';
|
|
3
|
+
export * from './useIMModulesState';
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
+
import { useContext, useMemo } from 'react';
|
|
3
|
+
import { useIMFetch } from '../../Fetch';
|
|
4
|
+
import { createStorageKey } from '../constants';
|
|
5
|
+
import { Context as DefaultContext } from '../Context';
|
|
6
|
+
import { IMTenantError } from '../IMTenantError';
|
|
7
|
+
import { MODULE_IDS } from '../modulesType';
|
|
8
|
+
const saveModuleStorage = async (storageKey, Modules) => {
|
|
9
|
+
return await AsyncStorage.setItem(storageKey, JSON.stringify(Modules));
|
|
10
|
+
};
|
|
11
|
+
const getModuleStorage = async (storageKey) => {
|
|
12
|
+
const state = await AsyncStorage.getItem(storageKey);
|
|
13
|
+
if (state == null)
|
|
14
|
+
return;
|
|
15
|
+
const result = JSON.parse(state);
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
const createInitialModules = () => {
|
|
19
|
+
const initState = MODULE_IDS.reduce((acc, moduleId) => {
|
|
20
|
+
acc[moduleId] = {
|
|
21
|
+
moduleId: moduleId,
|
|
22
|
+
version: '',
|
|
23
|
+
enabled: false,
|
|
24
|
+
others: {},
|
|
25
|
+
};
|
|
26
|
+
return acc;
|
|
27
|
+
}, {});
|
|
28
|
+
return initState;
|
|
29
|
+
};
|
|
30
|
+
const useRequestModule = (baseUrl, setModules) => {
|
|
31
|
+
const IMFetch = useIMFetch();
|
|
32
|
+
return async () => {
|
|
33
|
+
const storageKey = createStorageKey(baseUrl);
|
|
34
|
+
const storageState = await getModuleStorage(storageKey);
|
|
35
|
+
if (storageState) {
|
|
36
|
+
setModules(storageState);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const response = await IMFetch('api/smacolow/modules');
|
|
40
|
+
if (response.status === 404) {
|
|
41
|
+
const state = createInitialModules();
|
|
42
|
+
saveModuleStorage(storageKey, state);
|
|
43
|
+
setModules(state);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (response.status !== 200) {
|
|
47
|
+
throw new IMTenantError('failed to fetch module status.');
|
|
48
|
+
}
|
|
49
|
+
const result = await response.json();
|
|
50
|
+
const state = result.data.modules.reduce((acc, module) => {
|
|
51
|
+
const { moduleId, moduleVersion } = module;
|
|
52
|
+
acc[moduleId] = {
|
|
53
|
+
moduleId: moduleId,
|
|
54
|
+
version: moduleVersion,
|
|
55
|
+
enabled: true,
|
|
56
|
+
others: {},
|
|
57
|
+
};
|
|
58
|
+
return acc;
|
|
59
|
+
}, createInitialModules());
|
|
60
|
+
saveModuleStorage(storageKey, state);
|
|
61
|
+
setModules(state);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export const useIMModules = (Context) => {
|
|
66
|
+
const tenantContext = useContext(Context || DefaultContext);
|
|
67
|
+
if (tenantContext == null) {
|
|
68
|
+
throw new IMTenantError('useIMModules requires either a Context provide or an ancestor element with a IMTenantProvider.');
|
|
69
|
+
}
|
|
70
|
+
const requestModule = useRequestModule(tenantContext.getBaseUrl(), tenantContext.setModules);
|
|
71
|
+
return useMemo(() => {
|
|
72
|
+
const { getModules, destroyModule } = tenantContext;
|
|
73
|
+
return {
|
|
74
|
+
request: requestModule,
|
|
75
|
+
getModules,
|
|
76
|
+
destroy: destroyModule,
|
|
77
|
+
};
|
|
78
|
+
}, [tenantContext]);
|
|
79
|
+
};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { useContext, useEffect, useReducer, useRef } from 'react';
|
|
2
2
|
import { Context as DefaultContext } from '../Context';
|
|
3
3
|
import { IMTenantError } from '../IMTenantError';
|
|
4
|
-
export const
|
|
4
|
+
export const useIMModulesState = (Context) => {
|
|
5
5
|
const initRef = useRef(true);
|
|
6
6
|
const [, forceRender] = useReducer((s) => s + 1, 0);
|
|
7
7
|
const tenantContext = useContext(Context || DefaultContext);
|
|
8
8
|
if (tenantContext == null) {
|
|
9
|
-
throw new IMTenantError('
|
|
9
|
+
throw new IMTenantError('useIMModulesState requires either a Context provide or an ancestor element with a IMTenantProvider.');
|
|
10
10
|
}
|
|
11
11
|
if (initRef.current) {
|
|
12
12
|
initRef.current = false;
|
|
@@ -17,6 +17,5 @@ export const useIMFetchModuleState = (Context) => {
|
|
|
17
17
|
tenantContext.modulesStateRenderTarget.removeEventListener(forceRender);
|
|
18
18
|
};
|
|
19
19
|
}, []);
|
|
20
|
-
|
|
21
|
-
return state;
|
|
20
|
+
return tenantContext.getModules();
|
|
22
21
|
};
|
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import { Context as DefaultContext } from './Context';
|
|
3
|
-
import { ModuleIds } from './modulesType';
|
|
1
|
+
import { JSX } from 'react';
|
|
2
|
+
import { Context as DefaultContext, IMModules } from './Context';
|
|
4
3
|
interface IMTenantProps {
|
|
5
4
|
children: JSX.Element;
|
|
6
5
|
baseUrl: string;
|
|
7
6
|
tenantId?: string;
|
|
8
7
|
tenantContext?: typeof DefaultContext;
|
|
9
|
-
useModuleInfo?: boolean;
|
|
10
8
|
}
|
|
11
|
-
export interface ModuleInfo<T extends ModuleIds[number]> {
|
|
12
|
-
moduleKey: T;
|
|
13
|
-
enabled: boolean;
|
|
14
|
-
othes: unknown;
|
|
15
|
-
}
|
|
16
|
-
export type Moudles = {
|
|
17
|
-
[key in ModuleIds[number]]: ModuleInfo<key>;
|
|
18
|
-
};
|
|
19
|
-
export type FetchModuleState = 'notRequired' | 'notFetched' | 'fetched';
|
|
20
9
|
export interface ModuleState {
|
|
21
|
-
|
|
22
|
-
modules?: Moudles;
|
|
10
|
+
modules?: IMModules;
|
|
23
11
|
}
|
|
24
|
-
export declare const IMTenant: ({ children, baseUrl, tenantId, tenantContext,
|
|
12
|
+
export declare const IMTenant: ({ children, baseUrl, tenantId, tenantContext, }: IMTenantProps) => JSX.Element;
|
|
25
13
|
export {};
|
|
@@ -1,115 +1,48 @@
|
|
|
1
1
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
2
|
import { useCallback, useRef } from 'react';
|
|
3
3
|
import { useRenderTarget } from '../../_shared/renderTarget';
|
|
4
|
-
import { join } from '../../utils/path';
|
|
5
4
|
import { Context as DefaultContext } from './Context';
|
|
6
|
-
import {
|
|
7
|
-
import { HYBRID_SSO_KEY, MODULE_IDS } from './modulesType';
|
|
5
|
+
import { createStorageKey } from './constants';
|
|
8
6
|
const useGetBaseUrl = (baseUrl) => {
|
|
9
7
|
return useCallback(() => baseUrl, [baseUrl]);
|
|
10
8
|
};
|
|
11
9
|
const useGetTenantId = (tenantId) => {
|
|
12
10
|
return useCallback(() => tenantId, [tenantId]);
|
|
13
11
|
};
|
|
14
|
-
const
|
|
15
|
-
return useModuleInfo ? 'notFetched' : 'notRequired';
|
|
16
|
-
};
|
|
17
|
-
const createStorageKey = (baseUrl, moduleKey) => {
|
|
18
|
-
return `${baseUrl}_${moduleKey}`;
|
|
19
|
-
};
|
|
20
|
-
const saveModuleInfoStorage = async (storageKey, ModuleInfo) => {
|
|
21
|
-
return await AsyncStorage.setItem(storageKey, JSON.stringify(ModuleInfo));
|
|
22
|
-
};
|
|
23
|
-
const deleteModuleInfoStorage = async (storageKey) => {
|
|
12
|
+
const deleteModuleStorage = async (storageKey) => {
|
|
24
13
|
return await AsyncStorage.removeItem(storageKey);
|
|
25
14
|
};
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
if (state == null)
|
|
29
|
-
return;
|
|
30
|
-
const result = JSON.parse(state);
|
|
31
|
-
return result;
|
|
32
|
-
};
|
|
33
|
-
const useFetchModuleStateRef = (renderTarget, useModuleInfo) => {
|
|
34
|
-
const ref = useRef(createInitFetchModuleState(useModuleInfo));
|
|
15
|
+
const useModuleRef = (baseUrl, renderTarget) => {
|
|
16
|
+
const ref = useRef(null);
|
|
35
17
|
const setter = useCallback((state, silent) => {
|
|
36
|
-
if (!useModuleInfo)
|
|
37
|
-
throw new IMTenantError('useModuleInfo is not enabled');
|
|
38
18
|
if (ref.current === state)
|
|
39
19
|
return;
|
|
40
20
|
ref.current = state;
|
|
41
21
|
if (!silent)
|
|
42
22
|
renderTarget.dispatch();
|
|
43
23
|
}, []);
|
|
44
|
-
const getter = useCallback(() => ref.current, []);
|
|
45
|
-
return [ref, setter, getter];
|
|
46
|
-
};
|
|
47
|
-
const useModuleInfoRef = (baseUrl, fetchModuleStateSetter, useModuleInfo) => {
|
|
48
|
-
const ref = useRef();
|
|
49
|
-
const setter = useCallback((state, silent) => {
|
|
50
|
-
if (!useModuleInfo)
|
|
51
|
-
throw new IMTenantError('useModuleInfo is not enabled');
|
|
52
|
-
if (ref.current === state)
|
|
53
|
-
return;
|
|
54
|
-
ref.current = state;
|
|
55
|
-
fetchModuleStateSetter('fetched', silent);
|
|
56
|
-
}, [fetchModuleStateSetter, useModuleInfo]);
|
|
57
|
-
const getter = useCallback(() => ref.current, []);
|
|
24
|
+
const getter = useCallback(() => (ref.current == null ? void 0 : ref.current), []);
|
|
58
25
|
const destroy = useCallback(async () => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
ref.current = undefined;
|
|
67
|
-
fetchModuleStateSetter('notFetched');
|
|
68
|
-
}, [fetchModuleStateSetter, baseUrl]);
|
|
26
|
+
const storageKey = createStorageKey(baseUrl);
|
|
27
|
+
await deleteModuleStorage(storageKey);
|
|
28
|
+
ref.current = null;
|
|
29
|
+
renderTarget.dispatch();
|
|
30
|
+
}, [baseUrl]);
|
|
69
31
|
return [ref, setter, getter, destroy];
|
|
70
32
|
};
|
|
71
|
-
const
|
|
72
|
-
return join(baseUrl, '/im_hybrid_sso/logout');
|
|
73
|
-
};
|
|
74
|
-
const useFetchModuleInfo = (baseUrl, setModuleInfo) => {
|
|
75
|
-
return useCallback(async () => {
|
|
76
|
-
const hybridSSOUrl = createCheckHybridSSOUrl(baseUrl);
|
|
77
|
-
const storageKey = createStorageKey(baseUrl, HYBRID_SSO_KEY);
|
|
78
|
-
const storageState = await getModuleInfoStorage(storageKey);
|
|
79
|
-
if (storageState) {
|
|
80
|
-
setModuleInfo({
|
|
81
|
-
[HYBRID_SSO_KEY]: storageState,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
const response = await fetch(hybridSSOUrl);
|
|
86
|
-
const hasHyBridSSO = response.status !== 404;
|
|
87
|
-
const state = {
|
|
88
|
-
moduleKey: HYBRID_SSO_KEY,
|
|
89
|
-
enabled: hasHyBridSSO,
|
|
90
|
-
othes: {},
|
|
91
|
-
};
|
|
92
|
-
saveModuleInfoStorage(storageKey, state);
|
|
93
|
-
setModuleInfo({ [HYBRID_SSO_KEY]: state });
|
|
94
|
-
}
|
|
95
|
-
}, [setModuleInfo, baseUrl]);
|
|
96
|
-
};
|
|
97
|
-
export const IMTenant = ({ children, baseUrl, tenantId, tenantContext, useModuleInfo, }) => {
|
|
33
|
+
export const IMTenant = ({ children, baseUrl, tenantId, tenantContext, }) => {
|
|
98
34
|
const Context = tenantContext || DefaultContext;
|
|
99
|
-
const contextRef = useRef();
|
|
35
|
+
const contextRef = useRef(null);
|
|
100
36
|
const modulesStateRenderTarget = useRenderTarget();
|
|
101
37
|
const getBaseUrl = useGetBaseUrl(baseUrl);
|
|
102
38
|
const getTenantId = useGetTenantId(tenantId);
|
|
103
|
-
const [,
|
|
104
|
-
const [, setModuleInfo, getModuleInfo, destroyModuleInfo] = useModuleInfoRef(baseUrl, setFetchModuleState, useModuleInfo);
|
|
105
|
-
const fetchModuleInfo = useFetchModuleInfo(baseUrl, setModuleInfo);
|
|
39
|
+
const [, setModules, getModules, destroyModule] = useModuleRef(baseUrl, modulesStateRenderTarget);
|
|
106
40
|
contextRef.current = {
|
|
107
41
|
getBaseUrl,
|
|
108
42
|
getTenantId,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
fetchModuleInfo,
|
|
43
|
+
getModules,
|
|
44
|
+
setModules,
|
|
45
|
+
destroyModule,
|
|
113
46
|
modulesStateRenderTarget,
|
|
114
47
|
};
|
|
115
48
|
return (<Context.Provider value={contextRef.current}>{children}</Context.Provider>);
|