Skip to content

Commit

Permalink
Fix other places that use the setting
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed May 20, 2024
1 parent 6d13c8f commit d24ba1b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 87 deletions.
49 changes: 49 additions & 0 deletions apps/meteor/client/lib/convertTimeUnit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TIMEUNIT, timeUnitToMs, msToTimeUnit } from './convertTimeUnit';

describe('timeUnitToMs function', () => {
it('should correctly convert days to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.days, 1)).toBe(86400000);
expect(timeUnitToMs(TIMEUNIT.days, 2)).toBe(172800000);
expect(timeUnitToMs(TIMEUNIT.days, 0.5)).toBe(43200000);
});

it('should correctly convert hours to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.hours, 1)).toBe(3600000);
expect(timeUnitToMs(TIMEUNIT.hours, 2)).toBe(7200000);
expect(timeUnitToMs(TIMEUNIT.hours, 0.5)).toBe(1800000);
});

it('should correctly convert minutes to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.minutes, 1)).toBe(60000);
expect(timeUnitToMs(TIMEUNIT.minutes, 2)).toBe(120000);
expect(timeUnitToMs(TIMEUNIT.minutes, 0.5)).toBe(30000);
});

it('should throw an error for invalid time units', () => {
expect(() => timeUnitToMs('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - timeUnitToMs - invalid time unit');
});
});

describe('msToTimeUnit function', () => {
it('should correctly convert milliseconds to days', () => {
expect(msToTimeUnit(TIMEUNIT.days, 86400000)).toBe(1); // 1 day
expect(msToTimeUnit(TIMEUNIT.days, 172800000)).toBe(2); // 2 days
expect(msToTimeUnit(TIMEUNIT.days, 43200000)).toBe(0.5); // .5 days
});

it('should correctly convert milliseconds to hours', () => {
expect(msToTimeUnit(TIMEUNIT.hours, 3600000)).toBe(1); // 1 hour
expect(msToTimeUnit(TIMEUNIT.hours, 7200000)).toBe(2); // 2 hours
expect(msToTimeUnit(TIMEUNIT.hours, 1800000)).toBe(0.5); // .5 hours
});

it('should correctly convert milliseconds to minutes', () => {
expect(msToTimeUnit(TIMEUNIT.minutes, 60000)).toBe(1); // 1 min
expect(msToTimeUnit(TIMEUNIT.minutes, 120000)).toBe(2); // 2 min
expect(msToTimeUnit(TIMEUNIT.minutes, 30000)).toBe(0.5); // .5 min
});

it('should throw an error for invalid time units', () => {
expect(() => msToTimeUnit('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - msToTimeUnit - invalid time unit');
});
});
34 changes: 34 additions & 0 deletions apps/meteor/client/lib/convertTimeUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export enum TIMEUNIT {
days = 'days',
hours = 'hours',
minutes = 'minutes',
}

export const timeUnitToMs = (unit: TIMEUNIT, timespan: number) => {
switch (unit) {
case TIMEUNIT.days:
return timespan * 24 * 60 * 60 * 1000;

case TIMEUNIT.hours:
return timespan * 60 * 60 * 1000;

case TIMEUNIT.minutes:
return timespan * 60 * 1000;

default:
throw new Error('TimespanSettingInput - timeUnitToMs - invalid time unit');
}
};

export const msToTimeUnit = (unit: TIMEUNIT, timespan: number) => {
switch (unit) {
case TIMEUNIT.days:
return timespan / 24 / 60 / 60 / 1000;
case TIMEUNIT.hours:
return timespan / 60 / 60 / 1000;
case TIMEUNIT.minutes:
return timespan / 60 / 1000;
default:
throw new Error('TimespanSettingInput - msToTimeUnit - invalid time unit');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,15 @@ import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';

import { default as TimespanSettingInput, timeUnitToMs, msToTimeUnit, getHighestTimeUnit, TIMEUNIT } from './TimespanSettingInput';
import { TIMEUNIT } from '../../../../lib/convertTimeUnit';
import { default as TimespanSettingInput, getHighestTimeUnit } from './TimespanSettingInput';

global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));

describe('timeUnitToMs function', () => {
it('should correctly convert days to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.days, 1)).toBe(86400000);
expect(timeUnitToMs(TIMEUNIT.days, 2)).toBe(172800000);
expect(timeUnitToMs(TIMEUNIT.days, 0.5)).toBe(43200000);
});

it('should correctly convert hours to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.hours, 1)).toBe(3600000);
expect(timeUnitToMs(TIMEUNIT.hours, 2)).toBe(7200000);
expect(timeUnitToMs(TIMEUNIT.hours, 0.5)).toBe(1800000);
});

it('should correctly convert minutes to milliseconds', () => {
expect(timeUnitToMs(TIMEUNIT.minutes, 1)).toBe(60000);
expect(timeUnitToMs(TIMEUNIT.minutes, 2)).toBe(120000);
expect(timeUnitToMs(TIMEUNIT.minutes, 0.5)).toBe(30000);
});

it('should throw an error for invalid time units', () => {
expect(() => timeUnitToMs('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - timeUnitToMs - invalid time unit');
});
});

