-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sidebar-tooltip stream previews (#367)
- Loading branch information
Showing
7 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
112 changes: 112 additions & 0 deletions
112
src/site/twitch.tv/modules/sidebar-previews/SidebarCard.vue
This file contains 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<template /> | ||
|
||
<script setup lang="ts"> | ||
import { onUnmounted, ref, toRaw, watch } from "vue"; | ||
import { REACT_TYPEOF_TOKEN } from "@/common/Constant"; | ||
import { HookedInstance } from "@/common/ReactHooks"; | ||
import { defineFunctionHook, definePropertyHook, unsetPropertyHook } from "@/common/Reflection"; | ||
import { useConfig } from "@/composable/useSettings"; | ||
const props = defineProps<{ | ||
instance: HookedInstance<Twitch.SidebarCardComponent>; | ||
}>(); | ||
const showPreviews = useConfig<boolean>("ui.sidebar_previews"); | ||
const tooltipContent = ref<ReactExtended.ReactRuntimeElement>(); | ||
definePropertyHook(props.instance.component, "props", { | ||
value: (v: Twitch.SidebarCardComponent["props"]) => { | ||
definePropertyHook(v, "tooltipContent", { | ||
value: (v) => { | ||
if (typeof v == "object") { | ||
tooltipContent.value = v; | ||
} | ||
}, | ||
}); | ||
}, | ||
}); | ||
watch( | ||
tooltipContent, | ||
(tooltip, old) => { | ||
if (tooltip != old) { | ||
if (old && typeof old.type == "function") unsetPropertyHook(old.type.prototype, "render"); | ||
if (tooltip && typeof tooltip.type == "function") { | ||
defineFunctionHook(tooltip.type.prototype, "render", function (old, ...args: unknown[]) { | ||
const vnode = old?.apply(this, args); | ||
return patchTooltip(this, vnode); | ||
}); | ||
rerenderCard(); | ||
} | ||
} | ||
}, | ||
{ immediate: true }, | ||
); | ||
watch(showPreviews, () => rerenderCard()); | ||
function rerenderCard() { | ||
toRaw(props.instance.component).forceUpdate(); | ||
} | ||
function patchTooltip(tooltip: ReactExtended.ReactRuntimeElement, vnode: ReactExtended.ReactRuntimeElement) { | ||
if (!showPreviews.value) return vnode; | ||
const body = vnode?.props?.children; | ||
if (!body || !body.props?.children) return vnode; | ||
body.props.style = { width: "20rem" }; | ||
body.props.children.splice(2, 0, { | ||
[REACT_TYPEOF_TOKEN]: Symbol.for("react.element"), | ||
ref: null, | ||
key: null, | ||
type: "div", | ||
props: { | ||
className: "seventv-sidebar-tooltip-preview", | ||
style: { | ||
backgroundImage: getThumbnail(tooltip.props.channelDisplayName.toLowerCase()), | ||
}, | ||
}, | ||
}); | ||
return vnode; | ||
} | ||
function getThumbnail(channel: string) { | ||
let url = `https://static-cdn.jtvnw.net/previews-ttv/live_user_${channel}-190x107.jpg`; | ||
url += `?${Math.floor(Date.now() / 300000)}`; | ||
return `url("${url}")`; | ||
} | ||
onUnmounted(() => { | ||
unsetPropertyHook(props.instance.component, "props"); | ||
if (props.instance.component?.props) { | ||
unsetPropertyHook(props.instance.component.props, "tooltipContent"); | ||
} | ||
if (typeof tooltipContent.value?.type == "function") { | ||
unsetPropertyHook(tooltipContent.value.type.prototype, "type"); | ||
} | ||
}); | ||
</script> | ||
|
||
<style lang="scss"> | ||
.seventv-sidebar-tooltip-preview { | ||
margin: 2px 0; | ||
border-radius: 4px; | ||
width: 100%; | ||
padding-bottom: 56.25%; | ||
background-color: var(--color-background-placeholder); | ||
background-size: contain; | ||
background-position: center; | ||
} | ||
</style> |
35 changes: 35 additions & 0 deletions
35
src/site/twitch.tv/modules/sidebar-previews/SidebarPreviewsModule.vue
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<template> | ||
<template v-for="inst in sidebarCard.instances" :key="inst.identifier"> | ||
<SidebarCard :instance="inst" /> | ||
</template> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { declareModule } from "@/composable/useModule"; | ||
import { declareConfig } from "@/composable/useSettings"; | ||
import { useComponentHook } from "@/common/ReactHooks"; | ||
import SidebarCard from "./SidebarCard.vue"; | ||
const { markAsReady } = declareModule("sidebar-previews", { | ||
name: "Sidebar Previews", | ||
depends_on: ["settings"], | ||
}); | ||
const sidebarCard = useComponentHook<Twitch.SidebarCardComponent>({ | ||
parentSelector: ".side-nav-section", | ||
predicate: (n) => n.props && n.props.title && n.props.isWatchParty !== null && n.props.linkTo, | ||
}); | ||
markAsReady(); | ||
</script> | ||
|
||
<script lang="ts"> | ||
export const config = [ | ||
declareConfig("ui.sidebar_previews", "TOGGLE", { | ||
path: ["Appearance", "Interface"], | ||
label: "Sidebar Stream Thumbnails", | ||
hint: "Show stream thumbnails when hovering over streams on the sidebar.", | ||
defaultValue: false, | ||
}), | ||
]; | ||
</script> |
This file contains 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 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 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