Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ReactSDK): include error states #138

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/react-sdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type BucketContext = {
flags: {
flags: Flags;
isLoading: boolean;
isError: boolean;
error?: any;
};
};

Expand All @@ -29,6 +31,7 @@ const Context = createContext<BucketContext>({
flags: {
flags: {},
isLoading: true,
isError: false,
},
});

Expand All @@ -42,6 +45,7 @@ export type BucketProps = BucketSDKOptions & {
export default function Bucket({ children, sdk, ...config }: BucketProps) {
const [flags, setFlags] = useState<Flags>({});
const [flagsLoading, setFlagsLoading] = useState(true);
const [errorLoading, setErrorLoading] = useState(undefined);
const [bucket] = useState(() => sdk ?? BucketSingleton);

const contextKey = canonicalJSON(config);
Expand All @@ -61,7 +65,11 @@ export default function Bucket({ children, sdk, ...config }: BucketProps) {
setFlagsLoading(false);
})
.catch((err) => {
setErrorLoading(err);
console.error("[Bucket SDK] Error fetching flags:", err);
})
.finally(() => {
setFlagsLoading(false);
});
} catch (err) {
console.error("[Bucket SDK] Unknown error:", err);
Expand All @@ -70,7 +78,12 @@ export default function Bucket({ children, sdk, ...config }: BucketProps) {

const context: BucketContext = {
bucket,
flags: { flags, isLoading: flagsLoading },
flags: {
flags,
isLoading: flagsLoading,
isError: errorLoading == null,
error: errorLoading,
},
};

return <Context.Provider children={children} value={context} />;
Expand Down
Loading