describe('msToTimeUnit function', () => {
it('should correctly convert milliseconds to days', () => {
expect(msToTimeUnit(TIMEUNIT.days, 86400000)).toBe(1); // 1 day
expect(msToTimeUnit(TIMEUNIT.days, 172800000)).toBe(2); // 2 days
expect(msToTimeUnit(TIMEUNIT.days, 43200000)).toBe(0.5); // .5 days
});

it('should correctly convert milliseconds to hours', () => {
expect(msToTimeUnit(TIMEUNIT.hours, 3600000)).toBe(1); // 1 hour
expect(msToTimeUnit(TIMEUNIT.hours, 7200000)).toBe(2); // 2 hours
expect(msToTimeUnit(TIMEUNIT.hours, 1800000)).toBe(0.5); // .5 hours
});

it('should correctly convert milliseconds to minutes', () => {
expect(msToTimeUnit(TIMEUNIT.minutes, 60000)).toBe(1); // 1 min
expect(msToTimeUnit(TIMEUNIT.minutes, 120000)).toBe(2); // 2 min
expect(msToTimeUnit(TIMEUNIT.minutes, 30000)).toBe(0.5); // .5 min
});

it('should throw an error for invalid time units', () => {
expect(() => msToTimeUnit('invalidUnit' as TIMEUNIT, 1)).toThrow('TimespanSettingInput - msToTimeUnit - invalid time unit');
});
});

