Skip to content

Commit

Permalink
🩹 front: use logger instead of console (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlinagora committed May 13, 2024
1 parent e6234f9 commit 9048ce7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
DriveFileAccessLevelForPublicLink,
DriveItem,
} from '@features/drive/types';
import Logger from '@features/global/framework/logger-service';

const getLogger = () => Logger.getLogger('access-info-helpers');

const entityMatcher = (entityType: AuthEntity['type'], entityId?: string) =>
(entity: AuthEntity) =>
Expand All @@ -14,7 +17,7 @@ const entityMatcher = (entityType: AuthEntity['type'], entityId?: string) =>
/** Updates the level of entities matching the given type and id, or adds the return
* of updater with no argument.
*
* If an item is not unique, all are modified and a console warning is printed except for `entityType` "channel".
* If an item is not unique, all are modified and a logger warning is printed except for `entityType` "channel".
*/
function upsertEntities(
entities: AuthEntity[] | undefined,
Expand All @@ -34,7 +37,7 @@ function upsertEntities(
const matcher = entityMatcher(entityType, entityId);
const mapped = entities.map(item => {
if (!matcher(item)) return item;
if (found && entityType != "channel") console.warn(`DriveItem has more than one access_info entry for '${entityType}' id = ${entityId}:`, item);
if (found && entityType != "channel") getLogger().warn(`DriveItem has more than one access_info entry for '${entityType}' id = ${entityId}:`, item);
found = true;
return updater(item);
}).filter(x => x);
Expand Down Expand Up @@ -83,12 +86,12 @@ const itemWithEntityAccessChanged = (
},
} as DriveItem);

// Note this just assumes uniqueness and just logs in the console if that is not the case and uses the first one.
// Note this just assumes uniqueness and just logs if that is not the case and uses the first one.
const getAccessLevelOfUniqueForType = (item: DriveItem | undefined, entityType: AuthEntity['type'], entityId?: string) => {
if (!item) return undefined;
const accesses = item.access_info.entities.filter(entityMatcher(entityType, entityId));
if (accesses.length != 1 && entityType != "channel")
console.warn(`DriveItem doesn't have exactly one access_info entry for '${entityType}${entityId ? " id: " + entityId : ""}':`, item);
getLogger().warn(`DriveItem doesn't have exactly one access_info entry for '${entityType}${entityId ? " id: " + entityId : ""}':`, item);
return accesses[0]?.level;
}

Expand All @@ -99,7 +102,7 @@ export const changeUserAccess = (item: DriveItem, userId: string, level: DriveFi
/** Return a list of all DriveItemAccessInfo for users sorted by id */
export const getAllUserAccesses = (item: DriveItem) => accessInfosOfEntitiesOfType(item, "user");

/** Return the access level for the provided user; an entry is expected to exist (or there is a `console.warn`) */
/** Return the access level for the provided user; an entry is expected to exist (or there is a `logger.warn`) */
export const getUserAccessLevel = (item: DriveItem, userId: string) => getAccessLevelOfUniqueForType(item, "user", userId);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'moment/min/locales';
import { useEffect, useRef, useState } from 'react';
import { uniqueId } from 'lodash';

import Logger from '@features/global/framework/logger-service';
import Languages from 'features/global/services/languages-service';

import BaseBlock from '@molecules/grouped-rows/base';
Expand All @@ -16,6 +17,8 @@ import { ConfirmModal } from 'app/atoms/modal/confirm';

import Styles from './styles';

const getLogger = () => Logger.getLogger('expiry-editor-row');

export const ExpiryEditorRow = (props: {
disabled?: boolean;
value: number;
Expand Down Expand Up @@ -54,7 +57,7 @@ export const ExpiryEditorRow = (props: {
setIsEditing(false);
},
(e: unknown) => {
console.error("Error while saving expiry date:", e);
getLogger().error("Error while saving expiry date:", e);
setIsWaitingForSave(false);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react';
import { uniqueId } from 'lodash';

import Languages from 'features/global/services/languages-service';
import Logger from '@features/global/framework/logger-service';

import BaseBlock from '@molecules/grouped-rows/base';
import { Base } from '@atoms/text';
Expand All @@ -13,6 +14,8 @@ import { ConfirmModal } from 'app/atoms/modal/confirm';

import Styles from './styles';

const getLogger = () => Logger.getLogger('password-editor-row');

export const PasswordEditorRow = (props: {
disabled?: boolean;
password?: string;
Expand Down Expand Up @@ -51,7 +54,7 @@ export const PasswordEditorRow = (props: {
setIsEditingPassword(false);
},
(e: unknown) => {
console.error("Error while saving password:", e);
getLogger().error("Error while saving password:", e);
setIsWaitingForPasswordSave(false);
},
);
Expand All @@ -70,7 +73,7 @@ export const PasswordEditorRow = (props: {
savePassword(password);
}

return <>
return <>
<BaseBlock
className={"m-4" + (disabled ? Styles.Disabled.Yes : "")}
disabled={disabled}
Expand Down

0 comments on commit 9048ce7

Please sign in to comment.