- remove unnecessary / deprecated code
This commit is contained in:
103
src/state.ts
103
src/state.ts
@@ -11,25 +11,6 @@ export interface OptionRecord {
|
||||
createdBy: string;
|
||||
}
|
||||
|
||||
export interface PollOptionViewModel extends OptionRecord {
|
||||
voteCount: number;
|
||||
isVotedByMe: boolean;
|
||||
percentage: number;
|
||||
}
|
||||
|
||||
export interface PollViewModel {
|
||||
title: string;
|
||||
roomId: string;
|
||||
shareUrl: string;
|
||||
connectionStatus: ConnectionStatus;
|
||||
peerCount: number;
|
||||
options: PollOptionViewModel[];
|
||||
totalVotes: number;
|
||||
myVoteOptionId: string | null;
|
||||
deadline: number | null;
|
||||
votingClosed: boolean;
|
||||
}
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
export function createOptionId(): string {
|
||||
@@ -104,20 +85,6 @@ export function toggleVote(
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteOption(
|
||||
options: Y.Map<OptionRecord>,
|
||||
votes: Y.Map<string>,
|
||||
optionId: string,
|
||||
): void {
|
||||
options.delete(optionId);
|
||||
// Clean up votes pointing to this option
|
||||
for (const [userId, votedOptionId] of votes.entries()) {
|
||||
if (votedOptionId === optionId) {
|
||||
votes.delete(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Deadline ---
|
||||
|
||||
export function setDeadline(
|
||||
@@ -135,73 +102,3 @@ export function getDeadline(deadlineMap: Y.Map<unknown>): number | null {
|
||||
const val = deadlineMap.get("deadline");
|
||||
return typeof val === "number" ? val : null;
|
||||
}
|
||||
|
||||
// --- ViewModel ---
|
||||
|
||||
export function createViewModel(params: {
|
||||
yTitle: Y.Text;
|
||||
options: Y.Map<OptionRecord>;
|
||||
votes: Y.Map<string>;
|
||||
deadlineMap: Y.Map<unknown>;
|
||||
roomId: string;
|
||||
shareUrl: string;
|
||||
connectionStatus: ConnectionStatus;
|
||||
peerCount: number;
|
||||
userId: string;
|
||||
}): PollViewModel {
|
||||
const {
|
||||
yTitle,
|
||||
options,
|
||||
votes,
|
||||
deadlineMap,
|
||||
roomId,
|
||||
shareUrl,
|
||||
connectionStatus,
|
||||
peerCount,
|
||||
userId,
|
||||
} = params;
|
||||
|
||||
// Tally votes per option
|
||||
const tally = new Map<string, number>();
|
||||
for (const optionId of votes.values()) {
|
||||
tally.set(optionId, (tally.get(optionId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
let totalVotes = 0;
|
||||
for (const count of tally.values()) {
|
||||
totalVotes += count;
|
||||
}
|
||||
|
||||
const myVoteOptionId = votes.get(userId) ?? null;
|
||||
const deadline = getDeadline(deadlineMap);
|
||||
const votingClosed = deadline !== null && Date.now() >= deadline;
|
||||
|
||||
// Sort by votes desc, then alphabetically
|
||||
const sortedOptions = Array.from(options.values()).sort((a, b) => {
|
||||
const aVotes = tally.get(a.id) ?? 0;
|
||||
const bVotes = tally.get(b.id) ?? 0;
|
||||
if (bVotes !== aVotes) return bVotes - aVotes;
|
||||
return a.label.localeCompare(b.label);
|
||||
});
|
||||
|
||||
return {
|
||||
title: getPollTitle(yTitle),
|
||||
roomId,
|
||||
shareUrl,
|
||||
connectionStatus,
|
||||
peerCount,
|
||||
myVoteOptionId,
|
||||
totalVotes,
|
||||
deadline,
|
||||
votingClosed,
|
||||
options: sortedOptions.map((option) => ({
|
||||
...option,
|
||||
voteCount: tally.get(option.id) ?? 0,
|
||||
isVotedByMe: myVoteOptionId === option.id,
|
||||
percentage:
|
||||
totalVotes > 0
|
||||
? Math.round(((tally.get(option.id) ?? 0) / totalVotes) * 100)
|
||||
: 0,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user