Skip to content

Commit

Permalink
enormous app improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaomiDEV committed Aug 7, 2024
1 parent 7e5f833 commit 78ecd76
Show file tree
Hide file tree
Showing 27 changed files with 630 additions and 383 deletions.
29 changes: 6 additions & 23 deletions src/lib/db/entities/boardMessages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Ref, shallowRef, watch } from "vue";
import { db } from "..";
import { makeUUIDv5 } from "../../util/uuid";
import { UUID, UUIDable } from "../types";
import { getMembersTable, Member } from "./members";
import { getSystemUUID } from "./system";
import { from, useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";

export type BoardMessage = UUIDable & {
member: UUID,
Expand All @@ -20,33 +17,19 @@ export function getBoardMessagesTable() {
return db.boardMessages;
}

export const boardMessages: Ref<BoardMessageComplete[]> = shallowRef([]);

export async function updateBoardMessagesRef() {
const boardMessagesComplete: BoardMessageComplete[] = [];
for (const boardMessage of await getBoardMessagesTable().toArray()) {
const member = (await getMembersTable().get(boardMessage.member))!
boardMessagesComplete.push({ ...boardMessage, member});
}
boardMessages.value = boardMessagesComplete;
export async function toBoardMessageComplete(boardMessage: BoardMessage): Promise<BoardMessageComplete> {
const member = (await getMembersTable().get(boardMessage.member))!;
return { ...boardMessage, member };
}

watch([
useObservable(from(liveQuery(() => getBoardMessagesTable().toArray()))),
useObservable(from(liveQuery(() => getMembersTable().toArray()))),
], updateBoardMessagesRef, { immediate: true });


function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `boardMessages\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `boardMessages\0${name}`);
}

export async function newBoardMessage(boardMessage: Omit<BoardMessage, keyof UUIDable>) {
const uuid = genid(boardMessage.title);
const uuid = await genid(boardMessage.title);
return await getBoardMessagesTable().add({
...boardMessage,
uuid
});
}


6 changes: 3 additions & 3 deletions src/lib/db/entities/chatMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export function getChatMessagesTable() {
return db.chatMessages;
}

function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `chatMessages\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `chatMessages\0${name}`);
}

export async function newChatMessage(chatMessage: Omit<ChatMessage, keyof UUIDable>) {
const uuid = genid(chatMessage.member + chatMessage.date.toTimeString());
const uuid = await genid(chatMessage.member + chatMessage.date.toTimeString());
return await getChatMessagesTable().add({
...chatMessage,
uuid
Expand Down
6 changes: 3 additions & 3 deletions src/lib/db/entities/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export function getChatsTable(){
return db.chats;
}

function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `chats\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `chats\0${name}`);
}

export async function newChat(chat: Omit<Chat, keyof UUIDable>) {
const uuid = genid(chat.name);
const uuid = await genid(chat.name);
return await getChatsTable().add({
...chat,
uuid
Expand Down
74 changes: 5 additions & 69 deletions src/lib/db/entities/frontingEntries.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { Ref, shallowRef, watch } from "vue";
import { db } from "..";
import { makeUUIDv5 } from "../../util/uuid";
import { UUID, UUIDable } from "../types";
import { getSystemUUID } from "./system";
import { from, useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";
import { getMembersTable, Member } from "./members";
import { parseFrontingHistoryFilterQuery as parseFrontingEntriesFilterQuery } from "../../util/filterQuery";
import dayjs from "dayjs";

export type FrontingEntry = UUIDable & {
member: UUID,
Expand All @@ -28,28 +23,12 @@ export async function toFrontingEntryComplete(frontingEntry: FrontingEntry): Pro
return { ...frontingEntry, member };
}

export const frontingEntries: Ref<FrontingEntryComplete[]> = shallowRef([]);

export async function updateFrontingEntriesRef() {
const frontingEntriesComplete: FrontingEntryComplete[] = [];
for (const frontingEntry of await getFrontingEntriesTable().toArray()) {
frontingEntriesComplete.push(await toFrontingEntryComplete(frontingEntry));
}
frontingEntries.value = frontingEntriesComplete;
}

watch([
useObservable(from(liveQuery(() => getFrontingEntriesTable().toArray()))),
useObservable(from(liveQuery(() => getMembersTable().toArray())))
], updateFrontingEntriesRef, { immediate: true });


function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `frontingEntries\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `frontingEntries\0${name}`);
}

export async function newFrontingEntry(frontingEntry: Omit<FrontingEntry, keyof UUIDable>) {
const uuid = genid(frontingEntry.member + frontingEntry.startTime.getTime());
const uuid = await genid(frontingEntry.member + frontingEntry.startTime.getTime());
return await getFrontingEntriesTable().add({
...frontingEntry,
uuid
Expand Down Expand Up @@ -108,7 +87,7 @@ export async function getCurrentFrontEntryForMember(member: Member){
}

export async function getMainFronter(){
const mainFronterEntry = await getFrontingEntriesTable().get({ endTime: undefined, isMainFronter: true });
const mainFronterEntry = await getFrontingEntriesTable().filter(x => x.endTime === undefined && x.isMainFronter).first();
if(mainFronterEntry){
return await getMembersTable().get(mainFronterEntry.member);
}
Expand All @@ -124,47 +103,4 @@ export async function getFronting() {
frontingMembers.push(member);
}
return frontingMembers;
}

export async function getFrontingEntriesFromFilterQuery(filterQuery: string) {
const parsed = parseFrontingEntriesFilterQuery(filterQuery);

const filtered: FrontingEntryComplete[] = [];

for(const x of await getFrontingEntriesTable().toArray()){
const complete = await toFrontingEntryComplete(x);

if (!complete.member.name.startsWith(parsed.query))
continue;

if (parsed.currentlyFronting) {
if (x.endTime)
continue;
}

if (parsed.dateString) {
const date = dayjs(parsed.dateString).startOf("day");
if (date.valueOf() !== dayjs(x.startTime).startOf("day").valueOf())
continue;
}

if (parsed.day) {
if (parsed.day !== dayjs(x.startTime).get("date"))
continue;
}

if (parsed.month) {
if (parsed.month !== dayjs(x.startTime).get("month") + 1)
continue;
}

if (parsed.year) {
if (parsed.year !== dayjs(x.startTime).get("year"))
continue;
}

filtered.push(complete)
}

return filtered;
}
}
6 changes: 3 additions & 3 deletions src/lib/db/entities/journalPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function getJournalPostsTable() {
return db.journalPosts;
}

function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `journalPosts\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `journalPosts\0${name}`);
}

export async function newJournalPost(journalPost: Omit<JournalPost, keyof UUIDable>) {
const uuid = genid(journalPost.member + journalPost.title);
const uuid = await genid(journalPost.member + journalPost.title);
return await getJournalPostsTable().add({
...journalPost,
uuid
Expand Down
54 changes: 3 additions & 51 deletions src/lib/db/entities/members.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { shallowRef, watch } from "vue";
import { db } from "..";
import { parseMemberFilterQuery } from "../../util/filterQuery";
import { makeUUIDv5 } from "../../util/uuid";
import { UUID, UUIDable } from "../types";
import { getSystemUUID } from "./system";
import { from, useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";

export type Member = UUIDable & {
name: string,
Expand All @@ -23,58 +19,14 @@ export function getMembersTable() {
return db.members;
}

export const members = shallowRef<Member[]>([]);

export async function updateMembersRef(){
members.value = await getMembersTable().toArray();
}

watch(useObservable(from(liveQuery(() => getMembersTable().toArray()))), updateMembersRef, { immediate: true });

function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `members\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `members\0${name}`);
}

export async function newMember(member: Omit<Member, keyof UUIDable>) {
const uuid = genid(member.name);
const uuid = await genid(member.name);
return await getMembersTable().add({
...member,
uuid
});
}

export async function getMembersFromFilterQuery(filterQuery: string) {
const parsed = await parseMemberFilterQuery(filterQuery);

return getMembersTable().where("name").startsWithIgnoreCase(parsed.query).filter(x => {

if(parsed.pronouns){
if (!x.pronouns || x.pronouns.toLowerCase() !== parsed.pronouns.toLowerCase())
return false;
}

if (parsed.role) {
if (!x.role || x.role.toLowerCase() !== parsed.role.toLowerCase())
return false;
}

if(parsed.isArchived !== undefined){
if(x.isArchived !== parsed.isArchived)
return false;
}

if (parsed.isCustomFront !== undefined) {
if (x.isCustomFront !== parsed.isCustomFront)
return false;
}

if (parsed.tags.length) {
for(const uuid of parsed.tags){
if (!x.tags.includes(uuid))
return false;
}
}

return true;
}).toArray();
}
6 changes: 3 additions & 3 deletions src/lib/db/entities/reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export function getRemindersTable() {
return db.reminders;
}

function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `reminders\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `reminders\0${name}`);
}

export async function newReminder(reminder: Omit<Reminder, keyof UUIDable>) {
const uuid = genid(reminder.name);
const uuid = await genid(reminder.name);
return await getRemindersTable().add({
...reminder,
uuid
Expand Down
28 changes: 6 additions & 22 deletions src/lib/db/entities/system.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ref, Ref, watch } from "vue";
import { db } from "..";
import { AppNamespace, makeUUIDv5 } from "../../util/uuid"
import { UUIDable } from "../types"
import { from, useObservable } from "@vueuse/rxjs";
import { liveQuery } from "dexie";

export type System = UUIDable & {
name: string,
Expand All @@ -15,19 +12,6 @@ export function getSystemTable(){
return db.system;
}

export const system: Ref<System> = ref({
name: "",
uuid: ""
});

export async function updateSystemRef() {
const _system = await getSystemTable().toArray();
if(_system.length)
system.value = _system[0];
}

watch(useObservable(from(liveQuery(() => getSystemTable().toArray()))), updateSystemRef, { immediate: true });

export function genid(name: string) {
return makeUUIDv5(AppNamespace, name);
}
Expand All @@ -43,14 +27,14 @@ export async function newSystem(system: Omit<System, keyof UUIDable>){
}

// Extra because there shall only be one
export function getSystem(){
return {...system.value}
export async function getSystem(){
return await getSystemTable().toCollection().first();
}

export function getSystemUUID(){
return system.value.uuid;
export async function getSystemUUID(){
return (await getSystemTable().toCollection().first())?.uuid;
}

export function modifySystem(system: Partial<System>) {
return db.system.update(getSystemUUID(), system);
export async function modifySystem(system: Partial<System>) {
return db.system.update(await getSystemUUID(), system);
}
26 changes: 7 additions & 19 deletions src/lib/db/entities/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import { makeUUIDv5 } from "../../util/uuid";
import { UUID, UUIDable } from "../types";
import { getSystemUUID } from "./system";

import { getMembersTable as getMembers } from "./members";
import { getJournalPostsTable as getJournalPosts } from "./journalPosts";
import { liveQuery } from "dexie";
import { useObservable, from } from "@vueuse/rxjs";
import { Ref, shallowRef, watch } from "vue";
import { getMembersTable } from "./members";
import { getJournalPostsTable } from "./journalPosts";

export type Tag = UUIDable & {
name: string,
Expand All @@ -20,21 +17,12 @@ export function getTagsTable() {
return db.tags;
}

export const tags: Ref<Tag[]> = shallowRef([]);

export async function updateTagsRef() {
tags.value = await getTagsTable().toArray();
}

watch(useObservable(from(liveQuery(() => getTagsTable().toArray()))), updateTagsRef, { immediate: true });


function genid(name: string) {
return makeUUIDv5(getSystemUUID(), `tags\0${name}`);
async function genid(name: string) {
return makeUUIDv5((await getSystemUUID())!, `tags\0${name}`);
}

export async function newTag(tag: Omit<Tag, keyof UUIDable>) {
const uuid = genid(tag.name);
const uuid = await genid(tag.name);
return await getTagsTable().add({
...tag,
uuid
Expand All @@ -44,11 +32,11 @@ export async function newTag(tag: Omit<Tag, keyof UUIDable>) {
export async function removeTag(uuid: UUID){
const tag = await getTagsTable().get(uuid);
if(tag?.type === "member"){
await getMembers().toCollection().modify(member => {
await getMembersTable().toCollection().modify(member => {
member.tags = member.tags?.filter(tag => tag !== uuid)
});
} else if(tag?.type === "journal") {
await getJournalPosts().toCollection().modify(journalPost => {
await getJournalPostsTable().toCollection().modify(journalPost => {
journalPost.tags = journalPost.tags?.filter(tag => tag !== uuid)
});
}
Expand Down
Loading

0 comments on commit 78ecd76

Please sign in to comment.