From 9f06815febf517327b33fc4e3939a97d82a1f79c Mon Sep 17 00:00:00 2001 From: enpitsulin Date: Sun, 26 Mar 2023 22:13:35 +0800 Subject: [PATCH] feat: implemented nsfw settings --- src/App.vue | 2 +- src/components/DataTable.vue | 19 +++++++++++-------- src/composables/data.ts | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/App.vue b/src/App.vue index cd2f783..0702e47 100644 --- a/src/App.vue +++ b/src/App.vue @@ -19,7 +19,7 @@ ); const [showTable, toggleShowTable] = useToggle(false); - const { isFetching, error, data } = useGreasyfork(); + const data = useDataList(); const pagePsl = computed(() => { return psl.get(window.location.hostname); diff --git a/src/components/DataTable.vue b/src/components/DataTable.vue index 8892632..1932807 100644 --- a/src/components/DataTable.vue +++ b/src/components/DataTable.vue @@ -156,15 +156,18 @@ daily: '' as '' | 'desc' | 'asc' }); - const sortedData = useSorted(props.data, (a, b) => { - if (sort.updated !== '') { - if (sort.updated === 'asc') - return +new Date(a.code_updated_at) - +new Date(b.code_updated_at); - return +new Date(b.code_updated_at) - +new Date(a.code_updated_at); + const sortedData = useSorted( + computed(() => props.data), + (a, b) => { + if (sort.updated !== '') { + if (sort.updated === 'asc') + return +new Date(a.code_updated_at) - +new Date(b.code_updated_at); + return +new Date(b.code_updated_at) - +new Date(a.code_updated_at); + } + if (sort.daily === 'asc') return a.daily_installs - b.daily_installs; + return b.daily_installs - a.daily_installs; } - if (sort.daily === 'asc') return a.daily_installs - b.daily_installs; - return b.daily_installs - a.daily_installs; - }); + ); const sortIcon = (sort: '' | 'desc' | 'asc') => { if (sort === '') return 'i-carbon-caret-sort'; diff --git a/src/composables/data.ts b/src/composables/data.ts index bda001b..735109b 100644 --- a/src/composables/data.ts +++ b/src/composables/data.ts @@ -31,10 +31,41 @@ export type GreasyforkScript = { version: string; }; -export function useGreasyfork(site = 'https://greasyfork.org') { +export function useGreasyfork( + site = 'https://greasyfork.org', + immediate = true +) { const host = psl.get(window.location.hostname); const apiEndpoint = `${site}/en/scripts/by-site/${host}.json`; - return useFetch(apiEndpoint, { fetch: unsafeWindow.fetch }).json< + return useFetch(apiEndpoint, { fetch: unsafeWindow.fetch, immediate }).json< GreasyforkScript[] >(); } + +export function useDataList() { + const settings = useUserjsDiggerSettings(); + const { data: greasyfork } = useGreasyfork(); + const { data: sleazyfork, execute } = useGreasyfork( + 'https://sleazyfork.org', + false + ); + watch( + () => settings.value.nsfw, + (val) => { + if (val) { + if (!sleazyfork.value) execute(); + } + }, + { immediate: true } + ); + + const data = computed(() => { + return ( + greasyfork.value?.concat( + settings.value.nsfw ? sleazyfork.value ?? [] : [] + ) ?? [] + ); + }); + + return data; +}