Skip to content

Commit

Permalink
More type fixes for user-related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 5, 2024
1 parent 83c7d0e commit 85dd962
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
11 changes: 5 additions & 6 deletions client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export function isHistoryItem(item: object): item is HistoryItemSummary {

type RegisteredUserModel = components["schemas"]["DetailedUserModel"];
type AnonymousUserModel = components["schemas"]["AnonUserModel"];
type UserModel = RegisteredUserModel | AnonymousUserModel;

export interface RegisteredUser extends RegisteredUserModel {
isAnonymous: false;
Expand All @@ -226,20 +227,18 @@ export interface AnonymousUser extends AnonymousUserModel {
isAnonymous: true;
}

export type GenericUser = RegisteredUser | AnonymousUser;

/** Represents any user, including anonymous users or session-less (null) users.**/
export type AnyUser = GenericUser | null;
export type AnyUser = RegisteredUser | AnonymousUser | null;

export function isRegisteredUser(user: AnyUser): user is RegisteredUser {
export function isRegisteredUser(user: AnyUser | UserModel): user is RegisteredUser {
return user !== null && "email" in user;
}

export function isAnonymousUser(user: AnyUser): user is AnonymousUser {
export function isAnonymousUser(user: AnyUser | UserModel): user is AnonymousUser {
return user !== null && !isRegisteredUser(user);
}

export function isAdminUser(user: AnyUser): user is RegisteredUser {
export function isAdminUser(user: AnyUser | UserModel): user is RegisteredUser {
return isRegisteredUser(user) && user.is_admin;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { BAlert } from "bootstrap-vue";
import { computed, ref } from "vue";
import { type GenericUser, type HistorySummary, userOwnsHistory } from "@/api";
import { type AnyUser, type HistorySummary, userOwnsHistory } from "@/api";
import localize from "@/utils/localization";
library.add(faArchive, faBurn, faTrash);
interface Props {
history: HistorySummary;
currentUser: GenericUser | null;
currentUser: AnyUser;
}
const props = defineProps<Props>();
Expand Down
6 changes: 3 additions & 3 deletions client/src/composables/hashedUserId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useLocalStorage } from "@vueuse/core";
import { storeToRefs } from "pinia";
import { computed, type Ref, ref, watch } from "vue";

import { type GenericUser } from "@/api";
import { type AnyUser } from "@/api";
import { useUserStore } from "@/stores/userStore";

async function hash32(value: string): Promise<string> {
Expand Down Expand Up @@ -32,8 +32,8 @@ let unhashedId: string | null = null;
/**
* One way hashed ID of the current User
*/
export function useHashedUserId(user?: Ref<GenericUser | null>) {
let currentUser: Ref<GenericUser | null>;
export function useHashedUserId(user?: Ref<AnyUser>) {
let currentUser: Ref<AnyUser>;

if (user) {
currentUser = user;
Expand Down
4 changes: 2 additions & 2 deletions client/src/composables/userLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useLocalStorage } from "@vueuse/core";
import { computed, customRef, type Ref, ref } from "vue";

import { type GenericUser } from "@/api";
import { type AnyUser } from "@/api";

import { useHashedUserId } from "./hashedUserId";

Expand All @@ -10,7 +10,7 @@ import { useHashedUserId } from "./hashedUserId";
* @param key
* @param initialValue
*/
export function useUserLocalStorage<T>(key: string, initialValue: T, user?: Ref<GenericUser | null>) {
export function useUserLocalStorage<T>(key: string, initialValue: T, user?: Ref<AnyUser>) {
const { hashedUserId } = useHashedUserId(user);

const storedRef = computed(() => {
Expand Down
34 changes: 26 additions & 8 deletions client/src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import {
setCurrentThemeQuery,
} from "@/stores/users/queries";

interface FavoriteTools {
tools: string[];
}

interface Preferences {
theme: string;
favorites: { tools: string[] };
theme?: string;
favorites: FavoriteTools;
[key: string]: unknown;
}

type ListViewMode = "grid" | "list";
Expand Down Expand Up @@ -68,12 +73,15 @@ export const useUserStore = defineStore("userStore", () => {
if (!loadPromise) {
loadPromise = getCurrentUser()
.then(async (user) => {
currentUser.value = { ...user, isAnonymous: !user.email };
currentPreferences.value = user?.preferences ?? null;
// TODO: This is a hack to get around the fact that the API returns a string
if (currentPreferences.value?.favorites) {
currentPreferences.value.favorites = JSON.parse(user?.preferences?.favorites ?? { tools: [] });
if (isRegisteredUser(user)) {
currentUser.value = user;
currentPreferences.value = processUserPreferences(user);
} else if (isAnonymousUser(user)) {
currentUser.value = user;
} else if (user === null) {
currentUser.value = null;
}

if (includeHistories) {
const historyStore = useHistoryStore();
// load first few histories for user to start pagination
Expand Down Expand Up @@ -116,7 +124,7 @@ export const useUserStore = defineStore("userStore", () => {

function setFavoriteTools(tools: string[]) {
if (currentPreferences.value) {
currentPreferences.value.favorites.tools = tools ?? { tools: [] };
currentPreferences.value.favorites.tools = tools;
}
}

Expand All @@ -128,6 +136,16 @@ export const useUserStore = defineStore("userStore", () => {
toggledSideBar.value = toggledSideBar.value === currentOpen ? "" : currentOpen;
}

function processUserPreferences(user: RegisteredUser): Preferences {
// Favorites are returned as a JSON string by the API
const favorites =
typeof user.preferences.favorites === "string" ? JSON.parse(user.preferences.favorites) : { tools: [] };
return {
...user.preferences,
favorites,
};
}

return {
currentUser,
currentPreferences,
Expand Down

0 comments on commit 85dd962

Please sign in to comment.