Files
cinny/src/app/utils/reactionOverflow.ts
T

27 lines
985 B
TypeScript
Raw Normal View History

/**
* [Gitea #138] How many reaction chips fit on one row next to a "+N" chip.
*
* Returns `undefined` when every chip fits on the first row (nothing to
* collapse). Otherwise returns the number of leading chips to show; the
* caller renders a "+N" chip after them. Always at least 1 chip.
*/
export function fitReactionRow(
chipWidths: number[],
moreChipWidth: number,
gap: number,
rowWidth: number,
): number | undefined {
const rowLength = (count: number, extra: number) => {
let total = extra;
for (let i = 0; i < count; i += 1) total += chipWidths[i] + (i > 0 || extra > 0 ? gap : 0);
return total;
};
if (rowLength(chipWidths.length, 0) <= rowWidth) return undefined;
let count = chipWidths.length - 1;
while (count > 1 && rowLength(count, 0) + gap + moreChipWidth > rowWidth) count -= 1;
return count;
}
/** Session-only memory of which messages have their reactions expanded. */
export const expandedReactionMessages = new Set<string>();