Skip to content

Commit 51d98c2

Browse files
feat: scope support in usePreferences hook (#1405)
1 parent 513d0c2 commit 51d98c2

File tree

5 files changed

+78
-23
lines changed

5 files changed

+78
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ resource_config
2424
/dist
2525
__debug_*
2626
*.local.*
27+
node_modules/
28+
.next/

web/pnpm-lock.yaml

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/sdk/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@
6666
},
6767
"devDependencies": {
6868
"@jest/globals": "^29.7.0",
69+
"@radix-ui/react-form": "^0.0.2",
6970
"@radix-ui/react-icons": "^1.3.2",
7071
"@raystack/apsara": "^0.56.2",
7172
"@raystack/eslint-config": "workspace:^",
7273
"@raystack/frontier-tsconfig": "workspace:^",
7374
"@size-limit/preset-small-lib": "^8.2.6",
75+
"@stitches/react": "^1.2.8",
7476
"@types/jest": "^29.5.14",
7577
"@types/lodash": "^4.17.17",
7678
"@types/node": "^20.19.0",
@@ -85,16 +87,14 @@
8587
"np": "^7.7.0",
8688
"patch-package": "^8.0.1",
8789
"prettier": "^2.8.8",
90+
"react-loading-skeleton": "^3.4.0",
8891
"release-it": "^16.3.0",
8992
"semver": "^7.7.2",
9093
"size-limit": "^8.2.6",
94+
"sonner": "^1.4.41",
9195
"ts-jest": "^29.3.4",
9296
"tsup": "^6.7.0",
9397
"typescript": "^5.8.3",
94-
"@radix-ui/react-form": "^0.0.2",
95-
"@stitches/react": "^1.2.8",
96-
"react-loading-skeleton": "^3.4.0",
97-
"sonner": "^1.4.41",
9898
"usehooks-ts": "^3.1.1",
9999
"zod": "^3.22.3"
100100
},
@@ -104,7 +104,7 @@
104104
"@connectrpc/connect-query": "2.1.1",
105105
"@connectrpc/connect-web": "^2.0.2",
106106
"@hookform/resolvers": "^3.10.0",
107-
"@raystack/proton": "0.1.0-b1687af73f994fa9612a023c850aa97c35735af8",
107+
"@raystack/proton": "0.1.0-330c7558f34570056814d418f99730fb45cfe80f",
108108
"@tanstack/react-query": "^5.83.0",
109109
"@tanstack/react-router": "1.58.17",
110110
"axios": "^1.9.0",
@@ -144,4 +144,4 @@
144144
"publishConfig": {
145145
"access": "public"
146146
}
147-
}
147+
}

web/sdk/react/hooks/usePreferences.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { useQuery, useMutation, createConnectQueryKey, useTransport } from '@connectrpc/connect-query';
2-
import { useQueryClient } from '@tanstack/react-query';
2+
import { UseMutationResult, useQueryClient, UseQueryResult } from '@tanstack/react-query';
33
import { create } from '@bufbuild/protobuf';
4-
import { FrontierServiceQueries, CreateCurrentUserPreferencesRequestSchema } from '@raystack/proton/frontier';
4+
import { FrontierServiceQueries, CreateCurrentUserPreferencesRequestSchema, ListCurrentUserPreferencesRequestSchema } from '@raystack/proton/frontier';
55
import { useCallback, useMemo } from 'react';
66

77
type Preference = {
88
name?: string;
99
value?: string;
10+
scopeType?: string;
11+
scopeId?: string;
1012
[key: string]: any;
1113
};
1214

@@ -21,6 +23,8 @@ export interface UsePreferences {
2123
updatePreferences: (
2224
preferences: Preference[]
2325
) => Promise<void>;
26+
fetchPreferencesStatus: UseQueryResult['status'];
27+
updatePreferencesStatus: UseMutationResult['status'];
2428
}
2529

2630
function getFormattedData(preferences: Preference[] = []): Preferences {
@@ -31,20 +35,28 @@ function getFormattedData(preferences: Preference[] = []): Preferences {
3135
}
3236

3337
export function usePreferences({
34-
autoFetch = true
38+
autoFetch = true,
39+
scopeType,
40+
scopeId
3541
}: {
3642
autoFetch?: boolean;
43+
scopeType?: string;
44+
scopeId?: string;
3745
} = {}): UsePreferences {
3846
const queryClient = useQueryClient();
3947
const transport = useTransport();
4048

4149
const {
4250
data: preferencesData,
4351
isLoading: isFetchingPreferences,
44-
refetch
52+
refetch,
53+
status: fetchPreferencesStatus
4554
} = useQuery(
4655
FrontierServiceQueries.listCurrentUserPreferences,
47-
{},
56+
create(ListCurrentUserPreferencesRequestSchema, {
57+
scopeType,
58+
scopeId
59+
}),
4860
{
4961
enabled: autoFetch
5062
}
@@ -54,14 +66,14 @@ export function usePreferences({
5466

5567
const {
5668
mutateAsync: updatePreferencesMutation,
57-
isPending: isUpdatingPreferences
69+
isPending: isUpdatingPreferences,
70+
status: updatePreferencesStatus
5871
} = useMutation(FrontierServiceQueries.createCurrentUserPreferences, {
5972
onSuccess: () => {
6073
queryClient.invalidateQueries({
6174
queryKey: createConnectQueryKey({
6275
schema: FrontierServiceQueries.listCurrentUserPreferences,
6376
transport,
64-
input: {},
6577
cardinality: 'finite'
6678
})
6779
});
@@ -101,6 +113,8 @@ export function usePreferences({
101113
isLoading: isUpdatingPreferences,
102114
isFetching: isFetchingPreferences,
103115
fetchPreferences: refetch,
104-
updatePreferences
116+
updatePreferences,
117+
updatePreferencesStatus,
118+
fetchPreferencesStatus
105119
};
106120
}

web/sdk/tsconfig.json

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
{
22
"extends": "@raystack/frontier-tsconfig/react-library.json",
33
"compilerOptions": {
4+
"jsx": "react-jsx",
45
"moduleResolution": "bundler",
56
"target": "ES2015",
6-
"lib": ["ES2020", "DOM"],
7+
"lib": [
8+
"ES2020",
9+
"DOM"
10+
],
711
"paths": {
8-
"~/*": ["./*"],
9-
"~hooks": ["./hooks/index.ts"]
12+
"~/*": [
13+
"./*"
14+
],
15+
"~hooks": [
16+
"./hooks/index.ts"
17+
]
1018
},
11-
"types": ["jest"]
19+
"types": [
20+
"jest"
21+
]
1222
},
13-
"include": ["."],
14-
"exclude": ["dist", "react/dist", "hooks/dist", "node_modules"]
15-
}
23+
"include": [
24+
"."
25+
],
26+
"exclude": [
27+
"dist",
28+
"react/dist",
29+
"hooks/dist",
30+
"node_modules"
31+
]
32+
}

0 commit comments

Comments
 (0)