describe('getHighestTimeUnit function', () => {
it('should return minutes if milliseconds cannot be evenly divided into hours or days', () => {
expect(getHighestTimeUnit(900000)).toBe(TIMEUNIT.minutes); // 15 min
Expand Down Expand Up @@ -91,6 +44,7 @@ describe('TimespanSettingInput component', () => {
render(
<TimespanSettingInput
disabled={false}
packageValue='2592000000'
hasResetButton={false}
_id='timespanInput'
label='Timespan'
Expand All @@ -111,6 +65,7 @@ describe('TimespanSettingInput component', () => {
render(
<TimespanSettingInput
disabled={false}
packageValue='2592000000'
hasResetButton={false}
_id='timespanInput'
label='Timespan'
Expand All @@ -133,6 +88,7 @@ describe('TimespanSettingInput component', () => {
<TimespanSettingInput
disabled={false}
hasResetButton={false}
packageValue='2592000000'
_id='timespanInput'
label='Timespan'
value='86400000' // 1 day in milliseconds
Expand All @@ -154,6 +110,7 @@ describe('TimespanSettingInput component', () => {
<TimespanSettingInput
disabled={false}
hasResetButton={false}
packageValue='2592000000'
_id='timespanInput'
label='Timespan'
value='43200000' // half a day
Expand All @@ -175,6 +132,7 @@ describe('TimespanSettingInput component', () => {
<TimespanSettingInput
disabled={false}
_id='timespanInput'
packageValue='2592000000'
label='Timespan'
value='3600000' // 1 hour in milliseconds
placeholder='Enter timespan'
Expand All @@ -188,5 +146,6 @@ describe('TimespanSettingInput component', () => {
userEvent.click(resetButton);

expect(onResetButtonClickMock).toHaveBeenCalled();
expect(screen.getByDisplayValue('30')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,14 @@ import { useTranslation } from '@rocket.chat/ui-contexts';
import type { FormEventHandler, ReactElement } from 'react';
import React, { useMemo, useState } from 'react';

import { TIMEUNIT, timeUnitToMs, msToTimeUnit } from '../../../../lib/convertTimeUnit';
import ResetSettingButton from '../ResetSettingButton';
import type { SettingInputProps } from './types';

type TimespanSettingInputProps = SettingInputProps<string, string | number> & {
value: string;
};

export enum TIMEUNIT {
days = 'days',
hours = 'hours',
minutes = 'minutes',
}

export const timeUnitToMs = (unit: TIMEUNIT, timespan: number) => {
switch (unit) {
case TIMEUNIT.days:
return timespan * 24 * 60 * 60 * 1000;

case TIMEUNIT.hours:
return timespan * 60 * 60 * 1000;

case TIMEUNIT.minutes:
return timespan * 60 * 1000;

default:
throw new Error('TimespanSettingInput - timeUnitToMs - invalid time unit');
}
};

export const msToTimeUnit = (unit: TIMEUNIT, timespan: number) => {
switch (unit) {
case TIMEUNIT.days:
return timespan / 24 / 60 / 60 / 1000;
case TIMEUNIT.hours:
return timespan / 60 / 60 / 1000;
case TIMEUNIT.minutes:
return timespan / 60 / 1000;
default:
throw new Error('TimespanSettingInput - msToTimeUnit - invalid time unit');
}
};

export const getHighestTimeUnit = (value: number): TIMEUNIT => {
const minutes = msToTimeUnit(TIMEUNIT.minutes, value);
if (minutes % 60 !== 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const extractExcludePinned = (room: IRoom, { doNotPrunePinned }: RetentionPolicy

const getMaxAge = (room: IRoom, { maxAgeChannels, maxAgeGroups, maxAgeDMs }: RetentionPolicySettings): number => {
if (hasRetentionPolicy(room) && room.retention.overrideGlobal) {
return room.retention.maxAge;
return room.retention.maxAge * 24 * 60 * 60 * 1000;
}

if (room.t === 'c') {
Expand Down Expand Up @@ -99,6 +99,6 @@ export const useRetentionPolicy = (
return {
filesOnly: extractFilesOnly(room, settings),
excludePinned: extractExcludePinned(room, settings),
maxAge: getMaxAge(room, settings) * 24 * 60 * 60 * 1000,
maxAge: getMaxAge(room, settings),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IRoomWithRetentionPolicy } from '@rocket.chat/core-typings';
import { useSetting } from '@rocket.chat/ui-contexts';
import { useMemo } from 'react';

import { msToTimeUnit, TIMEUNIT } from '../../../../../lib/convertTimeUnit';
import { roomCoordinator } from '../../../../../lib/rooms/roomCoordinator';

const getPolicyRoomType = (roomType: IRoomWithRetentionPolicy['t']) => {
Expand All @@ -19,7 +20,8 @@ export const useEditRoomInitialValues = (room: IRoomWithRetentionPolicy) => {
const { t, ro, archived, topic, description, announcement, joinCodeRequired, sysMes, encrypted, retention, reactWhenReadOnly } = room;

const retentionPolicyEnabled = useSetting('RetentionPolicy_Enabled');
const maxAgeDefault = useSetting<number>(`RetentionPolicy_MaxAge_${getPolicyRoomType(room.t)}`) || 30;
const maxAgeSetting = useSetting<number>(`RetentionPolicy_MaxAge_${getPolicyRoomType(room.t)}`);
const maxAgeDefault = maxAgeSetting ? msToTimeUnit(TIMEUNIT.days, maxAgeSetting) : 30;
const retentionEnabledDefault = useSetting<boolean>(`RetentionPolicy_AppliesTo${getPolicyRoomType(room.t)}`);
const excludePinnedDefault = useSetting('RetentionPolicy_DoNotPrunePinned');
const filesOnlyDefault = useSetting('RetentionPolicy_FilesOnly');
Expand Down

0 comments on commit d24ba1b

Please sign in to comment.