Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions .changeset/fix-feed-settings-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@knocklabs/react-core": patch
"@knocklabs/react": patch
"@knocklabs/react-native": patch
---

Handle missing `features` in feed settings responses to prevent crashes on partial API responses.
10 changes: 8 additions & 2 deletions packages/react-core/src/modules/feed/hooks/useFeedSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function useFeedSettings(feedClient: Feed): {
settings: FeedSettings | null;
loading: boolean;
} {
const [settings, setSettings] = useState(null);
const [settings, setSettings] = useState<FeedSettings | null>(null);
const [isLoading, setIsLoading] = useState(false);

// TODO: consider moving this into the feed client and into the feed store state when
Expand All @@ -29,7 +29,13 @@ function useFeedSettings(feedClient: Feed): {
});

if (!response.error) {
setSettings(response.body);
setSettings({
features: {
branding_required: Boolean(
response.body?.features?.branding_required,
),
},
});
}

setIsLoading(false);
Expand Down
20 changes: 20 additions & 0 deletions packages/react-core/test/feed/useFeedSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ describe("useFeedSettings", () => {

expect(result.current.settings).toBeNull();
});

it("handles partial settings response by defaulting features", async () => {
const { feed, mockApiClient } = createMockFeed("feed_123");

mockNetworkSuccess(mockApiClient, {});

const { result } = renderHook(() =>
useFeedSettings(feed as unknown as Feed),
);

await waitFor(() => {
expect(result.current.loading).toBe(false);
});

expect(result.current.settings).toEqual({
features: {
branding_required: false,
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const NotificationFeed: React.FC<NotificationFeedProps> = ({
onEndReached={onEndReached}
onEndReachedThreshold={0.5}
/>
{settings?.features.branding_required && (
{settings?.features?.branding_required && (
<View style={styles.branding}>
<PoweredByKnockIcon />
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const NotificationFeed: React.FC<NotificationFeedProps> = ({
{!requestInFlight && noItems && EmptyComponent}
</div>

{settings?.features.branding_required && (
{settings?.features?.branding_required && (
<div className="rnf-notification-feed__knock-branding">
<a href={poweredByKnockUrl} target="_blank">
{t("poweredBy") || "Powered by Knock"}
Expand Down