From fbf0d0cbdbb927a5d1ec1d9a5164f2016986e514 Mon Sep 17 00:00:00 2001 From: Julian Early Date: Wed, 17 Apr 2024 22:35:43 -0700 Subject: [PATCH] feat: add data read/write for users --- .env | 1 + .env.development | 2 ++ .env.production | 3 +-- src/shared/data/fidgetData.ts | 46 +++++++++++++++++++++++++++++++++++ tsconfig.json | 9 ------- 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 .env create mode 100644 .env.development create mode 100644 src/shared/data/fidgetData.ts diff --git a/.env b/.env new file mode 100644 index 00000000..4c3e1842 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +NEXT_PUBLIC_MOD_PROTOCOL_API_URL = 'https://api.modprotocol.org/api' \ No newline at end of file diff --git a/.env.development b/.env.development new file mode 100644 index 00000000..49decb0a --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +NEXT_PUBLIC_VERCEL_ENV = development +NEXT_PUBLIC_URL = 'http://localhost:3000' \ No newline at end of file diff --git a/.env.production b/.env.production index a58516d5..b22e73a1 100644 --- a/.env.production +++ b/.env.production @@ -1,3 +1,2 @@ NEXT_PUBLIC_VERCEL_ENV = production -NEXT_PUBLIC_URL = 'https://nounspace.com' -NEXT_PUBLIC_MOD_PROTOCOL_API_URL = 'https://api.modprotocol.org/api' \ No newline at end of file +NEXT_PUBLIC_URL = 'https://nounspace.com' \ No newline at end of file diff --git a/src/shared/data/fidgetData.ts b/src/shared/data/fidgetData.ts new file mode 100644 index 00000000..e7cb7542 --- /dev/null +++ b/src/shared/data/fidgetData.ts @@ -0,0 +1,46 @@ +import { createClient } from "@/space/common/helpers/supabase/component"; + +export async function readPublicFidgetData(fidgetName, key) { + return readFidgetData(fidgetName, key); +} + +export async function writePublicFidgetData(fidgetName, key, value) { + return writeFidgetData(fidgetName, key, value); +} + +export async function readPrivateFidgetData(fidgetName, key) { + return readFidgetData(fidgetName, key, true); +} + +export async function writePivateFidgetData(fidgetName, key, value) { + return writeFidgetData(fidgetName, key, value, true); +} + +async function writeFidgetData(fidgetName, key, value, isPrivate = false) { + const supabase = createClient(); + const { data } = await supabase.storage.from(isPrivate ? "fidgets" : "fidgetsPublic").update(await createFileLoc(fidgetName, key, isPrivate), value, { + cacheControl: '3600', + upsert: false + }); + return data; +} + +async function readFidgetData(fidgetName, key, isPrivate = false) { + const supabase = createClient(); + const { data } = await supabase.storage.from(isPrivate ? "fidgets" : "fidgetsPublic").download(await createFileLoc(fidgetName, key, isPrivate)); + return data; +} + +async function createFileLoc(fidgetName, key, isPrivate = false) { + let fileLoc = `${fidgetName}/${key}`; + if (isPrivate) { + const supabase = createClient(); + const { data: { session }} = await supabase.auth.getSession(); + if (session) { + fileLoc = `${session!.user.id}${fileLoc}` + } else { + throw new Error("User is not logged in and cannot access private data") + } + } + return fileLoc; +} diff --git a/tsconfig.json b/tsconfig.json index ed56b581..fa5d38b5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,11 +25,6 @@ "jsx": "preserve", "baseUrl": ".", "incremental": true, - "types": [ - "node", - "react/next", - "react-dom/next" - ], "paths": { "@/*": [ "src/*" @@ -48,9 +43,5 @@ // "./**/*.tsx", // "./src", // "./src/**/*.tsx" - ], - "exclude": [ - "node_modules", - "./node_modules" ] }