From 645ccc37f80b24e1b709e43898ffa3e2abfadbcf Mon Sep 17 00:00:00 2001 From: Drew Harris Date: Mon, 16 Sep 2024 13:33:43 -0500 Subject: [PATCH] cleanup: small fixes --- .../src/components/ApiKeyGenerateModal.tsx | 2 +- .../src/contexts/UserContext.tsxd demo | 187 ------------------ 2 files changed, 1 insertion(+), 188 deletions(-) delete mode 100644 frontends/dashboard/src/contexts/UserContext.tsxd demo diff --git a/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx b/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx index 09ed4a91c3..f3dd575a30 100644 --- a/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx +++ b/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx @@ -150,7 +150,7 @@ export const ApiKeyGenerateModal = (props: { }, false); const currentUserRole = createMemo(() => { - const selectedOrgId = userContext.selectedOrganizationId?.(); + const selectedOrgId = userContext.selectedOrganization().id; if (!selectedOrgId) return 0; return ( userContext diff --git a/frontends/dashboard/src/contexts/UserContext.tsxd demo b/frontends/dashboard/src/contexts/UserContext.tsxd demo deleted file mode 100644 index 347ee8a041..0000000000 --- a/frontends/dashboard/src/contexts/UserContext.tsxd demo +++ /dev/null @@ -1,187 +0,0 @@ -import { - Accessor, - JSX, - Resource, - Show, - createContext, - createEffect, - createResource, - createSignal, - useContext, -} from "solid-js"; -import { createToast } from "../components/ShowToasts"; -import { SlimUser } from "shared/types"; -import { redirect, useSearchParams } from "@solidjs/router"; -import { ApiContext } from ".."; -import { DatasetAndUsage } from "trieve-ts-sdk"; - -export interface UserStoreContextProps { - children?: JSX.Element; -} - -export interface Notification { - message: string; - type: "error" | "success" | "info"; - timeout?: number; -} - -export interface UserStore { - user: Accessor; - isNewUser: Accessor; - selectedOrganization: Accessor; - orgDatasets: Resource; - login: () => void; - logout: () => void; -} - -export const UserContext = createContext({ - user: () => null as unknown as SlimUser, - isNewUser: () => false, - login: () => {}, - orgDatasets: null as unknown as Resource, - logout: () => {}, - selectedOrganization: () => null as unknown as SlimUser["orgs"][0], -}); - -const getInitalUser = () => { - const user = window.localStorage.getItem("trieve:user"); - if (user) { - return JSON.parse(user) as SlimUser; - } -}; - -export const UserContextWrapper = (props: UserStoreContextProps) => { - const [searchParams] = useSearchParams(); - const trieve = useContext(ApiContext); - - const [user, setUser] = createSignal( - getInitalUser() ?? null, - ); - const [isNewUser, setIsNewUser] = createSignal(false); - const [selectedOrganization, setSelectedOrganization] = createSignal< - SlimUser["orgs"][0] | null - >(null); - - const apiHost = import.meta.env.VITE_API_HOST as string; - - const logout = () => { - void fetch(`${apiHost}/auth?redirect_uri=${window.origin}`, { - method: "DELETE", - credentials: "include", - }).then((res) => { - res - .json() - .then((res) => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access - window.location.href = res.logout_url; - window.localStorage.removeItem("trieve:user"); - setUser(null); - setSelectedOrganization(null); - }) - .catch((error) => { - console.error(error); - }); - }); - }; - - const login = () => { - fetch(`${apiHost}/auth/me`, { - credentials: "include", - }) - .then((res) => { - if (res.status === 401) { - window.location.href = `${apiHost}/auth?redirect_uri=${window.origin}/dashboard/foo`; - } - return res.json(); - }) - .then((data: SlimUser) => { - // cache the user - window.localStorage.setItem("trieve:user", JSON.stringify(data)); - - // Grab org id from localstorage - const possibleOrgId = window.localStorage.getItem( - `${data.id}:selectedOrg`, - ); - if (possibleOrgId) { - const matchingOrg = data.orgs.find((org) => org.id === possibleOrgId); - if (matchingOrg) { - setSelectedOrganization(matchingOrg); - } - } else { - const firstOrg = data.orgs.at(0); - if (firstOrg) { - setSelectedOrganization(firstOrg); - } else { - redirect("/dashboard/new_user"); - } - } - - setUser(data); - }) - .catch((err) => { - setUser(null); - console.error(err); - createToast({ - title: "Error", - type: "error", - message: "Error logging in", - }); - }); - }; - - createEffect(() => { - if (searchParams["new_user"]) { - setIsNewUser(true); - } - }); - - const [orgDatasets, _] = createResource(selectedOrganization, async (org) => { - const result = await trieve.fetch( - "/api/dataset/organization/{organization_id}", - "get", - { - organizationId: org.id, - }, - ); - return result; - }); - - createEffect(() => { - login(); - }); - - return ( - <> - -
-
- } - when={user()} - > - {(user) => ( - - {(org) => ( - - {props.children} - -
New user!!
-
-
- )} -
- )} -
- - ); -};