From 5c13d5833c9842b8ab8291d5dac24ec6fa56fd07 Mon Sep 17 00:00:00 2001 From: spaenleh Date: Tue, 14 Jan 2025 09:06:52 +0100 Subject: [PATCH] fix: code smell --- .../memberAnalytics/MemberActionsChart.tsx | 40 +-- src/modules/analytics/mockServer/database.ts | 20 -- .../analytics/mockServer/mockServer.ts | 278 ------------------ .../landing/Platforms/PlatformCube.tsx | 2 +- 4 files changed, 23 insertions(+), 317 deletions(-) delete mode 100644 src/modules/analytics/mockServer/database.ts delete mode 100644 src/modules/analytics/mockServer/mockServer.ts diff --git a/src/modules/account/home/memberAnalytics/MemberActionsChart.tsx b/src/modules/account/home/memberAnalytics/MemberActionsChart.tsx index c43429ce3..2b189d14b 100644 --- a/src/modules/account/home/memberAnalytics/MemberActionsChart.tsx +++ b/src/modules/account/home/memberAnalytics/MemberActionsChart.tsx @@ -31,6 +31,24 @@ import { import { MyAnalyticsDateRangeDataContext } from '~analytics/context/MyAnalyticsDateRangeContext'; import { groupActions } from '~analytics/utils'; +function getFreqFromData(actions: [string, Action[]][]) { + const { months, days, years } = intervalToDuration({ + start: new Date(actions[0]?.[0]), + end: new Date(actions[1]?.[0]), + }); + + // get bar interval + if (years && years >= 1) { + return GroupByInterval.Year; + } else if (days === 1) { + return GroupByInterval.Day; + } else if (months === 1 && !days) { + return GroupByInterval.Month; + } else { + return 'other'; + } +} + export function MemberActionsChart({ actions, }: Readonly<{ @@ -56,24 +74,10 @@ export function MemberActionsChart({ isLargeScreen ? MAX_BARS_LARGE_SCREEN : MAX_BARS_SMALL_SCREEN, ); - const actionsEntires = Object.entries(groupedActionsByInterval); + const actionsEntries = Object.entries(groupedActionsByInterval); + const freq = getFreqFromData(actionsEntries); - const { months, days, years } = intervalToDuration({ - start: new Date(actionsEntires[0]?.[0]), - end: new Date(actionsEntires[1]?.[0]), - }); - - // get bar interval - const freq = - years && years >= 1 - ? GroupByInterval.Year - : days === 1 - ? GroupByInterval.Day - : months === 1 && !days - ? GroupByInterval.Month - : 'other'; - - const noOfActionTypesOverInterval = actionsEntires.map( + const noOfActionTypesOverInterval = actionsEntries.map( ([dateString, localActions], index) => { const actionsOverIntervalTypeCounts = countBy(localActions, 'type'); @@ -81,7 +85,7 @@ export function MemberActionsChart({ let title = ''; const date = new Date(dateString); const nextDate = new Date( - actionsEntires[index + 1]?.[0] || dateRange.endDate, + actionsEntries[index + 1]?.[0] || dateRange.endDate, ); switch (freq) { diff --git a/src/modules/analytics/mockServer/database.ts b/src/modules/analytics/mockServer/database.ts deleted file mode 100644 index 001a84e9f..000000000 --- a/src/modules/analytics/mockServer/database.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { DiscriminatedItem, ItemMembership, Member } from '@graasp/sdk'; - -type Database = { - currentMember?: Member; - items?: DiscriminatedItem[]; - itemMemberships?: ItemMembership[]; - members?: Member[]; -}; - -export const buildDatabase = ({ - currentMember, - items = [], - itemMemberships = [], - members, -}: Partial = {}): Database => ({ - currentMember, - items, - itemMemberships, - members: members ?? (currentMember ? [currentMember] : []), -}); diff --git a/src/modules/analytics/mockServer/mockServer.ts b/src/modules/analytics/mockServer/mockServer.ts deleted file mode 100644 index 510ec8069..000000000 --- a/src/modules/analytics/mockServer/mockServer.ts +++ /dev/null @@ -1,278 +0,0 @@ -// import { API_ROUTES } from '@graasp/query-client'; -// import { DiscriminatedItem, ItemMembership, Member } from '@graasp/sdk'; - -// import { StatusCodes } from 'http-status-codes'; -// import { -// Model, -// Response, -// RestSerializer, -// Server, -// createServer, -// } from 'miragejs'; - -// import MOCK_ACTION_DATA from '../cypress/fixtures/actions'; -// import { -// MOCK_AGGREGATE_ACTIONS_ACTIVE_USERS, -// MOCK_AGGREGATE_ACTIONS_BY_DAY, -// MOCK_AGGREGATE_ACTIONS_BY_TIME, -// MOCK_AGGREGATE_ACTIONS_BY_WEEKDAY, -// MOCK_AGGREGATE_ACTIONS_TOTAL_ACTIONS, -// MOCK_AGGREGATE_ACTIONS_TOTAL_USERS, -// MOCK_AGGREGATE_ACTIONS_TYPE, -// } from '../cypress/fixtures/aggregateActions'; -// import { buildDatabase } from './database'; - -// const { buildGetItemRoute, buildGetCurrentMemberRoute } = API_ROUTES; - -// const ApplicationSerializer = RestSerializer.extend({ -// root: false, -// embed: true, -// }); - -// const checkPermission = ( -// // todo: improve type -// schema: any, -// itemId: string, -// currentMember?: Member, -// ) => { -// // todo: apply public -// if (!currentMember) { -// return false; -// } - -// const item = schema.find('item', itemId); -// if (!item) { -// return false; -// } -// if (currentMember?.id === item.creator.id) { -// return true; -// } -// const itemPath = item.path; -// const validPaths = schema -// .all('membership') -// .filter(({ account }: ItemMembership) => account.id === currentMember?.id) -// .models.map((i: ItemMembership) => i.item.path); -// return validPaths.some((path: string) => itemPath.includes(path)); -// }; - -// const buildPathFromId = (id: string) => id.replace(/-/g, '_'); - -// const mockServer = ({ -// urlPrefix, -// database = buildDatabase(), -// externalUrls = [], -// }: { -// urlPrefix?: string; -// database?: ReturnType; -// externalUrls?: string[]; -// }): Server => { -// const { items, members, itemMemberships } = database; -// const currentMember = members?.[0]; - -// return createServer({ -// // environment -// urlPrefix, -// models: { -// item: Model, -// member: Model, -// membership: Model, -// }, - -// serializers: { -// item: ApplicationSerializer, -// member: ApplicationSerializer, -// membership: ApplicationSerializer, -// }, -// seeds(server) { -// members?.forEach((m) => { -// server.create('member', m); -// }); -// items?.forEach((i) => { -// server.create('item', i); -// }); -// itemMemberships?.forEach((m) => { -// server.create('membership', m); -// }); -// }, -// routes() { -// // get current member -// this.get(`/${buildGetCurrentMemberRoute()}`, () => { -// if (currentMember) { -// return currentMember; -// } - -// return new Response(StatusCodes.UNAUTHORIZED); -// }); - -// // members route -// this.get(`/members/:id`, (schema, request) => { -// return schema.find('member', request.params.id); -// }); -// // members avatar route -// this.get(`/members/:id/avatar/:size`, () => { -// return new Response(StatusCodes.NOT_FOUND); -// }); - -// // membership -// this.get(`/item-memberships`, (schema, request) => { -// const itemId = request.queryParams.itemId; -// if (!itemId) { -// throw new Error('item id does not exist'); -// } - -// const memberships = schema -// .all('membership') -// // TODO: remove any after figuring out the type -// .filter(({ item }: any) => itemId === item.id); - -// return { data: { [itemId as string]: memberships.models } }; -// }); - -// // get item -// this.get(`/${buildGetItemRoute(':id')}`, (schema, request) => { -// const itemId = request.url.split('/').at(-1); -// if (!itemId) { -// throw new Error('item id does not exist'); -// } -// if (!checkPermission(schema, itemId, currentMember)) { -// return new Response(StatusCodes.FORBIDDEN); -// } -// if (!itemId) { -// throw new Error('item id does not exist'); -// } -// return schema.find('item', itemId); -// }); - -// // get children -// this.get(`/items/:id/children`, (schema, request) => { -// const itemId = request.params.id; -// if (!itemId) { -// throw new Error('item id does not exist'); -// } - -// return ( -// schema -// .all('item') -// // TODO: remove any after figuring out the type -// .filter(({ id, path }: any) => -// path.includes( -// `${buildPathFromId(itemId)}.${buildPathFromId(id)}`, -// ), -// ) -// ); -// }); - -// // get descendants -// this.get(`/items/:id/descendants`, (schema, request) => { -// const itemId = request.params.id; -// if (!itemId) { -// throw new Error('item id does not exist'); -// } -// const allItems = schema.all('item'); -// const descendantsOfItem = allItems.filter(({ id, path }: any) => -// path.includes(`${buildPathFromId(itemId)}.${buildPathFromId(id)}`), -// ); -// return descendantsOfItem; -// }); - -// // get parents -// this.get(`/items/:id/parents`, (schema, request) => { -// const itemId = request.params.id; -// if (!itemId) { -// throw new Error('item id does not exist'); -// } -// const itemPath = ( -// schema.find('item', itemId) as unknown as DiscriminatedItem -// ).path; - -// return ( -// schema -// .all('item') -// // TODO: remove any after figuring out the type -// .filter( -// ({ path }: any) => itemPath.startsWith(path) && itemPath !== path, -// ) -// ); -// }); - -// // get actions -// this.get(`/items/:id/actions`, () => MOCK_ACTION_DATA); - -// // get aggregate actions -// this.get(`/items/:id//actions/aggregation`, (_schema, request) => { -// const { -// countGroupBy, -// aggregateBy, -// aggregateMetric, -// aggregateFunction, -// } = request.queryParams; - -// if ( -// countGroupBy === 'user' && -// aggregateMetric === 'user' && -// aggregateFunction === 'count' -// ) { -// return MOCK_AGGREGATE_ACTIONS_TOTAL_USERS; -// } -// if ( -// countGroupBy === 'createdDay' && -// aggregateBy === 'createdDay' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'count' -// ) { -// return MOCK_AGGREGATE_ACTIONS_ACTIVE_USERS; -// } -// if ( -// countGroupBy === 'createdDay' && -// aggregateBy === 'createdDay' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'avg' -// ) { -// return MOCK_AGGREGATE_ACTIONS_BY_DAY; -// } -// if ( -// countGroupBy === 'createdDay' && -// aggregateBy === 'createdDay' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'sum' -// ) { -// return MOCK_AGGREGATE_ACTIONS_TOTAL_ACTIONS; -// } -// if ( -// countGroupBy === 'createdTimeOfDay' && -// aggregateBy === 'createdTimeOfDay' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'avg' -// ) { -// return MOCK_AGGREGATE_ACTIONS_BY_TIME; -// } -// if ( -// countGroupBy === 'createdDayOfWeek' && -// aggregateBy === 'createdDayOfWeek' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'avg' -// ) { -// return MOCK_AGGREGATE_ACTIONS_BY_WEEKDAY; -// } -// if ( -// countGroupBy === 'actionType' && -// aggregateBy === 'actionType' && -// aggregateMetric === 'actionCount' && -// aggregateFunction === 'sum' -// ) { -// return MOCK_AGGREGATE_ACTIONS_TYPE; -// } -// return []; -// }); - -// this.post(`/items/:id/actions/export`, () => { -// return { success: true }; -// }); - -// // passthrough external urls -// externalUrls.forEach((url) => { -// this.passthrough(url); -// }); -// }, -// }); -// }; diff --git a/src/modules/landing/Platforms/PlatformCube.tsx b/src/modules/landing/Platforms/PlatformCube.tsx index 313aeeeee..122950545 100644 --- a/src/modules/landing/Platforms/PlatformCube.tsx +++ b/src/modules/landing/Platforms/PlatformCube.tsx @@ -20,7 +20,7 @@ import { import { PlatformColorSurface } from './PlatformColorSurface'; -function Row({ children }: { children: ReactNode }) { +function Row({ children }: Readonly<{ children: ReactNode }>) { return ( {children}