1
1
import type { ComputedRef } from 'vue' ;
2
2
import { computed , reactive } from 'vue' ;
3
3
4
- import type { QueryKeyBase } from '@/query/_types/query-key-type' ;
4
+ import type { QueryContext } from '@/query/_types/query-key-type' ;
5
5
6
6
import { useAppContextStore } from '@/store/app-context/app-context-store' ;
7
7
import { useUserWorkspaceStore } from '@/store/app-context/workspace/user-workspace-store' ;
@@ -11,13 +11,10 @@ import { useUserWorkspaceStore } from '@/store/app-context/workspace/user-worksp
11
11
* Interface for global query parameters used across API requests.
12
12
* These parameters are automatically included in API and reference query keys.
13
13
*/
14
- export interface QueryKeyBaseParams {
15
- /** Flag indicating whether admin mode is enabled */
14
+ export interface QueryKeyContextParams {
16
15
isAdminMode ?: boolean ;
17
- /** Current workspace ID from the workspace context */
18
16
workspaceId ?: string ;
19
- /** Additional parameters that might be added */
20
- [ key : string ] : any ;
17
+ context : 'service' | 'reference' ;
21
18
}
22
19
23
20
/**
@@ -26,24 +23,28 @@ export interface QueryKeyBaseParams {
26
23
* that are commonly used across different API queries.
27
24
*
28
25
* @param queryKeyOptions - Optional query key options to merge with default params
29
- * @returns A computed reference containing the combined global query parameters as an ordered array
26
+ * @returns A computed reference containing the combined global query parameters as a QueryContext object
30
27
*
31
28
* ### Example Usage:
32
29
* ```ts
33
- * const queryKeyBase = useQueryKeyBase( );
34
- * // Access params (now returns array)
35
- * const [ mode, workspaceId, others] = queryKeyBase .value;
30
+ * const queryContext = useQueryKeyContext({ context: 'service' } );
31
+ * // Access params
32
+ * const { mode, workspaceId, context } = queryContext .value;
36
33
*
37
34
* // With additional params
38
- * const params = useQueryKeyBase({ customParam: 'value' });
35
+ * const params = useQueryKeyContext({
36
+ * context: 'service',
37
+ * isAdminMode: true,
38
+ * workspaceId: 'custom-id'
39
+ * });
39
40
* ```
40
41
*
41
42
* ### Features:
42
- * - **Ordered Parameters **: Guarantees consistent parameter order: [ mode, workspaceId, others]
43
+ * - **Structured Context **: Returns a QueryContext object with mode, workspaceId, and context
43
44
* - **Reactive State**: Automatically updates when workspace or admin mode changes
44
- * - **Parameter Merging **: Allows adding custom parameters in the others object
45
+ * - **Type Safety **: Ensures context is always provided with valid values
45
46
*/
46
- export const useQueryKeyBase = ( queryKeyOptions ?: Partial < QueryKeyBaseParams > ) : ComputedRef < QueryKeyBase > => {
47
+ export const useQueryKeyContext = ( queryKeyOptions : QueryKeyContextParams ) : ComputedRef < QueryContext > => {
47
48
const appContextStore = useAppContextStore ( ) ;
48
49
const userWorkspaceStore = useUserWorkspaceStore ( ) ;
49
50
@@ -52,15 +53,17 @@ export const useQueryKeyBase = (queryKeyOptions?: Partial<QueryKeyBaseParams>):
52
53
currentWorkspaceId : computed < string | undefined > ( ( ) => userWorkspaceStore . getters . currentWorkspaceId ) ,
53
54
} ) ;
54
55
55
- return computed < QueryKeyBase > ( ( ) => {
56
- const { isAdminMode, workspaceId : _workspaceId , ...otherParams } = queryKeyOptions || { } ;
57
- const mode = ( isAdminMode || _state . isAdminMode ) ? 'ADMIN' : 'WORKSPACE' ;
56
+ return computed < QueryContext > ( ( ) => {
57
+ const {
58
+ isAdminMode, workspaceId : _workspaceId , context,
59
+ } = queryKeyOptions || { } ;
60
+ const mode = ( isAdminMode || _state . isAdminMode ) ? 'admin' : 'workspace' ;
58
61
const workspaceId = _workspaceId || _state . currentWorkspaceId ;
59
62
60
- return [
63
+ return {
61
64
mode,
62
65
workspaceId,
63
- Object . keys ( otherParams ) . length > 0 ? otherParams : undefined ,
64
- ] ;
66
+ context ,
67
+ } ;
65
68
} ) ;
66
69
} ;
0 commit comments