1- import { getNodeEnv } from "roamjs-components/util/env" ;
21import getCurrentUserEmail from "roamjs-components/queries/getCurrentUserEmail" ;
32import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName" ;
43import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle" ;
@@ -8,11 +7,12 @@ import { Enums } from "@repo/database/types.gen";
87import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage" ;
98import getBlockProps from "~/utils/getBlockProps" ;
109import setBlockProps from "~/utils/setBlockProps" ;
10+ import { type DGSupabaseClient } from "@repo/ui/lib/supabase/client" ;
1111import {
12- createClient ,
13- type DGSupabaseClient ,
14- } from "@repo/ui/src/lib/supabase/client" ;
15- import { spaceAnonUserEmail } from "@repo/ui/lib/utils " ;
12+ fetchOrCreateSpaceId ,
13+ fetchOrCreatePlatformAccount ,
14+ createLoggedInClient ,
15+ } from "@repo/ui/lib/supabase/contextFunctions " ;
1616
1717declare const crypto : { randomUUID : ( ) => string } ;
1818
@@ -27,12 +27,6 @@ export type SupabaseContext = {
2727
2828let _contextCache : SupabaseContext | null = null ;
2929
30- // TODO: This should be an util on its own.
31- const base_url =
32- getNodeEnv ( ) === "development"
33- ? "http://localhost:3000/api/supabase"
34- : "https://discoursegraphs.com/api/supabase" ;
35-
3630const settingsConfigPageUid = getPageUidByPageTitle (
3731 DISCOURSE_CONFIG_PAGE_TITLE ,
3832) ;
@@ -53,95 +47,30 @@ const getOrCreateSpacePassword = () => {
5347// It is better if this is still at least protected by CORS.
5448// But calls anywhere else should use the supabase client directly.
5549
56- const fetchOrCreateSpaceId = async (
57- account_id : number ,
58- password : string ,
59- ) : Promise < number > => {
60- const url = getRoamUrl ( ) ;
61- const name = window . roamAlphaAPI . graph . name ;
62- const platform : Platform = "Roam" ;
63- const response = await fetch ( base_url + "/space" , {
64- method : "POST" ,
65- headers : {
66- "Content-Type" : "application/json" ,
67- } ,
68- body : JSON . stringify ( {
69- space : { url, name, platform } ,
70- password,
71- account_id,
72- } ) ,
73- } ) ;
74- if ( ! response . ok )
75- throw new Error (
76- `Platform API failed: \${response.status} \${response.statusText}` ,
77- ) ;
78- const space = await response . json ( ) ;
79- if ( typeof space . id !== "number" ) throw new Error ( "API did not return space" ) ;
80- return space . id ;
81- } ;
82-
83- const fetchOrCreatePlatformAccount = async ( {
84- accountLocalId,
85- personName,
86- personEmail,
87- } : {
88- accountLocalId : string ;
89- personName : string ;
90- personEmail : string | undefined ;
91- } ) : Promise < number > => {
92- const response = await fetch ( `${ base_url } /platform-account` , {
93- method : "POST" ,
94- headers : {
95- "Content-Type" : "application/json" ,
96- } ,
97- body : JSON . stringify ( {
98- platform : "Roam" ,
99- account_local_id : accountLocalId ,
100- name : personName ,
101- } ) ,
102- } ) ;
103- if ( ! response . ok )
104- throw new Error (
105- `Platform API failed: \${response.status} \${response.statusText}` ,
106- ) ;
107- const account = await response . json ( ) ;
108- if ( personEmail !== undefined ) {
109- const idResponse = await fetch ( `${ base_url } /agent-identifier` , {
110- method : "POST" ,
111- headers : {
112- "Content-Type" : "application/json" ,
113- } ,
114- body : JSON . stringify ( {
115- account_id : account . id ,
116- identifier_type : "email" ,
117- value : personEmail ,
118- trusted : false , // Roam tests email
119- } ) ,
120- } ) ;
121- if ( ! idResponse . ok ) {
122- const error = await idResponse . text ( ) ;
123- console . error ( `Error setting the email for the account: ${ error } ` ) ;
124- // This is not a reason to stop here
125- }
126- }
127- if ( typeof account . id !== "number" )
128- throw new Error ( "API did not return account" ) ;
129- return account . id ;
130- } ;
131-
13250export const getSupabaseContext = async ( ) : Promise < SupabaseContext | null > => {
13351 if ( _contextCache === null ) {
13452 try {
13553 const accountLocalId = window . roamAlphaAPI . user . uid ( ) ;
13654 const spacePassword = getOrCreateSpacePassword ( ) ;
13755 const personEmail = getCurrentUserEmail ( ) ;
13856 const personName = getCurrentUserDisplayName ( ) ;
57+ const url = getRoamUrl ( ) ;
58+ const spaceName = window . roamAlphaAPI . graph . name ;
59+ const platform : Platform = "Roam" ;
60+ const spaceId = await fetchOrCreateSpaceId ( {
61+ password : spacePassword ,
62+ url,
63+ name : spaceName ,
64+ platform,
65+ } ) ;
13966 const userId = await fetchOrCreatePlatformAccount ( {
67+ platform : "Roam" ,
14068 accountLocalId,
141- personName,
142- personEmail,
69+ name : personName ,
70+ email : personEmail ,
71+ spaceId,
72+ password : spacePassword ,
14373 } ) ;
144- const spaceId = await fetchOrCreateSpaceId ( userId , spacePassword ) ;
14574 _contextCache = {
14675 platform : "Roam" ,
14776 spaceId,
@@ -162,15 +91,11 @@ export const getLoggedInClient = async (): Promise<DGSupabaseClient> => {
16291 if ( _loggedInClient === null ) {
16392 const context = await getSupabaseContext ( ) ;
16493 if ( context === null ) throw new Error ( "Could not create context" ) ;
165- _loggedInClient = createClient ( ) ;
166- const { error } = await _loggedInClient . auth . signInWithPassword ( {
167- email : spaceAnonUserEmail ( context . platform , context . spaceId ) ,
168- password : context . spacePassword ,
169- } ) ;
170- if ( error ) {
171- _loggedInClient = null ;
172- throw new Error ( `Authentication failed: ${ error . message } ` ) ;
173- }
94+ _loggedInClient = await createLoggedInClient (
95+ context . platform ,
96+ context . spaceId ,
97+ context . spacePassword ,
98+ ) ;
17499 } else {
175100 // renew session
176101 const { error } = await _loggedInClient . auth . getSession ( ) ;
0 commit comments