Skip to content

Commit

Permalink
[Statsig] Typecheck gates (#3467)
Browse files Browse the repository at this point in the history
* Typecheck gates

* Lint against untyped useGate()

* Alphabetic
  • Loading branch information
gaearon authored Apr 10, 2024
1 parent bf974b1 commit 427f3a8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
},
},
],
'bsky-internal/use-typed-gates': 'error',
'simple-import-sort/imports': [
'warn',
{
Expand Down
1 change: 1 addition & 0 deletions eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
module.exports = {
rules: {
'avoid-unwrapped-text': require('./avoid-unwrapped-text'),
'use-typed-gates': require('./use-typed-gates'),
},
}
31 changes: 31 additions & 0 deletions eslint/use-typed-gates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

exports.create = function create(context) {
return {
ImportSpecifier(node) {
if (
!node.local ||
node.local.type !== 'Identifier' ||
node.local.name !== 'useGate'
) {
return
}
if (
node.parent.type !== 'ImportDeclaration' ||
!node.parent.source ||
node.parent.source.type !== 'Literal'
) {
return
}
const source = node.parent.source.value
if (source.startsWith('.') || source.startsWith('#')) {
return
}
context.report({
node,
message:
"Use useGate() from '#/lib/statsig/statsig' instead of the one on npm.",
})
},
}
}
11 changes: 8 additions & 3 deletions src/lib/statsig/gates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {useGate} from './statsig'

export const useNewSearchGate = () => useGate('new_search')
export type Gate =
// Keep this alphabetic please.
| 'autoexpand_suggestions_on_profile_follow'
| 'disable_min_shell_on_foregrounding'
| 'disable_poll_on_discover'
| 'new_search'
| 'show_follow_back_label'
| 'start_session_with_following'
3 changes: 2 additions & 1 deletion src/lib/statsig/statsig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import {logger} from '#/logger'
import {useSession} from '../../state/session'
import {LogEvents} from './events'
import {Gate} from './gates'

export type {LogEvents}

Expand Down Expand Up @@ -69,7 +70,7 @@ export function logEvent<E extends keyof LogEvents>(
}
}

export function useGate(gateName: string) {
export function useGate(gateName: Gate): boolean {
const {isLoading, value} = useStatsigGate(gateName)
if (isLoading) {
// This should not happen because of waitForInitialization={true}.
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {HITSLOP_10} from '#/lib/constants'
import {usePalette} from '#/lib/hooks/usePalette'
import {MagnifyingGlassIcon} from '#/lib/icons'
import {NavigationProp} from '#/lib/routes/types'
import {useNewSearchGate} from '#/lib/statsig/gates'
import {useGate} from '#/lib/statsig/statsig'
import {augmentSearchQuery} from '#/lib/strings/helpers'
import {s} from '#/lib/styles'
import {logger} from '#/logger'
Expand Down Expand Up @@ -337,7 +337,7 @@ export function SearchScreenInner({
const {isDesktop} = useWebMediaQueries()
const {_} = useLingui()

const isNewSearch = useNewSearchGate()
const isNewSearch = useGate('new_search')

const onPageSelected = React.useCallback(
(index: number) => {
Expand Down

0 comments on commit 427f3a8

Please sign in to comment.