Skip to content

Commit 0f03504

Browse files
committed
dev feature flags with query + cookies
1 parent 37cf000 commit 0f03504

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

explorer/components/AppFlags.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { useRouter } from 'next/router'
3+
import { useCookies } from 'react-cookie'
4+
5+
const flags = ['myTags']
6+
7+
export const AppFlags: React.FC = () => {
8+
const { query } = useRouter()
9+
const [cookies, setCookie] = useCookies()
10+
11+
React.useEffect(() => {
12+
flags.forEach(flag => {
13+
const key = `feature__${flag}`
14+
if (query?.[key] === 'on' && cookies[key] !== 'on') setCookie(key, 'on', { path: '/' })
15+
if (query?.[key] === 'off' && cookies[key] !== 'off') setCookie(key, 'off', { path: '/' })
16+
})
17+
}, [query])
18+
19+
return null
20+
}

explorer/components/DrawingPage/ActionsMenu.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import styled from 'styled-components'
33
import { navBarItemStyles } from './styles'
4+
import { useCookies } from 'react-cookie'
45

56
type Action = {
67
title: string
@@ -9,31 +10,38 @@ type Action = {
910

1011
export const ActionsMenu: React.FC = () => {
1112
const [isActive, setIsActive] = React.useState(false)
13+
const [cookies] = useCookies(['feature__myTags'])
14+
const [actions, setActions] = React.useState<Action[]>([])
1215

13-
const actions: Action[] = []
16+
const addNewTag = { title: 'Add new tag', onClick: () => alert('add new tag') }
1417

15-
if (!actions.length) return null
18+
React.useEffect(() => {
19+
const newActions = []
20+
if (cookies.feature__myTags === 'on') newActions.push(addNewTag)
21+
setActions(newActions)
22+
}, [cookies])
1623

1724
return (
18-
<Container title='More Actions' onClick={() => setIsActive(!isActive)}>
25+
<Container title='Actions' onClick={() => setIsActive(!isActive)} isHidden={!actions.length}>
1926
<svg viewBox="0 0 24 24">
2027
<circle cx="5" cy="12" r="2" />
2128
<circle cx="12" cy="12" r="2" />
2229
<circle cx="19" cy="12" r="2" />
2330
</svg>
2431
<Popup isActive={isActive}>
2532
{actions.map(action => (
26-
<Item><a onClick={action.onClick}>{action.title}</a></Item>
33+
<Item key={action.title}><a onClick={action.onClick}>{action.title}</a></Item>
2734
))}
2835
</Popup>
2936
</Container>
3037
)
3138
}
3239

33-
const Container = styled.button.attrs({ className: 'Explorer__ActionsMenu__Container'})`
40+
const Container = styled.button.attrs({ className: 'Explorer__ActionsMenu__Container'})<{ isHidden: boolean }>`
3441
${navBarItemStyles}
3542
background: none;
3643
border: 0;
44+
display: ${o => o.isHidden && 'none'};
3745
3846
svg {
3947
display: block;

explorer/pages/_app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { AppFlags } from '../components/AppFlags'
12
import { AppStyle } from '../components/AppStyle'
3+
import { CookiesProvider } from 'react-cookie'
24

35
export default function App({ Component, pageProps }) {
46
return (
5-
<>
7+
<CookiesProvider>
8+
<AppFlags />
69
<AppStyle />
710
<Component {...pageProps} />
8-
</>
11+
</CookiesProvider>
912
)
1013
}

0 commit comments

Comments
 (0)