-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Implement fuzzy-search for docs
- Loading branch information
1 parent
0aa6c35
commit 960ef88
Showing
7 changed files
with
228 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,101 @@ | ||
import { useStore } from 'vuex' | ||
import type Doc from '#root/src/models/doc' | ||
import { type Doc } from '#root/src/models/doc' | ||
|
||
export const useDocs = () => { | ||
const filterByDiscarded = (docs: MaybeRef<Doc[]>) => { | ||
return toValue(docs).filter((doc) => { | ||
return !!doc.discardedAt | ||
}) | ||
} | ||
|
||
const filterByKept = (docs: MaybeRef<Doc[]>) => { | ||
return toValue(docs).filter((doc) => { | ||
return !doc.discardedAt | ||
}) | ||
} | ||
|
||
const filterByTag = (docs: MaybeRef<Doc[]>, tag: string) => { | ||
return filterByTags(docs, [tag]) | ||
} | ||
|
||
const filterByTags = (docs: MaybeRef<Doc[]>, tags: string[]) => { | ||
return toValue(docs).filter((doc) => { | ||
return tags.some((tag) => doc.tags.includes(tag)) | ||
}) | ||
} | ||
|
||
const filterByTasks = (docs: MaybeRef<Doc[]>) => { | ||
return toValue(docs).filter((doc) => { | ||
return doc.tasks.length > 0 | ||
}) | ||
} | ||
|
||
const filterByUntagged = (docs: MaybeRef<Doc[]>) => { | ||
return toValue(docs).filter((doc) => { | ||
return doc.tags.length === 0 | ||
}) | ||
} | ||
|
||
const filterByWorkspace = (docs: MaybeRef<Doc[]>, workspace: { active: boolean, tags: string[] }) => { | ||
if (!workspace.active) { | ||
return toValue(docs) | ||
} | ||
|
||
return filterByTags(docs, workspace.tags) | ||
} | ||
|
||
const sortByRecent = (docs: MaybeRef<Doc[]>) => { | ||
return toValue(docs).sort((a, b) => { | ||
// Reverse chronological order. | ||
return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() | ||
}) | ||
} | ||
|
||
export const useDocs = ({ filter }: { filter?: MaybeRef<string | undefined> } = {}) => { | ||
const store = useStore() | ||
const router = useRouter() | ||
const docs = computed(() => store.getters.decrypted) | ||
const doc = computed(() => docs.value.find((doc: Doc) => doc.id === router.currentRoute.value.params.docId)) | ||
const allDocs = computed<Doc[]>(() => store.state.documents.all) | ||
const decryptedDocs = computed(() => allDocs.value.filter(doc => !doc.encrypted)) | ||
const sortedDocs = computed(() => sortByRecent(decryptedDocs)) | ||
const workspaceDocs = computed(() => filterByWorkspace(sortedDocs, store.state.context)) | ||
const keptDocs = computed(() => filterByKept(workspaceDocs)) | ||
const discardedDocs = computed(() => filterByDiscarded(workspaceDocs.value)) | ||
const taskDocs = computed(() => filterByTasks(keptDocs)) | ||
const untaggedDocs = computed(() => filterByUntagged(keptDocs)) | ||
|
||
const docs = computed(() => { | ||
const filterValue = toValue(filter) | ||
|
||
if (filterValue?.startsWith('#')) { | ||
return filterByTag(keptDocs, filterValue.slice(1)) | ||
} | ||
|
||
if (filterValue === 'tasks') { | ||
return taskDocs.value | ||
} | ||
|
||
if (filterValue === 'discarded') { | ||
return discardedDocs.value | ||
} | ||
|
||
if (filterValue === 'untagged') { | ||
return untaggedDocs.value | ||
} | ||
|
||
return keptDocs.value | ||
}) | ||
|
||
const doc = computed(() => decryptedDocs.value.find((doc: Doc) => doc.id === router.currentRoute.value.params.docId)) | ||
|
||
return { | ||
allDocs, | ||
decryptedDocs, | ||
doc, | ||
docs, | ||
filterByTag, | ||
filterByTasks, | ||
filterByUntagged, | ||
keptDocs, | ||
sortedDocs, | ||
workspaceDocs, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.