Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(flags): look up the flag key without the flag editor logic (#119100)

Paul D'Ambra455a50950d5e
diff --git a/frontend/src/lib/components/FlagSelector.tsx b/frontend/src/lib/components/FlagSelector.tsx
index 107ed9f24676..bee2b13fa0c7 100644
--- a/frontend/src/lib/components/FlagSelector.tsx
+++ b/frontend/src/lib/components/FlagSelector.tsx
@@ -6,10 +6,11 @@ import { TaxonomicFilterGroupType, TaxonomicFilterLogicProps } from 'lib/compone
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { Popover } from 'lib/lemon-ui/Popover'
import { productHasEffectiveNoneAccess } from 'lib/utils/accessControlUtils'
-import { featureFlagLogic } from 'scenes/feature-flags/featureFlagLogic'
import { AccessControlResourceType, FeatureFlagBasicType } from '~/types'
+import { flagSelectorLogic } from './flagSelectorLogic'
+
interface FlagSelectorProps {
value: number | undefined
onChange: (id: number, key: string, flag: FeatureFlagBasicType) => void
@@ -56,7 +57,7 @@ export function FlagSelector({
// whatever the picker handed us to cover that gap.
const [selectedFlag, setSelectedFlag] = useState<PickedFlag | undefined>(undefined)
- const { featureFlag } = useValues(featureFlagLogic({ id: value || 'link' }))
+ const { flagKey } = useValues(flagSelectorLogic({ id: value }))
const taxonomicFilterLogicProps: TaxonomicFilterLogicProps = {
groupType: TaxonomicFilterGroupType.FeatureFlags,
@@ -83,7 +84,7 @@ export function FlagSelector({
: undefined
const buttonLabel = flagSelectorButtonLabel({
- flagKey: featureFlag.key,
+ flagKey,
value,
pickedFlag: selectedFlag,
initialButtonLabel,
diff --git a/frontend/src/lib/components/flagSelectorLogic.test.ts b/frontend/src/lib/components/flagSelectorLogic.test.ts
new file mode 100644
index 000000000000..dcec56ec2b0b
--- /dev/null
+++ b/frontend/src/lib/components/flagSelectorLogic.test.ts
@@ -0,0 +1,26 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { flagSelectorLogic } from './flagSelectorLogic'
+
+describe('flagSelectorLogic', () => {
+ test.each([
+ ['labels the button with the stored flag key', 200, 'checkout-redesign'],
+ ['leaves the label to the fallbacks when the lookup fails', 403, ''],
+ ])('%s', async (_name: string, status: number, expectedKey: string) => {
+ useMocks({
+ get: {
+ '/api/projects/:team_id/feature_flags/:id/': () => [status, { id: 7, key: 'checkout-redesign' }],
+ },
+ })
+ initKeaTests()
+ const logic = flagSelectorLogic({ id: 7 })
+ logic.mount()
+
+ await expectLogic(logic).toFinishAllListeners().toMatchValues({ flagKey: expectedKey })
+
+ logic.unmount()
+ })
+})
diff --git a/frontend/src/lib/components/flagSelectorLogic.ts b/frontend/src/lib/components/flagSelectorLogic.ts
new file mode 100644
index 000000000000..6228b3cd6b0e
--- /dev/null
+++ b/frontend/src/lib/components/flagSelectorLogic.ts
@@ -0,0 +1,68 @@
+import { LogicWrapper, MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import { projectLogic } from 'scenes/projectLogic'
+
+import { featureFlagsRetrieve } from 'products/feature_flags/frontend/generated/api'
+
+export interface FlagSelectorLogicProps {
+ id: number | undefined
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface flagSelectorLogicValues {
+ currentProjectId: number | null // projectLogic
+ flagKey: string
+ flagKeyLoading: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface flagSelectorLogicActions {
+ loadFlagKey: () => any
+ loadFlagKeyFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadFlagKeySuccess: (
+ flagKey: string,
+ payload?: any
+ ) => {
+ flagKey: string
+ payload?: any
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface flagSelectorLogicMeta {
+ key: number | 'none'
+}
+
+export type flagSelectorLogicType = MakeLogicType<
+ flagSelectorLogicValues,
+ flagSelectorLogicActions,
+ FlagSelectorLogicProps,
+ flagSelectorLogicMeta
+>
+
+/** The key of the flag a FlagSelector holds, without loading the whole feature flag editor logic. */
+export const flagSelectorLogic: LogicWrapper<flagSelectorLogicType> = kea<flagSelectorLogicType>([
+ props({} as FlagSelectorLogicProps),
+ key((props) => props.id ?? 'none'),
+ path((key) => ['lib', 'components', 'flagSelectorLogic', key]),
+ connect(() => ({ values: [projectLogic, ['currentProjectId']] })),
+ loaders(({ props, values }) => ({
+ flagKey: [
+ '',
+ {
+ loadFlagKey: async () =>
+ props.id ? (await featureFlagsRetrieve(String(values.currentProjectId), props.id)).key : '',
+ },
+ ],
+ })),
+ afterMount(({ actions }) => {
+ actions.loadFlagKey()
+ }),
+])