Skip to content

Commit

Permalink
$ Create component quick bookmark button to avoid duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed May 14, 2024
1 parent 5e924c9 commit 978f30e
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 193 deletions.
64 changes: 4 additions & 60 deletions src/renderer/components/ft-list-video/ft-list-video.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineComponent } from 'vue'
import FtIconButton from '../ft-icon-button/ft-icon-button.vue'
import { mapActions } from 'vuex'
import FtIconButton from '../ft-icon-button/ft-icon-button.vue'
import FtPlaylistQuickBookmarkButton from '../ft-playlist-quick-bookmark-button/ft-playlist-quick-bookmark-button.vue'
import {
copyToClipboard,
deepCopy,
Expand All @@ -17,7 +18,8 @@ import debounce from 'lodash.debounce'
export default defineComponent({
name: 'FtListVideo',
components: {
'ft-icon-button': FtIconButton
'ft-icon-button': FtIconButton,
'ft-playlist-quick-bookmark-button': FtPlaylistQuickBookmarkButton,
},
props: {
data: {
Expand Down Expand Up @@ -105,7 +107,6 @@ export default defineComponent({
isPremium: false,
hideViews: false,
addToPlaylistPromptCloseCallback: null,
selectQuickBookmarkTargetPromptCloseCallback: null,
debounceGetDeArrowThumbnail: null,
}
},
Expand Down Expand Up @@ -432,29 +433,6 @@ export default defineComponent({
isQuickBookmarkEnabled() {
return this.quickBookmarkPlaylist != null
},
isInQuickBookmarkPlaylist: function () {
if (!this.isQuickBookmarkEnabled) { return false }

return this.quickBookmarkPlaylist.videos.some((video) => {
return video.videoId === this.id
})
},
quickBookmarkIconText: function () {
if (!this.isQuickBookmarkEnabled) { return this.$t('User Playlists.Add to Playlist') }

const translationProperties = {
playlistName: this.quickBookmarkPlaylist.playlistName,
}
return this.isInQuickBookmarkPlaylist
? this.$t('User Playlists.Remove from Favorites', translationProperties)
: this.$t('User Playlists.Add to Favorites', translationProperties)
},
quickBookmarkIconTheme: function () {
return this.isInQuickBookmarkPlaylist ? 'base favorite' : 'base'
},
selectQuickBookmarkTargetPromptShown() {
return this.$store.getters.getShowSelectQuickBookmarkTargetPrompt
},

watchPageLinkTo() {
// For `router-link` attribute `to`
Expand Down Expand Up @@ -494,13 +472,6 @@ export default defineComponent({
if (this.addToPlaylistPromptCloseCallback == null) { return }
this.addToPlaylistPromptCloseCallback()
},
selectQuickBookmarkTargetPromptShown(value) {
if (value) { return }
// Execute on prompt close

if (this.selectQuickBookmarkTargetPromptCloseCallback == null) { return }
this.selectQuickBookmarkTargetPromptCloseCallback()
},
},
created: function () {
this.parseVideoData()
Expand Down Expand Up @@ -771,33 +742,6 @@ export default defineComponent({
showToast(this.$t('Channel Unhidden', { channel: channelName }))
},

toggleQuickBookmarked() {
if (!this.isQuickBookmarkEnabled) {
showToast(this.$t('User Playlists["Quick Bookmark Disabled. Pick a Playlist as Quick Bookmark Target"]'))
this.selectQuickBookmarkTargetPromptCloseCallback = () => {
// Run once only
this.selectQuickBookmarkTargetPromptCloseCallback = null

// Auto add this video to quick bookmark target if target set in prompt
if (!this.isQuickBookmarkEnabled) { return }

// Users don't know the video is in target playlist or not
// Assuming they want to add the video
// Add it only if not already present in target playlist
if (!this.isInQuickBookmarkPlaylist) {
this.addToQuickBookmarkPlaylist()
}
}
this.showSelectQuickBookmarkTargetPrompt()
return
}

if (this.isInQuickBookmarkPlaylist) {
this.removeFromQuickBookmarkPlaylist()
} else {
this.addToQuickBookmarkPlaylist()
}
},
addToQuickBookmarkPlaylist() {
const videoData = {
videoId: this.id,
Expand Down
18 changes: 8 additions & 10 deletions src/renderer/components/ft-list-video/ft-list-video.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,17 @@
:size="appearance === `watchPlaylistItem` ? 14 : 18"
@click="togglePlaylistPrompt"
/>
<ft-icon-button
<ft-playlist-quick-bookmark-button
v-if="quickBookmarkButtonEnabled"
:title="quickBookmarkIconText"
:icon="isInQuickBookmarkPlaylist ? ['fas', 'check'] : ['fas', 'bookmark']"
class="quickBookmarkVideoIcon"
:class="{
bookmarked: isInQuickBookmarkPlaylist,
alwaysVisible: alwaysShowAddToPlaylistButton,
}"
:theme="quickBookmarkIconTheme"
:id="id"
:title="title"
:channel-id="channelId"
:channel-name="channelName"
:view-count="viewCount"
:length-seconds="lengthSeconds"
:padding="appearance === `watchPlaylistItem` ? 5 : 6"
:size="appearance === `watchPlaylistItem` ? 14 : 18"
@click="toggleQuickBookmarked"
:always-visible="alwaysShowAddToPlaylistButton"
/>
<ft-icon-button
v-if="inUserPlaylist && canMoveVideoUp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { defineComponent } from 'vue'
import { mapActions } from 'vuex'
import FtIconButton from '../ft-icon-button/ft-icon-button.vue'
import { showToast } from '../../helpers/utils'

export default defineComponent({
name: 'FtPlaylistQuickBookmarkButton',
components: {
'ft-icon-button': FtIconButton,
},
props: {
id: {
type: String,
required: true
},
title: {
type: String,
required: true
},
channelId: {
type: String,
required: true
},
channelName: {
type: String,
required: true
},
viewCount: {
type: Number,
required: true
},
lengthSeconds: {
type: Number,
required: true
},
padding: {
type: Number,
default: 10
},
size: {
type: Number,
default: 20
},
alwaysVisible: {
type: Boolean,
default: false
},
},
data() {
return {
selectQuickBookmarkTargetPromptCloseCallback: null,
}
},
computed: {
quickBookmarkPlaylistId() {
return this.$store.getters.getQuickBookmarkTargetPlaylistId
},
quickBookmarkPlaylist() {
return this.$store.getters.getPlaylist(this.quickBookmarkPlaylistId)
},
isQuickBookmarkEnabled() {
return this.quickBookmarkPlaylist != null
},
isInQuickBookmarkPlaylist: function () {
if (!this.isQuickBookmarkEnabled) { return false }

return this.quickBookmarkPlaylist.videos.some((video) => {
return video.videoId === this.id
})
},
quickBookmarkIconText: function () {
if (!this.isQuickBookmarkEnabled) { return this.$t('User Playlists.Add to Playlist') }

const translationProperties = {
playlistName: this.quickBookmarkPlaylist.playlistName,
}
return this.isInQuickBookmarkPlaylist
? this.$t('User Playlists.Remove from Favorites', translationProperties)
: this.$t('User Playlists.Add to Favorites', translationProperties)
},
quickBookmarkIconTheme: function () {
return this.isInQuickBookmarkPlaylist ? 'base favorite' : 'base'
},
selectQuickBookmarkTargetPromptShown() {
return this.$store.getters.getShowSelectQuickBookmarkTargetPrompt
},
},
watch: {
selectQuickBookmarkTargetPromptShown(value) {
if (value) { return }
// Execute on prompt close

if (this.selectQuickBookmarkTargetPromptCloseCallback == null) { return }
this.selectQuickBookmarkTargetPromptCloseCallback()
},
},
methods: {
toggleQuickBookmarked() {
if (!this.isQuickBookmarkEnabled) {
showToast(this.$t('User Playlists["Quick Bookmark Disabled. Pick a Playlist as Quick Bookmark Target"]'))
this.selectQuickBookmarkTargetPromptCloseCallback = () => {
// Run once only
this.selectQuickBookmarkTargetPromptCloseCallback = null

// Auto add this video to quick bookmark target if target set in prompt
if (!this.isQuickBookmarkEnabled) { return }

// Users don't know the video is in target playlist or not
// Assuming they want to add the video
// Add it only if not already present in target playlist
if (!this.isInQuickBookmarkPlaylist) {
this.addToQuickBookmarkPlaylist()
}
}
this.showSelectQuickBookmarkTargetPrompt()
return
}

if (this.isInQuickBookmarkPlaylist) {
this.removeFromQuickBookmarkPlaylist()
} else {
this.addToQuickBookmarkPlaylist()
}
},
addToQuickBookmarkPlaylist() {
const videoData = {
videoId: this.id,
title: this.title,
author: this.channelName,
authorId: this.channelId,
viewCount: this.viewCount,
lengthSeconds: this.lengthSeconds,
}

this.addVideos({
_id: this.quickBookmarkPlaylist._id,
videos: [videoData],
})
// Update playlist's `lastUpdatedAt`
this.updatePlaylist({ _id: this.quickBookmarkPlaylist._id })

// TODO: Maybe show playlist name
showToast(this.$t('Video.Video has been saved'))
},
removeFromQuickBookmarkPlaylist() {
this.removeVideo({
_id: this.quickBookmarkPlaylist._id,
// Remove all playlist items with same videoId
videoId: this.id,
})
// Update playlist's `lastUpdatedAt`
this.updatePlaylist({ _id: this.quickBookmarkPlaylist._id })

// TODO: Maybe show playlist name
showToast(this.$t('Video.Video has been removed from your saved list'))
},

...mapActions([
'showSelectQuickBookmarkTargetPrompt',
'addVideos',
'updatePlaylist',
'removeVideo',
])
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<ft-icon-button
:title="quickBookmarkIconText"
:icon="isInQuickBookmarkPlaylist ? ['fas', 'check'] : ['fas', 'bookmark']"
class="quickBookmarkVideoIcon"
:class="{
bookmarked: isInQuickBookmarkPlaylist,
alwaysVisible: alwaysVisible,
}"
:theme="quickBookmarkIconTheme"
:padding="padding"
:size="size"
@click="toggleQuickBookmarked"
/>
</template>

<script src="./ft-playlist-quick-bookmark-button.js" />

0 comments on commit 978f30e

Please sign in to comment.