-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add pinning functionality for streamers in a channel section #97
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
Open
maksPodstawski
wants to merge
1
commit into
master
Choose a base branch
from
feat/pin-streamer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,6 @@ import type { QuickAccessLink } from "$types/shared/components/settings.componen | |||||||||
| import type { TwitchModuleConfig } from "$types/shared/module/module.types.ts"; | ||||||||||
| import { type Signal, signal } from "@preact/signals"; | ||||||||||
| import { render } from "preact"; | ||||||||||
| import styled from "styled-components"; | ||||||||||
| import TwitchModule from "../../twitch.module.ts"; | ||||||||||
|
|
||||||||||
| export default class ChannelSectionModule extends TwitchModule { | ||||||||||
|
|
@@ -12,6 +11,9 @@ export default class ChannelSectionModule extends TwitchModule { | |||||||||
| private currentDisplayName = signal(""); | ||||||||||
| private currentLogin = signal(""); | ||||||||||
| private watchtimeInterval: NodeJS.Timeout | undefined; | ||||||||||
| private pinnedStreamers: string[] = []; | ||||||||||
| private channelId: string | undefined; | ||||||||||
| private isPinned: Signal<boolean> | undefined; | ||||||||||
|
|
||||||||||
| readonly config: TwitchModuleConfig = { | ||||||||||
| name: "channel-info", | ||||||||||
|
|
@@ -31,50 +33,82 @@ export default class ChannelSectionModule extends TwitchModule { | |||||||||
| this.quickAccessLinks.value = quickAccessLinks; | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| type: "event", | ||||||||||
| key: "pinned-streamers-updated", | ||||||||||
| event: "twitch:pinnedStreamersUpdated", | ||||||||||
| callback: (pinned: string[]) => { | ||||||||||
| this.pinnedStreamers = [...pinned]; | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| ], | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| async initialize() { | ||||||||||
| const quickAccessLinks = await this.settingsService().getSettingsKey("quickAccessLinks"); | ||||||||||
| this.quickAccessLinks = signal(quickAccessLinks); | ||||||||||
| this.pinnedStreamers.push(...(await this.settingsService().getSettingsKey("pinnedStreamers"))); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private async run(elements: Element[]) { | ||||||||||
| const wrappers = this.commonUtils().createEmptyElements(this.getId(), elements, "div"); | ||||||||||
| for (const wrapper of wrappers) { | ||||||||||
| if (this.updateNames()) continue; | ||||||||||
| if (await this.updateNames()) continue; | ||||||||||
| await this.startWatchtimeUpdates(); | ||||||||||
| const logo = await this.commonUtils().getAssetFile( | ||||||||||
| this.workerService(), | ||||||||||
| "enhancer/logo.svg", | ||||||||||
| "https://enhancer.at/assets/brand/logo.png", | ||||||||||
| ); | ||||||||||
| const pinnedEnabled = await this.settingsService().getSettingsKey("pinnedStreamersEnabled"); | ||||||||||
| this.channelId = await this.getChannelId(); | ||||||||||
| this.isPinned = signal(!!(pinnedEnabled && this.channelId && this.isPinnedStreamer(this.channelId))); | ||||||||||
| render( | ||||||||||
| <ChannelSectionComponent | ||||||||||
| displayName={this.currentDisplayName} | ||||||||||
| login={this.currentLogin} | ||||||||||
| sites={this.quickAccessLinks} | ||||||||||
| watchTime={this.watchtimeCounter} | ||||||||||
| logoUrl={logo} | ||||||||||
| isPinned={pinnedEnabled && this.channelId ? this.isPinned : undefined} | ||||||||||
| onTogglePin={ | ||||||||||
| pinnedEnabled && this.channelId | ||||||||||
| ? async () => { | ||||||||||
| const channelId = this.channelId; | ||||||||||
| const isPinned = this.isPinned; | ||||||||||
| if (!channelId || !isPinned) return; | ||||||||||
| isPinned.value = await this.togglePinnedStreamer(channelId); | ||||||||||
|
Comment on lines
+79
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Possible Bug
Suggested change
|
||||||||||
| } | ||||||||||
| : undefined | ||||||||||
| } | ||||||||||
| />, | ||||||||||
| wrapper, | ||||||||||
| ); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private updateNames() { | ||||||||||
| private async updateNames() { | ||||||||||
| const channelInfo = this.twitchUtils().getChannelInfo() || this.twitchUtils().getChannelInfoFromHomeLowerContent(); | ||||||||||
| if (!channelInfo) { | ||||||||||
| this.logger.warn("Channel name not found"); | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
| this.currentDisplayName.value = channelInfo.displayName; | ||||||||||
| this.currentLogin.value = channelInfo.channelLogin; | ||||||||||
| try { | ||||||||||
| this.channelId = this.twitchUtils().getChannelId(); | ||||||||||
| if (this.isPinned) { | ||||||||||
| const pinnedEnabled = await this.settingsService().getSettingsKey("pinnedStreamersEnabled"); | ||||||||||
| this.isPinned.value = !!(pinnedEnabled && this.channelId && this.isPinnedStreamer(this.channelId)); | ||||||||||
| } | ||||||||||
| } catch { | ||||||||||
| this.logger.error("Failed to get channel ID"); | ||||||||||
| } | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private async updateWatchtime() { | ||||||||||
| if (this.updateNames()) return; | ||||||||||
| if (await this.updateNames()) return; | ||||||||||
| if (this.currentLogin.value.length < 1) return; | ||||||||||
| try { | ||||||||||
| this.watchtimeCounter.value = await this.getWatchTime(this.currentLogin.value); | ||||||||||
|
|
@@ -103,4 +137,43 @@ export default class ChannelSectionModule extends TwitchModule { | |||||||||
| }); | ||||||||||
| return watchtime?.time ?? 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private async getChannelId(): Promise<string | undefined> { | ||||||||||
| let resolvedId: string | undefined; | ||||||||||
| await this.commonUtils().waitFor( | ||||||||||
| () => { | ||||||||||
| try { | ||||||||||
| const direct = this.twitchUtils().getChannelId(); | ||||||||||
| if (direct) return direct; | ||||||||||
| const alt = this.twitchUtils().getChannelInfoFromHomeLowerContent(); | ||||||||||
| return alt?.channelId; | ||||||||||
| } catch { | ||||||||||
| return undefined; | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| async (id) => { | ||||||||||
| resolvedId = id; | ||||||||||
| return true; | ||||||||||
| }, | ||||||||||
| { maxRetries: 50, delay: 100 }, | ||||||||||
| ); | ||||||||||
| return resolvedId; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private isPinnedStreamer(channelId: string): boolean { | ||||||||||
| return this.pinnedStreamers.includes(channelId); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private async togglePinnedStreamer(channelId: string): Promise<boolean> { | ||||||||||
| const isPinned = this.isPinnedStreamer(channelId); | ||||||||||
| if (isPinned) { | ||||||||||
| this.pinnedStreamers = this.pinnedStreamers.filter((id) => id !== channelId); | ||||||||||
| } else { | ||||||||||
| this.pinnedStreamers.push(channelId); | ||||||||||
| } | ||||||||||
| await this.settingsService().updateSettingsKey("pinnedStreamers", this.pinnedStreamers); | ||||||||||
| this.twitchUtils().getPersonalSections()?.forceUpdate(); | ||||||||||
| this.emitter.emit("twitch:pinnedStreamersUpdated", this.pinnedStreamers); | ||||||||||
| return !isPinned; | ||||||||||
| } | ||||||||||
| } | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Possible Bug
The
this.channelIdis assigned twice - once inupdateNames()and again in therun()method. SinceupdateNames()is now async, this could lead to a race condition where the channel ID gets overwritten with a different value before the pin toggle handler executes.