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

PostHog Early Access Features #1585

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
114 changes: 114 additions & 0 deletions src/scenes/EarlyAccess/EarlyAccess.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogProps,
DialogTitle,
List,
ListItem,
ListItemText,
Switch,
Typography,
} from '@mui/material';
import Markdown from 'markdown-to-jsx';
import { EarlyAccessFeature } from 'posthog-js';
import { useActiveFeatureFlags, usePostHog } from 'posthog-js/react';
import { Fragment, memo, useEffect, useState } from 'react';

export const EarlyAccessDialog = ({ children, ...props }: DialogProps) => (
<Dialog {...props} aria-labelledby="ea-dialog-title" maxWidth="md">
<DialogTitle id="ea-dialog-title">Early Access Features</DialogTitle>
<DialogContent dividers sx={{ p: 0 }}>
<EarlyAccessFeatures />
</DialogContent>
<DialogActions>
<Button
onClick={() => props.onClose?.({}, 'backdropClick')}
variant="text"
color="secondary"
>
Close
</Button>
</DialogActions>
</Dialog>
);

const EarlyAccessFeatures = () => {
const postHog = usePostHog();

const active = new Set(useActiveFeatureFlags());

const toggle = (flag: string, next: boolean) => {
postHog.updateEarlyAccessFeatureEnrollment(flag, next);
};

const [features, setFeatures] = useState<EarlyAccessFeature[]>([]);
useEffect(() => {
postHog.getEarlyAccessFeatures(setFeatures, true);
}, [postHog, setFeatures]);

return (
<List>
{features.map((feature) => {
const flag = feature.flagKey!;
const enabled = active.has(flag);
return (
<ListItem
key={flag}
divider
sx={{
px: 3,
gap: 1,
'&:last-of-type': { borderBottom: 'none' },
}}
>
<ListItemText
primary={feature.name}
secondary={<Description>{feature.description}</Description>}
secondaryTypographyProps={{ component: 'div' }}
sx={{ whiteSpace: 'pre' }}
id={`switch-list-label-${flag}`}
/>
<Switch
edge="end"
onChange={() => toggle(flag, !enabled)}
checked={enabled}
inputProps={{
'aria-labelledby': `switch-list-label-${flag}`,
}}
/>
</ListItem>
);
})}
{features.length === 0 && (
<Typography sx={{ px: 3, py: 1 }}>
No early access features are currently available.
<br /> Check back later!
</Typography>
)}
</List>
);
};

const Description = memo(function Description({
children,
}: {
children: string;
}) {
return (
<Markdown
options={{
wrapper: Fragment,
overrides: {
p: {
component: Typography,
props: { color: 'inherit' },
},
},
}}
>
{children}
</Markdown>
);
});
20 changes: 20 additions & 0 deletions src/scenes/Root/Header/ProfileMenu/EarlyAccessMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MenuItem, MenuItemProps } from '@mui/material';
import { useDialog } from '../../../../components/Dialog';
import { EarlyAccessDialog } from '../../../EarlyAccess/EarlyAccess';

export const EarlyAccessMenuItem = (props: MenuItemProps) => {
const [state, open] = useDialog();
return (
<>
<MenuItem
onClick={(event) => {
open();
props.onClick?.(event);
}}
>
Early Access Features
</MenuItem>
<EarlyAccessDialog {...state} />
</>
);
};
2 changes: 2 additions & 0 deletions src/scenes/Root/Header/ProfileMenu/ProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ImpersonationContext } from '~/api/client/ImpersonationContext';
import { MenuItemLink } from '../../../../components/Routing';
import { useSession } from '../../../../components/Session';
import { ChangePasswordMenuItem } from './ChangePasswordMenuItem';
import { EarlyAccessMenuItem } from './EarlyAccessMenuItem';
import { ImpersonationMenuItem } from './ImpersonationDialog';
import { ToggleUploadManagerMenuItem } from './ToggleUploadManagerMenuItem';

Expand Down Expand Up @@ -49,6 +50,7 @@ export const ProfileMenu = (props: Partial<MenuProps>) => {
)}
<ToggleUploadManagerMenuItem onClick={handleCloseMenu} />
<ImpersonationMenuItem onClick={handleCloseMenu} />
<EarlyAccessMenuItem />
<MenuItemLink to="/logout">Sign Out</MenuItemLink>
</Menu>
);
Expand Down
Loading