explorer-app/composables/useData.ts

346 lines
12 KiB
TypeScript
Raw Normal View History

import type { Category, Project, ProjectRating, ProjectRatingItem, ProjectShallow } from '~/types'
import type { Asset } from '~/types/asset'
import type { Ecosystem } from '~/types/ecosystem'
import type { Feature } from '~/types/feature'
import type { Rank } from '~/types/rank'
import type { Usecase } from '~/types/usecase'
2023-12-19 18:43:42 +01:00
export const useData = defineStore('data', () => {
const projectPhase = useState<{ id: string, name: string }[]>('projectPhase')
const assetCustody = useState<{ id: string, name: string }[]>('assetCustody')
const signInRequirments = useState<{ id: string, name: string }[]>('signInRequirmenets')
2024-09-12 18:03:06 +02:00
2023-12-19 18:43:42 +01:00
const categories = useState<Category[]>('categories')
const usecases = useState<Usecase[]>('usecases')
const features = useState<Feature[]>('features')
const assets = useState<Asset[]>('assets')
const ecosystems = useState<Ecosystem[]>('ecosystems')
2023-12-19 18:43:42 +01:00
const projects = useState<Project[]>('projects')
const ranks = useState<Rank[]>('ranks')
const selectedCategoryId = useState(() => 'all')
const selectedUsecaseId = useState(() => 'all')
const selectedEcosystemId = useState(() => 'all')
const selectedAssetsUsedId = useState(() => 'all')
const selectedFeaturesId = useState(() => 'all')
2023-12-19 18:43:42 +01:00
const filter = reactive({
query: '',
sortby: 'score',
sortDirection: 'desc',
2023-12-19 18:43:42 +01:00
})
const switcher = ref(true)
watch([selectedCategoryId, selectedUsecaseId, selectedEcosystemId, selectedAssetsUsedId, selectedFeaturesId], () => {
if (selectedCategoryId.value !== 'all' || selectedUsecaseId.value !== 'all' || selectedEcosystemId.value !== 'all' || selectedAssetsUsedId.value !== 'all' || selectedFeaturesId.value !== 'all')
2023-12-19 18:43:42 +01:00
filter.query = ''
})
watch(filter, () => {
if (filter.query !== '') {
2023-12-19 18:43:42 +01:00
selectedCategoryId.value = 'all'
selectedUsecaseId.value = 'all'
selectedEcosystemId.value = 'all'
selectedAssetsUsedId.value = 'all'
selectedFeaturesId.value = 'all'
}
2023-12-19 18:43:42 +01:00
})
const fetchData = async () => {
try {
const data = await $fetch<{
categories: Category[]
projects: Project[]
2024-09-23 21:14:59 +02:00
phases: { id: string, name: string }[]
custodys: { id: string, name: string }[]
requirements: { id: string, name: string }[]
usecases: Usecase[]
ecosystems: Ecosystem[]
assets: Asset[]
features: Feature[]
ranks: Rank[]
2023-12-19 18:43:42 +01:00
}>('/api/data')
data.projects.forEach(project => project.ratings = generateProjectRating(project))
2024-10-03 16:47:10 +02:00
// Percentage calculation
2024-10-03 16:41:44 +02:00
projects.value = data.projects.map((project) => {
const totalPoints = project.ratings?.reduce((a, b) => a + b.percentagePoints, 0) || 0
2024-10-03 16:47:10 +02:00
const numberOfRatings = project.ratings?.length || 1
2024-10-03 16:41:44 +02:00
const averagePercentage = totalPoints / numberOfRatings
return {
...project,
2024-10-03 16:47:10 +02:00
percentage: Math.min(Math.max(Math.round(averagePercentage), 0), 100),
2024-10-03 16:41:44 +02:00
}
}).filter(p => p.name)
2024-10-03 16:47:10 +02:00
2024-10-02 13:22:02 +02:00
const projectCategories = projects.value.map(p => p.categories).flat()
categories.value = data.categories.filter(c => projectCategories.includes(c.id))
usecases.value = data.usecases
ecosystems.value = data.ecosystems
assets.value = data.assets
features.value = data.features
ranks.value = data.ranks
2024-09-23 21:14:59 +02:00
projectPhase.value = data.phases?.map(p => ({ id: p.id.toLowerCase(), name: p.name }))
assetCustody.value = data.custodys.map(a => ({ id: a.id.toLowerCase(), name: a.name }))
signInRequirments.value = data.requirements.map(s => ({ id: s.id.toLowerCase(), name: s.name }))
2023-12-19 18:43:42 +01:00
}
catch (e) {
console.error(e)
return false
}
return true
}
const projectToShallow = (project: Project): ProjectShallow => {
const availableSupport = () => {
const filteredKeys = ['forum', 'discord', 'twitter', 'lens', 'farcaster', 'telegram']
// if (typeof project.links === 'object' && (project.links !== null || project.links !== undefined))
2024-06-27 13:00:42 +02:00
if (project.links === null || project.links === undefined)
return 0
2023-12-19 18:43:42 +01:00
if (typeof project.links === 'object' && Object.keys(project.links).length > 0)
return Object.keys(project.links).filter(key => filteredKeys.includes(key)).length
2023-12-19 18:43:42 +01:00
}
return {
id: project.id,
title1: project.name,
description: project.description ?? 'N/A',
percentage: project.percentage,
2023-12-19 18:43:42 +01:00
forum: project.links?.forum,
explorer: project.links?.block_explorer,
twitter: project.links?.twitter,
coingecko: project.links?.coingecko,
newsletter: project.links?.rss_feed,
github: project.links?.github,
website: project.links?.web,
readyness: project.project_status?.version,
team: project.team,
docs: project.links?.docs,
audits: project.audits,
support: availableSupport(),
image: project.logos?.[0]?.url ?? '',
anonymity: true,
categories: project.categories,
usecases: project.usecases,
ecosystem: project.ecosystem,
assets_used: project.assets_used,
ratings: project.ratings,
2023-12-19 18:43:42 +01:00
}
}
const shallowProjects = computed(() => projects.value.map(project => projectToShallow(project)))
const getProjectsByFilters = <T extends ProjectShallow>(options?: { shallow: boolean }): T[] => {
const selectedCategory = selectedCategoryId.value !== 'all' ? selectedCategoryId.value : undefined
const selectedUsecase = selectedUsecaseId.value !== 'all' ? selectedUsecaseId.value.toLowerCase() : undefined
const selectedEcosystem = selectedEcosystemId.value !== 'all' ? selectedEcosystemId.value.toLowerCase() : undefined
const selectedAssetsUsed = selectedAssetsUsedId.value !== 'all' ? selectedAssetsUsedId.value.toLowerCase() : undefined
const selectedFeature = selectedFeaturesId.value !== 'all' ? selectedFeaturesId.value.toLowerCase() : undefined
return (projects.value.filter((project) => {
if (selectedCategory && !project.categories.includes(selectedCategory))
return false
if (selectedUsecase) {
if (selectedUsecase === 'sunset') {
if (!project.sunset)
return false
}
else {
const usecases = project.usecases?.map(u => u.toLowerCase()) || []
if (!usecases.includes(selectedUsecase))
return false
}
}
if (selectedEcosystem) {
const ecosystems = project.ecosystem?.map(e => e.toLowerCase()) || []
if (!ecosystems.includes(selectedEcosystem))
return false
}
if (selectedAssetsUsed) {
const assetsUsed = project.assets_used?.map(a => a.toLowerCase()) || []
if (!assetsUsed.includes(selectedAssetsUsed))
return false
}
if (selectedFeature) {
2024-10-02 15:08:47 +02:00
switch (selectedFeature) {
case 'no-compliance':
return project.compliance ? false : true
case 'non-kyc':
return project.tracebility?.kyc ? false : true
case 'private-by-default':
return project.default_privacy
case 'non-custodial':
return project.blockchain_features?.asset_custody_type === 'non-custody'
case 'opensource':
return project.blockchain_features?.opensource
case 'live-on-mainnet':
return project.project_phase === 'mainnet'
}
}
return true
}).map(project => (options?.shallow ? projectToShallow(project) : project)) as T[])
2023-12-19 18:43:42 +01:00
}
const getProjectById = <T extends Project | ProjectShallow>(id: string, options?: { shallow: boolean }): T => {
const project = projects.value.find(project => project.id === id)
return (options?.shallow && project ? projectToShallow(project) : project) as T
}
const filteredProjects = computed(() => {
if (!projects.value) return []
2023-12-19 18:43:42 +01:00
const query = filter.query.toLowerCase()
const sortDirection = filter.sortDirection === 'desc' ? 1 : -1
const sortBy = filter.sortby
2023-12-19 18:43:42 +01:00
const filteredShallowProjects = getProjectsByFilters({ shallow: true })
2023-12-19 18:43:42 +01:00
.filter((project) => {
return project?.title1?.toLowerCase().includes(query)
})
.sort((a, b) => {
if (sortBy === 'score') {
return sortDirection * (b.percentage - a.percentage)
}
if (sortBy === 'title') {
// sortDirection is reversed because the default sort is descending
return sortDirection * a.title1.toLowerCase().localeCompare(b.title1.toLowerCase())
}
if (sortBy === 'openess' || sortBy === 'technology' || sortBy === 'privacy') {
const scoreA = a.ratings?.find(r => r.type === sortBy)?.points || 0
const scoreB = b.ratings?.find(r => r.type === sortBy)?.points || 0
return sortDirection * (scoreB - scoreA)
}
return 0
2023-12-19 18:43:42 +01:00
})
2023-12-19 18:43:42 +01:00
return filteredShallowProjects
})
const groupedProjectsPerCategory = computed(() => {
const groupedProjects = categories.value.map((category) => {
const projectsInCategory = filteredProjects.value.filter(project =>
project.categories.includes(category.id),
)
return {
title: category.name,
projects: projectsInCategory,
}
}).sort((a, b) => b.projects.length - a.projects.length)
2024-09-23 21:14:59 +02:00
return groupedProjects.filter(group => group.projects.length > 0)
})
2023-12-19 18:43:42 +01:00
const filteredProjectsCount = computed(() => filteredProjects.value.length)
const getNestedField = (project: Project, field: string) => {
const fields = field.split('.')
return fields.reduce((acc: any, curr: string) => {
return acc && acc[curr as keyof typeof acc]
}, project)
}
const generateProjectRating = (project: Project) => {
const projectRatings: ProjectRating[] = ranks.value?.map((rank) => {
let rankPoints = 0
let maxPoints = 0
const ratingStats: ProjectRatingItem[] = []
rank.references?.forEach((ref) => {
let isValid = false
const field = ref.field.includes('.') ? getNestedField(project, ref.field) : project[ref.field]
// adds only one valid social link
if (ref.label.positive === 'Link' && ref.label.name !== 'Documentation')
if (ratingStats.some(r => r.positive === 'Link' && r.label !== 'Documentation' && r.value) || !field)
return
let value
let positive
let negative
2024-09-20 12:13:27 +02:00
if (ref.condition.minLength !== undefined) {
value = (field as any[])?.length
if (value) {
isValid = value >= ref.condition.minLength
positive = `${value} ${ref.label.positive}${value > 1 ? 's' : ''}`
}
}
2024-09-20 12:13:27 +02:00
if (ref.condition.equals !== undefined) {
value = field
2024-09-20 12:13:27 +02:00
if (value !== undefined)
isValid = value === ref.condition.equals
}
2024-09-20 12:13:27 +02:00
if (ref.condition.exists !== undefined) {
value = field
if (ref.condition.exists === false)
isValid = !value ? true : false
else
isValid = value ? true : false
}
if (ref.field === 'compliance') {
2024-09-26 12:44:59 +02:00
positive = value
}
rankPoints += isValid ? ref.points : 0
maxPoints += ref.points
ratingStats.push({
isValid,
label: ref.label.name,
positive: positive ? positive : ref.label.positive,
negative: negative ? negative : ref.label.negative,
value,
} as ProjectRatingItem)
})
return {
type: rank.id,
name: rank.name,
items: ratingStats,
points: rankPoints,
percentagePoints: rankPoints / maxPoints * 100,
}
})
return projectRatings
}
2023-12-19 18:43:42 +01:00
return {
selectedCategoryId,
selectedUsecaseId,
selectedEcosystemId,
selectedAssetsUsedId,
selectedFeaturesId,
2023-12-19 18:43:42 +01:00
filter,
switcher,
categories,
projectPhase,
assetCustody,
signInRequirments,
usecases,
features,
ecosystems,
assets,
2023-12-19 18:43:42 +01:00
projects,
shallowProjects,
groupedProjectsPerCategory,
2023-12-19 18:43:42 +01:00
filteredProjectsCount,
fetchData,
getProjectById,
filteredProjects,
projectToShallow,
}
})