Skip to content

Commit

Permalink
feat: unify getFollowedStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed May 14, 2024
1 parent dffb62c commit eaf920f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
17 changes: 8 additions & 9 deletions src/components/VideoCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APP_KEY_PREFIX, APP_NAME, baseDebug } from '$common'
import { APP_KEY_PREFIX, APP_NAME } from '$common'
import { flexVerticalCenterStyle } from '$common/emotion-css'
import { useMittOn } from '$common/hooks/useMitt'
import { useRefStateBox } from '$common/hooks/useRefState'
Expand Down Expand Up @@ -33,6 +33,7 @@ import { BlacklistCard, DislikedCard, SkeletonCard } from './child-components/ot
import styles from './index.module.scss'
import type { VideoCardEmitter } from './index.shared'
import { borderRadiusStyle, defaultEmitter } from './index.shared'
import { getFollowedStatus } from './process/filter'
import type { IVideoCardData } from './process/normalize'
import { normalizeCardData } from './process/normalize'
import { AppRecIconScaleMap, AppRecIconSvgNameMap, makeStatItem } from './stat-item'
Expand All @@ -41,8 +42,6 @@ import { useOpenRelated } from './use/useOpenRelated'
import { usePreviewAnimation } from './use/usePreviewAnimation'
import { useWatchlaterRelated } from './use/useWatchlaterRelated'

const debug = baseDebug.extend('components:VideoCard')

function copyContent(content: string) {
GM.setClipboard(content)
AntdMessage.success(`已复制: ${content}`)
Expand Down Expand Up @@ -77,7 +76,7 @@ export const VideoCard = memo(function VideoCard({
// false when item provided
loading = loading ?? !item

const dislikedReason = useDislikedReason(item?.api === 'app' && item.param)
const dislikedReason = useDislikedReason(item?.api === EApiType.App && item.param)
const cardData = useMemo(() => item && normalizeCardData(item), [item])
const blacklisted = useInBlacklist(cardData?.authorMid)

Expand Down Expand Up @@ -315,8 +314,8 @@ const VideoCardInner = memo(function VideoCardInner({
*/

const hasUnfollowEntry =
item.api === 'dynamic' ||
((item.api === 'app' || item.api === 'pc') && recommendReason === '已关注')
item.api === EApiType.Dynamic ||
((item.api === EApiType.App || item.api === EApiType.Pc) && getFollowedStatus(recommendReason))
const onUnfollowUp = useMemoizedFn(async () => {
if (!authorMid) return
const success = await UserfollowService.unfollow(authorMid)
Expand Down Expand Up @@ -402,7 +401,7 @@ const VideoCardInner = memo(function VideoCardInner({
icon: <IconPark name='PeopleDelete' size={15} />,
onClick: onBlacklistUp,
},
item.api === 'watchlater' && {
item.api === EApiType.Watchlater && {
key: 'add-fav',
icon: (
<IconPark
Expand Down Expand Up @@ -446,7 +445,7 @@ const VideoCardInner = memo(function VideoCardInner({
onToggleWatchLater()
},
},
item.api === 'watchlater' &&
item.api === EApiType.Watchlater &&
watchLaterAdded && {
key: 'watchlater-readd',
label: '重新添加稍候再看 (移到最前)',
Expand All @@ -458,7 +457,7 @@ const VideoCardInner = memo(function VideoCardInner({
},
},

...(item.api === 'fav'
...(item.api === EApiType.Fav
? [
{ type: 'divider' as const },
{
Expand Down
12 changes: 8 additions & 4 deletions src/components/VideoCard/process/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { normalizeCardData } from './normalize'

const debug = baseDebug.extend('VideoCard:filter')

export function getFollowedStatus(recommendReason?: string): boolean {
return !!recommendReason && ['已关注', '新关注'].includes(recommendReason)
}

export function anyFilterEnabled(tab: ETabType) {
return (
tab === 'keep-follow-only' ||
Expand All @@ -34,13 +38,13 @@ export function filterRecItems(items: RecItemExtraType[], tab: ETabType) {

const { play, duration, recommendReason, goto, authorName, authorMid, title, bvid, href } =
normalizeCardData(item)
const isFollowed = recommendReason === '已关注' || !!recommendReason?.endsWith('关注')
const followed = getFollowedStatus(recommendReason)

/**
* 已关注 Tab
*/
if (tab === 'keep-follow-only') {
if (!isFollowed) return false
if (!followed) return false
}

const isVideo = goto === 'av'
Expand Down Expand Up @@ -105,7 +109,7 @@ export function filterRecItems(items: RecItemExtraType[], tab: ETabType) {

function filterVideo() {
// 不过滤已关注视频
if (isFollowed && !settings.enableFilterForFollowedVideo) return true
if (followed && !settings.enableFilterForFollowedVideo) return true

// https://github.com/magicdawn/bilibili-app-recommend/issues/87
// 反向推送, 蜜汁操作.
Expand Down Expand Up @@ -151,7 +155,7 @@ export function filterRecItems(items: RecItemExtraType[], tab: ETabType) {
function filterPicture() {
if (settings.filterOutGotoTypePicture) {
// 不去掉已关注的图文
if (isFollowed && !settings.enableFilterForFollowedPicture) {
if (followed && !settings.enableFilterForFollowedPicture) {
return true
}
debug('filter out by goto-type-picture-rule: %s %o', goto, {
Expand Down
2 changes: 1 addition & 1 deletion src/components/VideoCard/use/useOpenRelated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function useOpenRelated({

function openInIINA() {
let usingHref = href
if (item.api === 'watchlater') usingHref = `/video/${item.bvid}`
if (item.api === EApiType.Watchlater) usingHref = `/video/${item.bvid}`
const fullHref = new URL(usingHref, location.href).href
const iinaUrl = `iina://open?url=${encodeURIComponent(fullHref)}`
window.open(iinaUrl, '_self')
Expand Down
6 changes: 4 additions & 2 deletions src/components/VideoCard/use/useWatchlaterRelated.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type RecItemType } from '$define'
import { EApiType } from '$define/index.shared'
import { useWatchLaterState, watchLaterState } from '$modules/recommend/watchlater'
import { AntdMessage } from '$utility'
import { usePrevious } from 'ahooks'
Expand All @@ -25,7 +26,8 @@ export function useWatchlaterRelated({
actionButtonVisible: boolean
}) {
const { avid, bvid } = cardData
const hasWatchLaterEntry = item.api !== 'app' || (item.api === 'app' && item.goto === 'av')
const hasWatchLaterEntry =
(item.api === EApiType.App && item.goto === 'av') || item.api !== EApiType.App

// watchLater added
const watchLaterAdded = useWatchLaterState(bvid)
Expand Down Expand Up @@ -64,7 +66,7 @@ export function useWatchlaterRelated({
}

// 稍后再看
if (item.api === 'watchlater') {
if (item.api === EApiType.Watchlater) {
// when remove-watchlater for watchlater tab, remove this card
if (!targetState) {
await delay(100)
Expand Down

0 comments on commit eaf920f

Please sign in to comment.