Skip to content

Commit

Permalink
back to last ep viewing if not exists ep in path
Browse files Browse the repository at this point in the history
  • Loading branch information
tachibana-shin committed Apr 20, 2023
1 parent 8d66e3c commit 6f8c194
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const DELAY_SAVE_VIEWING_PROGRESS = 20_000 // x4 6s
export const CONFIRMATION_TIME_IS_ACTUALLY_WATCHING =
DELAY_SAVE_VIEWING_PROGRESS / 4

export const TIMEOUT_GET_LAST_EP_VIEWING_IN_STORE = 5_000 // 5s

export const REGEXP_OLD_HOST_CURL = /animevietsub\.(?:\w+)/i

export const HOST_CURL = [
Expand Down
92 changes: 86 additions & 6 deletions src/pages/phim/_season.vue
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
<script lang="ts" setup>
import { getAnalytics, logEvent } from "@firebase/analytics"
import { Icon } from "@iconify/vue"
import { computedAsync } from "@vueuse/core"
import { useHead } from "@vueuse/head"
import AddToPlaylist from "components/AddToPlaylist.vue"
import BrtPlayer from "components/BrtPlayer.vue"
Expand Down Expand Up @@ -433,7 +434,11 @@ import { PhimId } from "src/apis/runs/phim/[id]"
import { PhimIdChap } from "src/apis/runs/phim/[id]/[chap]"
// import BottomSheet from "src/components/BottomSheet.vue"
import type { Source } from "src/components/sources"
import { C_URL, labelToQuality } from "src/constants"
import {
C_URL,
labelToQuality,
TIMEOUT_GET_LAST_EP_VIEWING_IN_STORE,
} from "src/constants"
import { forceHttp2 } from "src/logic/forceHttp2"
import { formatView } from "src/logic/formatView"
import { getRealSeasonId } from "src/logic/getRealSeasonId"
Expand All @@ -445,7 +450,15 @@ import { useHistoryStore } from "stores/history"
import { usePlaylistStore } from "stores/playlist"
import { useSettingsStore } from "stores/settings"
import type { Ref } from "vue"
import { computed, reactive, ref, shallowRef, watch, watchEffect } from "vue"
import {
computed,
onBeforeUnmount,
reactive,
ref,
shallowRef,
watch,
watchEffect,
} from "vue"
import { useI18n } from "vue-i18n"
import { useRequest } from "vue-request"
import { RouterLink, useRoute, useRouter } from "vue-router"
Expand Down Expand Up @@ -512,6 +525,7 @@ const { data, run, error, loading } = useRequest(
.catch(() => {})
}),
]).catch((err) => {
error.value = err
console.error(err)
// eslint-disable-next-line promise/no-return-wrap
return Promise.reject(err)
Expand Down Expand Up @@ -752,21 +766,87 @@ const currentProgresWatch = computed(() => {
return undefined
})
const currentChap = computed(() => {
// eslint-disable-next-line functional/no-let, no-undef
let timeoutResolveCurrentChap: NodeJS.Timeout | number | undefined
// eslint-disable-next-line functional/no-let
let resolveDefaultCurrentChap: ((epId: string | null) => void) | undefined
const resetPromiseDefCurrentChap = () => {
clearTimeout(timeoutResolveCurrentChap)
timeoutResolveCurrentChap = undefined
resolveDefaultCurrentChap?.(null)
resolveDefaultCurrentChap = undefined
}
onBeforeUnmount(resetPromiseDefCurrentChap)
/** @type - currentChap is episode id */
const currentChap = computedAsync(async () => {
resetPromiseDefCurrentChap()
if (route.params.chap) return route.params.chap as string
// if this does not exist make sure the status has not finished loading, this function call also useless
// if not login -> return first episode in season
if (!authStore.uid) return currentDataSeason.value?.chaps[0].id
const episodeId = await Promise.race([
// if logged -> get last episode viewing in season
historyStore
.getLastEpOfSeason(currentSeason.value)
.catch((err) => {
console.warn(err)
return null
})
.then((res) => {
console.log("usage last ep of season", res)
return res
}),
new Promise<string | null>((resolve) => {
resolveDefaultCurrentChap = resolve
timeoutResolveCurrentChap = setTimeout(
() => resolve(currentDataSeason.value?.chaps[0].id ?? null),
TIMEOUT_GET_LAST_EP_VIEWING_IN_STORE
)
}),
])
// get first chap in season
resetPromiseDefCurrentChap()
// if not exists -> return first episode in season
if (episodeId === null) {
return currentDataSeason.value?.chaps[0].id
}
return currentDataSeason.value?.chaps[0].id
return episodeId
})
const currentMetaChap = computed(() => {
if (!currentChap.value) return
return currentDataSeason.value?.chaps.find(
(item) => item.id === currentChap.value
)
})
// replace router if last episode viewing exists
watchEffect(() => {
const episodeIdFirst = currentDataSeason.value?.chaps[0].id
if (
currentChap.value &&
currentChap.value !== episodeIdFirst &&
currentMetaChap.value
) {
const correctChapName = parseChapName(currentMetaChap.value.name)
router.replace({
path: `/phim/${route.params.season}/${correctChapName}-${currentChap.value}`,
query: route.query,
hash: route.hash,
})
}
})
watchEffect(() => {
if (currentDataSeason.value && currentChap.value !== undefined && !currentMetaChap.value) {
// currentChap != undefined because is load done from firestore and ready show but in chaps not found (!currentMetaChap.value)
if (
currentDataSeason.value &&
currentChap.value !== undefined &&
!currentMetaChap.value
) {
router.replace({
name: "not_found",
params: {
Expand Down
25 changes: 25 additions & 0 deletions src/stores/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const useHistoryStore = defineStore("history", () => {
seasonName: string

last?: {
/** @type : is a id chap. (e.g: 1132, 12345) */
chap: string
cur: number
dur: number
Expand Down Expand Up @@ -313,6 +314,28 @@ export const useHistoryStore = defineStore("history", () => {
])
}

async function getLastEpOfSeason(season: string): Promise<null | string> {
if (!authStore.uid)
// eslint-disable-next-line functional/no-throw-statement
throw new Error(
i18n.global.t("errors.require_login_to", [
i18n.global.t("xem-lich-su-gan-day"),
])
)

const data = await getDoc(
doc(
db,
"users",
authStore.uid,
"history",
getRealSeasonId(season)
) as DocumentReference<HistoryItem>
).then((res) => res.data())

return data?.last?.chap ?? null
}

return {
last30Item,
last30ItemError,
Expand All @@ -324,5 +347,7 @@ export const useHistoryStore = defineStore("history", () => {
getProgressChaps,
getProgressChap,
setProgressChap,

getLastEpOfSeason,
}
})

0 comments on commit 6f8c194

Please sign in to comment.