Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
53 changes: 52 additions & 1 deletion apps/frontend/src/components/new-launch/manage.modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { FC, useCallback, useRef, useState } from 'react';
import React, { FC, useCallback, useEffect, useRef, useState } from 'react';
import { AddEditModalProps } from '@gitroom/frontend/components/new-launch/add.edit.modal';
import clsx from 'clsx';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
Expand All @@ -27,6 +27,8 @@ import { SelectCustomer } from '@gitroom/frontend/components/launches/select.cus
import { CopilotPopup } from '@copilotkit/react-ui';
import { DummyCodeComponent } from '@gitroom/frontend/components/new-launch/dummy.code.component';
import { stripHtmlValidation } from '@gitroom/helpers/utils/strip.html.validation';
import { Checkbox } from '@gitroom/react/form/checkbox';
import dayjs from 'dayjs';

function countCharacters(text: string, type: string): number {
if (type !== 'x') {
Expand All @@ -41,6 +43,7 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
const ref = useRef(null);
const existingData = useExistingData();
const [loading, setLoading] = useState(false);
const [randomizeMinute, setRandomizeMinute] = useState(true);
const toaster = useToaster();
const modal = useModals();

Expand Down Expand Up @@ -132,6 +135,26 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
[integrations]
);

const didRandomizeInitially = useRef(false);
useEffect(() => {
if (dummy) return;
if (!randomizeMinute) return;
if (existingData.integration) return;
if (!date) return;
if (didRandomizeInitially.current) return;
if (date.minute() !== 0) return;

const base = date.clone().second(0).millisecond(0);
let minute = Math.floor(Math.random() * 60);
const now = dayjs();
if (base.isSame(now, 'hour')) {
const minNowPlus1 = now.minute() + 1;
if (minute < minNowPlus1) minute = Math.min(minNowPlus1, 59);
}
setDate(base.minute(minute));
didRandomizeInitially.current = true;
}, [randomizeMinute, date, dummy, existingData, setDate]);

const schedule = useCallback(
(type: 'draft' | 'now' | 'schedule') => async () => {
setLoading(true);
Expand Down Expand Up @@ -233,6 +256,7 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
...(repeater ? { inter: repeater } : {}),
tags,
shortLink,
randomizeMinute,
date: date.utc().format('YYYY-MM-DDTHH:mm:ss'),
posts: checkAllValid.map((post: any) => ({
integration: {
Expand Down Expand Up @@ -332,6 +356,33 @@ export const ManageModal: FC<AddEditModalProps> = (props) => {
<RepeatComponent repeat={repeater} onChange={setRepeater} />
)}
<DatePicker onChange={setDate} date={date} />
{!dummy && !existingData.integration && (
<div className="ms-[12px] flex items-center gap-[6px]">
<Checkbox
disableForm={true}
name="randomizeMinute"
checked={randomizeMinute}
onChange={(e: any) => {
const next = e?.target ? !!e.target.value : !!e;
setRandomizeMinute(next);
const base = date.clone().second(0).millisecond(0);
if (next) {
const currentMinute = base.minute();
let minute = Math.floor(Math.random() * 60);
let tries = 0;
while (minute === currentMinute && tries < 5) {
minute = Math.floor(Math.random() * 60);
tries++;
}
setDate(base.minute(minute));
} else {
setDate(base.minute(0));
}
}}
/>
<div>{t('randomize_minute', 'Randomize minute')}</div>
</div>
)}
</div>
</TopTitle>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,4 +1006,4 @@ export class PostsService {
message
);
}
}
}
4 changes: 4 additions & 0 deletions libraries/nestjs-libraries/src/dtos/posts/create.post.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class CreatePostDto {
@IsNumber()
inter?: number;

@IsOptional()
@IsBoolean()
randomizeMinute?: boolean;

@IsDefined()
@IsDateString()
date: string;
Expand Down