Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-collapse capability to pinned messages based on timeout provided by bannerProperties #156

Merged
merged 2 commits into from
Dec 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/components/ChatSummary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
clearTimeout(autoHideTimeout);
autoHideTimeout = null;
}
};
};

$: if (summary) {
dismissed = false;
Expand Down
14 changes: 12 additions & 2 deletions src/components/PinnedMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@

let dismissed = false;
let shorten = false;
let autoHideTimeout: NodeJS.Timeout | null = null;
const classes = 'rounded inline-flex flex-col overflow-visible ' +
'bg-secondary-900 p-2 w-full text-white z-10 shadow';

const onShorten = () => { shorten = !shorten; };

const onShorten = () => {
shorten = !shorten;
if (autoHideTimeout) {
clearTimeout(autoHideTimeout);
autoHideTimeout = null;
}
};

$: if (pinned) {
dismissed = false;
shorten = false;
if (pinned.showtime) {
autoHideTimeout = setTimeout(() => { shorten = true; }, pinned.showtime);
}
}

const dispatch = createEventDispatcher();
Expand Down
15 changes: 11 additions & 4 deletions src/ts/chat-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const splitRunsByNewline = (runs: Ytc.ParsedRun[], maxSplit: number = -1): Ytc.P
return acc;
}, [[]]);

const parseChatSummary = (renderer: Ytc.AddChatItem, isEphemeral: boolean, bannerTimeoutMs: number): Ytc.ParsedSummary | undefined => {
const parseChatSummary = (renderer: Ytc.AddChatItem, showtime: number): Ytc.ParsedSummary | undefined => {
if (!renderer.liveChatBannerChatSummaryRenderer) {
return;
}
Expand All @@ -101,7 +101,7 @@ const parseChatSummary = (renderer: Ytc.AddChatItem, isEphemeral: boolean, banne
message: splitRuns[2],
},
id: baseRenderer.liveChatSummaryId,
showtime: isEphemeral ? bannerTimeoutMs : 0,
showtime: showtime,
};
return item;
}
Expand Down Expand Up @@ -230,8 +230,14 @@ const parseMessageDeletedAction = (action: Ytc.MessageDeletedAction): Ytc.Parsed

const parseBannerAction = (action: Ytc.AddPinnedAction): Ytc.ParsedPinned | Ytc.ParsedSummary | undefined => {
const baseRenderer = action.bannerRenderer.liveChatBannerRenderer;

// fold both auto-disappear and auto-collapse into just collapse for showtime
const showtime = action.bannerProperties?.isEphemeral
? (action.bannerProperties?.bannerTimeoutMs || 0)
: 1000 * (action.bannerProperties?.autoCollapseDelay?.seconds || baseRenderer.bannerProperties?.autoCollapseDelay?.seconds || 0);

if (baseRenderer.contents.liveChatBannerChatSummaryRenderer) {
return parseChatSummary(baseRenderer.contents, action.bannerProperties?.isEphemeral ?? false, action.bannerProperties?.bannerTimeoutMs ?? 0);
return parseChatSummary(baseRenderer.contents, showtime);
}
const parsedContents = parseAddChatItemAction(
{ item: baseRenderer.contents }, true
Expand All @@ -246,7 +252,8 @@ const parseBannerAction = (action: Ytc.AddPinnedAction): Ytc.ParsedPinned | Ytc.
baseRenderer.header.liveChatBannerHeaderRenderer.text.runs
),
contents: parsedContents
}
},
showtime: showtime,
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/ts/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const isValidFrameInfo = (f: Chat.UncheckedFrameInfo, port?: Chat.Port):
return check;
};

const actionTypes = new Set(['messages', 'bonk', 'delete', 'pin', 'unpin', 'playerProgress', 'forceUpdate']);
const actionTypes = new Set(['messages', 'bonk', 'delete', 'pin', 'unpin', 'summary', 'playerProgress', 'forceUpdate']);
export const responseIsAction = (r: Chat.BackgroundResponse): r is Chat.Actions =>
actionTypes.has(r.type);

Expand Down
15 changes: 12 additions & 3 deletions src/ts/typings/ytc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ declare namespace Ytc {
text: RunsObj;
};
};
/** Gets used for pinned messages */
bannerProperties?: BannerPropertiesObj;
};
};
bannerProperties?: {
isEphemeral: boolean;
bannerTimeoutMs: number;
/** Gets used for chat summary/redirects */
bannerProperties?: BannerPropertiesObj;
}

interface BannerPropertiesObj {
isEphemeral?: boolean;
bannerTimeoutMs?: number;
autoCollapseDelay?: {
seconds: number;
}
}

Expand Down Expand Up @@ -378,6 +386,7 @@ declare namespace Ytc {
header: ParsedRun[];
contents: ParsedMessage;
};
showtime: number;
}

interface ParsedSummary {
Expand Down