Skip to content

Commit

Permalink
Restrict hours control
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Feb 23, 2024
1 parent 8d089ba commit 154313d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export function ChannelProgrammingConfig() {
<Button
variant="contained"
startIcon={<Delete />}
onClick={() => restrictHours(5, 6)}
onClick={() => restrictHours(5, 8)}
>
Restrict Hours
</Button>
Expand Down
28 changes: 28 additions & 0 deletions web2/src/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ export const isResolutionString = (
return !isNaN(parseInt(split[0])) && !isNaN(parseInt(split[1]));
};

export const resolutionToString = (res: Resolution) =>

export const isResolutionString = (
str: string,
): str is `${number}x${number}` => {
const split = str.split('x', 2);
if (split.length !== 2) {
return false;
}
return !isNaN(parseInt(split[0])) && !isNaN(parseInt(split[1]));
};

export const resolutionToString = (res: Resolution) =>
`${res.widthPx}x${res.heightPx}` as const;

Expand All @@ -70,6 +82,13 @@ export const resolutionFromAnyString = (res: string) => {
return resolutionFromString(res);
};

export const resolutionFromAnyString = (res: string) => {
if (!isResolutionString(res)) {
return;
}
return resolutionFromString(res);
};

export const hasOnlyDigits = (value: string) => {
return /^-?\d+$/g.test(value);
};
Expand Down Expand Up @@ -106,5 +125,14 @@ export const zipWithIndex = <T extends object>(
}));
};

export const createFlexProgram = (
duration: number,
persisted: boolean = false,
): FlexProgram => ({
duration,
persisted,
type: 'flex',
});

// Useful for toggling state
export const toggle = (b: boolean) => !b;
28 changes: 8 additions & 20 deletions web2/src/hooks/programming_controls/useRestrictHours.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CondensedChannelProgram, isFlexProgram } from '@tunarr/types';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { inRange, reject } from 'lodash-es';
import { forEach, inRange, reject } from 'lodash-es';
import { OneDayMillis } from '../../helpers/constants.ts';
import { createFlexProgram } from '../../helpers/util.ts';
import useStore from '../../store/index.ts';
Expand Down Expand Up @@ -31,36 +31,24 @@ export const restrictHours = (
(p) => isFlexProgram(p) || p.duration > maxDuration,
);

// We start on day 0 of the channel. We will begin pushing programs
// at "from millis" into the day.
const day = 0;
let currOffset = 0; // Offset from the channel start time
const currEndTime = endTimeOffset;

const newPrograms: CondensedChannelProgram[] = [];
while (workingPrograms.length > 0) {
const program = workingPrograms.shift();
if (!program) {
break;
}

forEach(workingPrograms, (program) => {
const timeLeft = maxDuration - currOffset;

// We don't have enough time left today. Push the remainder as flex,
// then increment the day and start over.
if (program.duration > timeLeft) {
// Put the program back and try tomorrow.
workingPrograms.unshift(program);
newPrograms.push(createFlexProgram(timeLeft));
// day++;
// currOffset += day * OneDayMillis + startTimeOffset;
// workingPrograms.unshift(program);
// Flex until the following day's start time
newPrograms.push(
createFlexProgram(timeLeft + OneDayMillis - maxDuration),
);
currOffset = 0;
// currEndTime += day * OneDayMillis + endTimeOffset;
}

newPrograms.push(program);
currOffset += program.duration;
}
});

return { newStartTime, newPrograms };
};
Expand Down

0 comments on commit 154313d

Please sign in to comment.