Skip to content

Commit

Permalink
Add auto-collapse capability to pinned messages based on timeout prov…
Browse files Browse the repository at this point in the history
…ided by bannerProperties

Also updates the impl in ChatSummary
  • Loading branch information
FlaminSarge committed Dec 23, 2024
1 parent 669a8c7 commit 30409c9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
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
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

0 comments on commit 30409c9

Please sign in to comment.