From e90afc56f8c2e4087e6882476004e77fc5e6f624 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 12 Dec 2021 10:18:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20(history)=20add=20last=20visited=20?= =?UTF-8?q?repo=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/FluxNote.vue | 7 +++- .../history/components/LastVisited.vue | 2 +- .../history/hooks/useLastVisitedRepos.hook.ts | 2 +- .../history/hooks/useVisitRepo.hook.ts | 41 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/modules/history/hooks/useVisitRepo.hook.ts diff --git a/src/components/FluxNote.vue b/src/components/FluxNote.vue index 69e72cb..004bd81 100644 --- a/src/components/FluxNote.vue +++ b/src/components/FluxNote.vue @@ -54,7 +54,8 @@ import { watch, nextTick, toRefs, - onUnmounted + onUnmounted, + onMounted } from 'vue' import HeaderNote from '@/components/HeaderNote.vue' import { useNote } from '@/hooks/useNote.hook' @@ -62,6 +63,7 @@ import { useMarkdown } from '@/hooks/useMarkdown.hook' import { useLinks } from '@/hooks/useLinks.hook' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' import { useUserSettings } from '@/modules/user/hooks/useUserSettings.hook' +import { useVisitRepo } from '@/modules/history/hooks/useVisitRepo.hook' const StackedNote = defineAsyncComponent( () => import('@/components/StackedNote.vue') @@ -85,6 +87,7 @@ export default defineComponent({ const refProps = toRefs(props) const store = useUserRepoStore() useUserSettings() + const { visitRepo } = useVisitRepo({ user: props.user, repo: props.repo }) const { renderString } = useMarkdown(props.repo) const { listenToClick } = useLinks('note-display') const { stackedNotes, resetStackedNotes } = useQueryStackedNotes() @@ -120,6 +123,8 @@ export default defineComponent({ { immediate: true } ) + onMounted(() => visitRepo()) + onUnmounted(() => { store.resetFiles() resetStackedNotes() diff --git a/src/modules/history/components/LastVisited.vue b/src/modules/history/components/LastVisited.vue index 8768652..79c7c65 100644 --- a/src/modules/history/components/LastVisited.vue +++ b/src/modules/history/components/LastVisited.vue @@ -7,7 +7,7 @@ {{ lastVisitedRepo.user }}/{{ lastVisitedRepo.repo }} diff --git a/src/modules/history/hooks/useLastVisitedRepos.hook.ts b/src/modules/history/hooks/useLastVisitedRepos.hook.ts index 9f328cf..95627f4 100644 --- a/src/modules/history/hooks/useLastVisitedRepos.hook.ts +++ b/src/modules/history/hooks/useLastVisitedRepos.hook.ts @@ -7,7 +7,7 @@ import { computed } from 'vue' export const useLastVisitedRepos = () => { const history = useAsyncState( data.get( - data.generateId(DataType.BacklinkNote, 'history') + data.generateId(DataType.History, 'history') ), null ) diff --git a/src/modules/history/hooks/useVisitRepo.hook.ts b/src/modules/history/hooks/useVisitRepo.hook.ts new file mode 100644 index 0000000..d910513 --- /dev/null +++ b/src/modules/history/hooks/useVisitRepo.hook.ts @@ -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(HISTORY_ID) + if (!history) { + const newHistory: History = { + _id: HISTORY_ID, + $type: DataType.History, + repos: [newRepo] + } + await data.add(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 + } +}