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(toolbar): support feature flag payload overrides in the flag toolbar #28058

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

dmarticus
Copy link
Contributor

@dmarticus dmarticus commented Jan 29, 2025

Context

NB: This change depends on PostHog/posthog-js#1697 for types and new functionality. Don't merge this until the posthog-js change goes in.

With that above posthog-js PR, I've added support for overriding feature flag payloads (along with the usual support for overriding feature flag values) via a new method, overrideFeatureFlags. My changes here adds this override feature flag payload support to our toolbar.

Changes

Loom: https://www.loom.com/share/6b59c95c98d74e61b50ac843134bedc8

Screenshot 2025-01-28 at 16 03 22

Does this work well for both Cloud and self-hosted?

Yes

How did you test this code?

Manual UI testing.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR adds support for feature flag payload overrides in the toolbar, allowing users to customize feature flag behavior with JSON payloads directly from the UI.

Key changes:

  • Added payload editor section in FlagsToolbarMenu.tsx with JSON validation and error handling
  • Implemented new overrideFeatureFlags method in flagsToolbarLogic.ts to handle both flag values and payloads
  • Updated state management to track payload overrides and draft payloads separately
  • Modified code snippets to use new overrideFeatureFlags method instead of deprecated override
  • Added visual feedback for payload validation errors in the toolbar UI

3 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings | Greptile

? JSON.stringify(payloadOverride, null, 2)
: '')
}
onChange={(val) => setDraftPayload(feature_flag.key, val)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider debouncing setDraftPayload to avoid excessive state updates during typing

}
toolbarPosthogJS.capture('toolbar feature flag override removed')
actions.checkLocalOverrides()
clientPostHog.featureFlags.reloadFeatureFlags()
}
},
savePayloadOverride: ({ flagKey }) => {
try {
const payload = JSON.parse(values.draftPayloads[flagKey] || '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Empty string JSON.parse() will throw - should check for truthy value before parsing

Suggested change
const payload = JSON.parse(values.draftPayloads[flagKey] || '')
const draftPayload = values.draftPayloads[flagKey]
const payload = draftPayload ? JSON.parse(draftPayload) : null

Comment on lines 173 to 178
const payloads = payloadOverride ? { [flagKey]: payloadOverride } : undefined
clientPostHog.featureFlags.overrideFeatureFlags(
{ ...values.localOverrides, [flagKey]: overrideValue },
{
payloads: payloads,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: No validation of payloadOverride structure before passing to overrideFeatureFlags - could cause runtime errors if malformed

Comment on lines 191 to 197
const updatedFlags = { ...values.localOverrides }
delete updatedFlags[flagKey]
if (Object.keys(updatedFlags).length > 0) {
clientPostHog.featureFlags.override({ ...updatedFlags })
clientPostHog.featureFlags.overrideFeatureFlags({ ...updatedFlags })
} else {
clientPostHog.featureFlags.override(false)
clientPostHog.featureFlags.overrideFeatureFlags(false)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: State inconsistency possible - payloadOverrides state not cleared when feature flags are disabled

Comment on lines 27 to 31
setOverriddenUserFlag: (flagKey: string, overrideValue: string | boolean, payloadOverride?: any) => ({
flagKey,
overrideValue,
payloadOverride,
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: payloadOverride type should be more strictly defined than 'any' for type safety

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

5 snapshot changes in total. 0 added, 5 modified, 0 deleted:

  • chromium: 0 added, 5 modified, 0 deleted (diff for shard 2)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

1 snapshot changes in total. 0 added, 1 modified, 0 deleted:

  • chromium: 0 added, 1 modified, 0 deleted (diff for shard 1)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

1 snapshot changes in total. 0 added, 1 modified, 0 deleted:

  • chromium: 0 added, 1 modified, 0 deleted (diff for shard 1)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants