Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): show random featured projects on home screen #2159

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions apps/app-frontend/src/pages/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { profile_listener } from '@/helpers/events'
import { useBreadcrumbs } from '@/store/breadcrumbs'
import { handleError } from '@/store/notifications.js'
import dayjs from 'dayjs'
import { get_search_results } from '@/helpers/cache.js'
import { useFeaturedProjects } from '@/store/featuredProjects.js'

const featuredModpacks = ref({})
const featuredMods = ref({})
Expand Down Expand Up @@ -51,36 +51,26 @@ const getInstances = async () => {
filter.value = filters.join(' AND ')
}

const getFeaturedModpacks = async () => {
const response = await get_search_results(
`?facets=[["project_type:modpack"]]&limit=10&index=follows&filters=${filter.value}`,
)
await getInstances()

if (response) {
featuredModpacks.value = response.result.hits
} else {
featuredModpacks.value = []
}
}
const getFeaturedMods = async () => {
const response = await get_search_results('?facets=[["project_type:mod"]]&limit=10&index=follows')
let featuredProjects = useFeaturedProjects()

if (response) {
featuredMods.value = response.result.hits
} else {
featuredModpacks.value = []
}
async function updateFeaturedProjects() {
let [modpacks, mods] = await Promise.all([
await featuredProjects.getFeaturedModpack(filter),
await featuredProjects.getFeaturedMods(),
])
featuredModpacks.value = modpacks
featuredMods.value = mods
}

await getInstances()

await Promise.all([getFeaturedModpacks(), getFeaturedMods()])
await updateFeaturedProjects()

const unlistenProfile = await profile_listener(async (e) => {
await getInstances()

if (e.event === 'added' || e.event === 'created' || e.event === 'removed') {
await Promise.all([getFeaturedModpacks(), getFeaturedMods()])
await updateFeaturedProjects()
}
})

Expand Down
49 changes: 49 additions & 0 deletions apps/app-frontend/src/store/featuredProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { defineStore } from 'pinia'
import { get_search_results } from '@/helpers/cache.js'

export const useFeaturedProjects = defineStore('featuredProjects', {
state: () => ({
modpacks: undefined,
mods: undefined,
}),
actions: {
async getFeaturedModpack(filter) {
if (this.modpacks !== undefined && this.modpacks.length !== 0) {
ToBinio marked this conversation as resolved.
Show resolved Hide resolved
return this.modpacks
}

let offset = Math.floor(Math.random() * 100)
ToBinio marked this conversation as resolved.
Show resolved Hide resolved

const response = await get_search_results(
`?facets=[["project_type:modpack"]]&limit=10&filters=${filter.value}&offset=${offset}`,
)

if (response) {
this.modpacks = response.result.hits
} else {
this.modpacks = []
}

return this.modpacks
},
async getFeaturedMods() {
if (this.mods !== undefined && this.mods.length !== 0) {
return this.mods
}

let offset = Math.floor(Math.random() * 100)

const response = await get_search_results(
`?facets=[["project_type:mod"]]&limit=10&offset=${offset}`,
)

if (response) {
this.mods = response.result.hits
} else {
this.mods = []
}

return this.mods
},
},
})