diff --git a/apps/meteor/client/lib/convertTimeUnit.spec.ts b/apps/meteor/client/lib/convertTimeUnit.spec.ts new file mode 100644 index 000000000000..b3ae0939dee3 --- /dev/null +++ b/apps/meteor/client/lib/convertTimeUnit.spec.ts @@ -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'); + }); +}); diff --git a/apps/meteor/client/lib/convertTimeUnit.ts b/apps/meteor/client/lib/convertTimeUnit.ts new file mode 100644 index 000000000000..3ebfc1760e91 --- /dev/null +++ b/apps/meteor/client/lib/convertTimeUnit.ts @@ -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'); + } +}; diff --git a/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.spec.tsx b/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.spec.tsx index 15386e32e203..f16b288b0db2 100644 --- a/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.spec.tsx +++ b/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.spec.tsx @@ -2,7 +2,8 @@ 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(), @@ -10,54 +11,6 @@ global.ResizeObserver = jest.fn().mockImplementation(() => ({ 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 @@ -91,6 +44,7 @@ describe('TimespanSettingInput component', () => { render( { render( { { { { userEvent.click(resetButton); expect(onResetButtonClickMock).toHaveBeenCalled(); + expect(screen.getByDisplayValue('30')).toBeTruthy(); }); }); diff --git a/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.tsx b/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.tsx index 0609b45476cf..9aacb4ca7a7c 100644 --- a/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.tsx +++ b/apps/meteor/client/views/admin/settings/inputs/TimespanSettingInput.tsx @@ -3,6 +3,7 @@ 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'; @@ -10,41 +11,6 @@ type TimespanSettingInputProps = SettingInputProps & { 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) { diff --git a/apps/meteor/client/views/room/body/hooks/useRetentionPolicy.ts b/apps/meteor/client/views/room/body/hooks/useRetentionPolicy.ts index f77bc5bf9948..875b9fb9fbc6 100644 --- a/apps/meteor/client/views/room/body/hooks/useRetentionPolicy.ts +++ b/apps/meteor/client/views/room/body/hooks/useRetentionPolicy.ts @@ -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') { @@ -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), }; }; diff --git a/apps/meteor/client/views/room/contextualBar/Info/EditRoomInfo/useEditRoomInitialValues.ts b/apps/meteor/client/views/room/contextualBar/Info/EditRoomInfo/useEditRoomInitialValues.ts index f36802bb9f56..37253fdb6b4f 100644 --- a/apps/meteor/client/views/room/contextualBar/Info/EditRoomInfo/useEditRoomInitialValues.ts +++ b/apps/meteor/client/views/room/contextualBar/Info/EditRoomInfo/useEditRoomInitialValues.ts @@ -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']) => { @@ -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(`RetentionPolicy_MaxAge_${getPolicyRoomType(room.t)}`) || 30; + const maxAgeSetting = useSetting(`RetentionPolicy_MaxAge_${getPolicyRoomType(room.t)}`); + const maxAgeDefault = maxAgeSetting ? msToTimeUnit(TIMEUNIT.days, maxAgeSetting) : 30; const retentionEnabledDefault = useSetting(`RetentionPolicy_AppliesTo${getPolicyRoomType(room.t)}`); const excludePinnedDefault = useSetting('RetentionPolicy_DoNotPrunePinned'); const filesOnlyDefault = useSetting('RetentionPolicy_FilesOnly');