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: Personal flags UI component #8221

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
72 changes: 67 additions & 5 deletions frontend/src/component/personalDashboard/PersonalDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { WelcomeDialog } from './WelcomeDialog';
import { useLocalStorageState } from 'hooks/useLocalStorageState';
import useProjectOverview from 'hooks/api/getters/useProjectOverview/useProjectOverview';
import { ProjectSetupComplete } from './ProjectSetupComplete';
import { usePersonalDashboard } from 'hooks/api/getters/usePersonalDashboard/usePersonalDashboard';
import { getFeatureTypeIcons } from 'utils/getFeatureTypeIcons';

const ScreenExplanation = styled(Typography)(({ theme }) => ({
marginTop: theme.spacing(1),
Expand Down Expand Up @@ -136,13 +138,56 @@ const useProjects = () => {
return { projects, activeProject, setActiveProject };
};

const FlagListItem: FC<{
flag: { name: string; project: string; type: string };
selected: boolean;
onClick: () => void;
}> = ({ flag, selected, onClick }) => {
const IconComponent = getFeatureTypeIcons(flag.type);
return (
<ListItem key={flag.name} disablePadding={true} sx={{ mb: 1 }}>
<ListItemButton
sx={projectStyle}
selected={selected}
onClick={onClick}
>
<ProjectBox>
<IconComponent color='primary' />
<StyledCardTitle>{flag.name}</StyledCardTitle>
<IconButton
component={Link}
href={`projects/${flag.project}/features/${flag.name}`}
size='small'
sx={{ ml: 'auto' }}
>
<LinkIcon
titleAccess={`projects/${flag.project}/features/${flag.name}`}
/>
</IconButton>
</ProjectBox>
</ListItemButton>
</ListItem>
);
};

export const PersonalDashboard = () => {
const { user } = useAuthUser();

const name = user?.name;

const { projects, activeProject, setActiveProject } = useProjects();

const { personalDashboard } = usePersonalDashboard();
Copy link
Contributor Author

@kwasniew kwasniew Sep 23, 2024

Choose a reason for hiding this comment

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

I will extract this code into a custom hook once we find shared items between flags and projects

const [activeFlag, setActiveFlag] = useState<string | null>(null);
useEffect(() => {
if (
personalDashboard?.flags.length &&
personalDashboard?.flags.length > 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this meant to be more like this (though both will work)?

Suggested change
personalDashboard?.flags.length &&
personalDashboard?.flags.length > 0
personalDashboard?.flags &&
personalDashboard?.flags.length > 0

We should also be able to simplify it:

Suggested change
personalDashboard?.flags.length &&
personalDashboard?.flags.length > 0
personalDashboard?.flags.length > 0

though TS might require us to do

Suggested change
personalDashboard?.flags.length &&
personalDashboard?.flags.length > 0
(personalDashboard?.flags.length ?? 0) > 0

at which point ... maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if(personalDashboard?.flags.length) will do

) {
setActiveFlag(personalDashboard.flags[0].name);
}
}, [JSON.stringify(personalDashboard)]);

const { project: activeProjectOverview, loading } =
useProjectOverview(activeProject);

Expand Down Expand Up @@ -255,11 +300,28 @@ export const PersonalDashboard = () => {
<Typography variant='h3'>My feature flags</Typography>
</SpacedGridItem>
<SpacedGridItem item lg={8} md={1} />
<SpacedGridItem item lg={4} md={1}>
<Typography>
You have not created or favorited any feature flags.
Once you do, the will show up here.
</Typography>
<SpacedGridItem
item
lg={4}
md={1}
sx={{ maxHeight: '400px', overflow: 'auto' }}
>
{personalDashboard && personalDashboard.flags.length > 0 ? (
personalDashboard.flags.map((flag) => {
return (
<FlagListItem
flag={flag}
selected={flag.name === activeFlag}
onClick={() => setActiveFlag(flag.name)}
/>
);
})
) : (
<Typography>
You have not created or favorited any feature flags.
Once you do, the will show up here.
</Typography>
)}
</SpacedGridItem>
<SpacedGridItem item lg={8} md={1}>
<Typography sx={{ mb: 4 }}>Feature flag metrics</Typography>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import useSWR from 'swr';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import type { PersonalDashboardSchema } from 'openapi';

export interface IPersonalDashboardOutput {
personalDashboard?: PersonalDashboardSchema;
refetch: () => void;
loading: boolean;
error?: Error;
}

export const usePersonalDashboard = (): IPersonalDashboardOutput => {
const { data, error, mutate } = useSWR(
formatApiPath('api/admin/personal-dashboard'),
fetcher,
);

return {
personalDashboard: data,
loading: !error && !data,
refetch: () => mutate(),
error,
};
};

const fetcher = (path: string) => {
return fetch(path)
.then(handleErrorResponses('Personal Dashboard'))
.then((res) => res.json());
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
export type PersonalDashboardSchemaFlagsItem = {
/** The name of the flag */
name: string;
project: string;
type: string;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { IPersonalDashboardReadModel } from './personal-dashboard-read-model-type';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
} from './personal-dashboard-read-model-type';

export class FakePersonalDashboardReadModel
implements IPersonalDashboardReadModel
{
async getPersonalFeatures(userId: number): Promise<{ name: string }[]> {
async getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ test('should return personal dashboard with own flags and favorited flags', asyn
expect(body).toMatchObject({
projects: [],
flags: [
{ name: 'my_feature_c' },
{ name: 'my_feature_d' },
{ name: 'other_feature_b' },
{ name: 'my_feature_d', type: 'release', project: 'default' },
{ name: 'my_feature_c', type: 'release', project: 'default' },
{ name: 'other_feature_b', type: 'release', project: 'default' },
],
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type PersonalFeature = { name: string; type: string; project: string };

export interface IPersonalDashboardReadModel {
getPersonalFeatures(userId: number): Promise<{ name: string }[]>;
getPersonalFeatures(userId: number): Promise<PersonalFeature[]>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Db } from '../../db/db';
import type { IPersonalDashboardReadModel } from './personal-dashboard-read-model-type';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
} from './personal-dashboard-read-model-type';

export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
private db: Db;
Expand All @@ -8,15 +11,34 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
this.db = db;
}

getPersonalFeatures(userId: number): Promise<{ name: string }[]> {
return this.db<{ name: string }>('favorite_features')
async getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
const result = await this.db<{
name: string;
type: string;
project: string;
}>('favorite_features')
.join('features', 'favorite_features.feature', 'features.name')
.where('favorite_features.user_id', userId)
.select('feature as name')
.whereNull('features.archived_at')
.select(
'features.name as name',
'features.type',
'features.project',
'features.created_at',
)
.union(function () {
this.select('name')
this.select('name', 'type', 'project', 'created_at')
.from('features')
.where('features.created_by_user_id', userId);
.where('features.created_by_user_id', userId)
.whereNull('features.archived_at'); // Ensuring archived_at is null in the features table
})
.orderBy('name', 'asc');
.orderBy('created_at', 'desc')
.limit(100);

return result.map((row) => ({
name: row.name,
type: row.type,
project: row.project,
}));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { IPersonalDashboardReadModel } from './personal-dashboard-read-model-type';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
} from './personal-dashboard-read-model-type';

export class PersonalDashboardService {
private personalDashboardReadModel: IPersonalDashboardReadModel;
Expand All @@ -7,7 +10,7 @@ export class PersonalDashboardService {
this.personalDashboardReadModel = personalDashboardReadModel;
}

getPersonalFeatures(userId: number): Promise<{ name: string }[]> {
getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
return this.personalDashboardReadModel.getPersonalFeatures(userId);
}
}
10 changes: 10 additions & 0 deletions src/lib/openapi/spec/personal-dashboard-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export const personalDashboardSchema = {
example: 'my-flag',
description: 'The name of the flag',
},
project: {
type: 'string',
example: 'my-project-id',
description: 'The id of the feature project',
},
type: {
type: 'string',
example: 'release',
description: 'The type of the feature flag',
},
},
},
description: 'A list of flags a user created or favorited',
Expand Down
1 change: 1 addition & 0 deletions src/server-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ process.nextTick(async () => {
addonUsageMetrics: true,
onboardingMetrics: true,
onboardingUI: true,
personalDashboardUI: true,
},
},
authentication: {
Expand Down