feat: scope support in usePreferences hook#1405
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis pull request adds build configuration updates, expands UI and form library dependencies, extends the TypeScript compiler options with JSX configuration, and enhances the usePreferences hook to accept scope parameters and expose query/mutation status properties. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Pull Request Test Coverage Report for Build 22339673600Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
web/sdk/react/hooks/usePreferences.ts (3)
37-45: Consider validating thatscopeTypeandscopeIdare provided together.If a caller passes
scopeTypewithoutscopeId(or vice versa), the request will include an incomplete scope context. Depending on the API contract, this could silently return unscoped results or fail unexpectedly. A runtime guard or a type-level constraint would make this more robust.Option A: Runtime guard
export function usePreferences({ autoFetch = true, scopeType, scopeId }: { autoFetch?: boolean; scopeType?: string; scopeId?: string; } = {}): UsePreferences { + if ((scopeType && !scopeId) || (!scopeType && scopeId)) { + console.warn('frontier:sdk:: usePreferences: scopeType and scopeId should be provided together'); + }Option B: Type-level constraint (discriminated union)
type UsePreferencesOptions = | { autoFetch?: boolean; scopeType?: undefined; scopeId?: undefined } | { autoFetch?: boolean; scopeType: string; scopeId: string };
89-101: Duplicate error logging —onErrorinuseMutationandcatchinupdatePreferencesboth log.When
updatePreferencesMutationrejects, theonErrorcallback on line 81–86 logs the error, and then thecatchblock on lines 96–100 logs the same error again before rethrowing. This results in double console output for every mutation failure.♻️ Remove one of the duplicate error handlers
Either remove the
onErrorcallback fromuseMutation(sinceupdatePreferencesalready handles errors), or remove the try/catch logging and letonErrorbe the single logging site:const updatePreferences = useCallback(async (preferences: Preference[]) => { - try { - const req = create(CreateCurrentUserPreferencesRequestSchema, { - bodies: preferences - }); - await updatePreferencesMutation(req); - } catch (err) { - console.error( - 'frontier:sdk:: There is problem with updating user preferences' - ); - console.error(err); - throw err; - } + const req = create(CreateCurrentUserPreferencesRequestSchema, { + bodies: preferences + }); + await updatePreferencesMutation(req); }, [updatePreferencesMutation]);The
onErroron the mutation already handles logging. If you need the caller to catch,mutateAsyncrejects naturally.
104-118: The compositestatusfield doesn't account for error states.The
statuscomputation maps to'loading' | 'fetching' | 'idle', but both the query and mutation can be in an error state. Consumers relying onstatusalone won't know about failures — they'd need to checkfetchPreferencesStatusorupdatePreferencesStatusseparately. This is pre-existing behavior, but now that you're exposing the granular statuses, consider documenting or aligning the compositestatustype.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
web/pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (4)
.gitignoreweb/sdk/package.jsonweb/sdk/react/hooks/usePreferences.tsweb/sdk/tsconfig.json
Summary
Updated the
usePreferenceshook to align with the new preferences API that supports scoped preferences (e.g., per-organization).Changes
scopeTypeandscopeIdparameters to the hook for scoped preference queriesPreferencetype to includescopeTypeandscopeIdfieldsfetchPreferencesStatusandupdatePreferencesStatusfrom the hook returnlistCurrentUserPreferencesrequestTechnical Details
listCurrentUserPreferencesAPI now acceptsscopeTypeandscopeIdto filter preferences by context (e.g.,"app/organization"+ org ID)