Skip to content
Merged
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.
17 changes: 14 additions & 3 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 @@ -28,8 +28,19 @@ function useFeedSettings(feedClient: Feed): {
url: feedSettingsPath,
});

if (!response.error) {
setSettings(response.body);
// Only trust a genuine success whose body actually contains the settings
// payload. On flaky connections a captive portal or proxy can return a
// 200 with a body that isn't the feed settings object; treat that as
// "unknown" (leave settings null) rather than fabricating a default that
// would silently suppress branding when it is required.
if (response.statusCode === "ok" && response.body?.features) {
setSettings({
features: {
branding_required: Boolean(
response.body.features.branding_required,
),
},
});
}

setIsLoading(false);
Expand Down
19 changes: 19 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,23 @@ describe("useFeedSettings", () => {

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

it("leaves settings null when a success response is missing the features payload", async () => {
const { feed, mockApiClient } = createMockFeed("feed_123");

// A degraded connection (captive portal / proxy) can return a 200 whose
// body is not the feed settings object. We must not fabricate a default
// from it, which would silently suppress branding when it is required.
mockNetworkSuccess(mockApiClient, {});

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

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

expect(result.current.settings).toBeNull();
});
});
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
Loading