(history) add last visited repo method

This commit is contained in:
Julien Calixte
2021-12-12 10:18:10 +01:00
parent 4a4597afbc
commit e90afc56f8
4 changed files with 49 additions and 3 deletions

View File

@@ -7,7 +7,7 @@
<router-link
:to="{
name: `Home`,
props: { user: lastVisitedRepo.user, repo: lastVisitedRepo.repo }
params: { user: lastVisitedRepo.user, repo: lastVisitedRepo.repo }
}"
>{{ lastVisitedRepo.user }}/{{ lastVisitedRepo.repo }}</router-link
>

View File

@@ -7,7 +7,7 @@ import { computed } from 'vue'
export const useLastVisitedRepos = () => {
const history = useAsyncState(
data.get<DataType.History, History>(
data.generateId(DataType.BacklinkNote, 'history')
data.generateId(DataType.History, 'history')
),
null
)

View File

@@ -0,0 +1,41 @@
import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum'
import { History } from '@/data/models/History'
const HISTORY_ID = data.generateId(DataType.History, 'history')
const MAX_REPO_HISTORY = 10
export const useVisitRepo = (newRepo: { user: string; repo: string }) => {
const visitRepo = async () => {
const history = await data.get<DataType.History, History>(HISTORY_ID)
if (!history) {
const newHistory: History = {
_id: HISTORY_ID,
$type: DataType.History,
repos: [newRepo]
}
await data.add<DataType.History>(newHistory)
return
}
const clearedRepos = history.repos.filter(
(repo) => repo.user !== newRepo.user && repo.repo !== newRepo.repo
)
const historyRepos = [newRepo, ...clearedRepos].slice(
0,
MAX_REPO_HISTORY - 1
)
const newHistory: History = {
...history,
repos: historyRepos
}
await data.update(newHistory)
}
return {
visitRepo
}
}