-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b82874
commit e9ae332
Showing
5 changed files
with
136 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from "react"; | ||
|
||
interface JuneContextType { | ||
identify: (props: IdentifyProps) => Promise<void>; | ||
track: (props: TrackProps) => Promise<void>; | ||
} | ||
|
||
interface JuneProviderProps { | ||
writeKey: string; | ||
children: React.ReactNode; | ||
|
||
disabled?: boolean; | ||
} | ||
|
||
interface TrackProps { | ||
event: string; | ||
timestamp?: Date; | ||
userId?: string; | ||
anonymousId?: string; | ||
properties?: Record<string, unknown>; | ||
context?: Record<string, unknown>; | ||
} | ||
|
||
interface IdentifyProps { | ||
userId?: string; | ||
anonymousId?: string; | ||
traits?: Record<string, unknown>; | ||
timestamp?: Date; | ||
context?: Record<string, unknown>; | ||
} | ||
|
||
const BASE_URL = "https://api.june.so/sdk"; | ||
|
||
const JuneContext = React.createContext<JuneContextType>({ | ||
identify: async () => {}, | ||
track: async () => {}, | ||
}); | ||
|
||
export const JuneProvider: React.FC<JuneProviderProps> = ({ | ||
children, | ||
writeKey, | ||
disabled = false, | ||
}) => { | ||
if (!writeKey) { | ||
throw new Error("[useJune] writeKey is required"); | ||
} | ||
|
||
const headers = { | ||
Authorization: `Basic ${writeKey}`, | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const track = async (props: TrackProps) => { | ||
if (disabled) return; | ||
|
||
try { | ||
await fetch(`${BASE_URL}/track`, { | ||
method: "POST", | ||
headers, | ||
body: JSON.stringify({ | ||
...props, | ||
}), | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const identify = async (props: IdentifyProps) => { | ||
if (!props.userId || disabled) return; | ||
|
||
try { | ||
await fetch(`${BASE_URL}/identify`, { | ||
method: "POST", | ||
headers, | ||
body: JSON.stringify({ | ||
...props, | ||
}), | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<JuneContext.Provider value={{ identify, track }}> | ||
{children} | ||
</JuneContext.Provider> | ||
); | ||
}; | ||
|
||
export const useJune = () => { | ||
const context = React.useContext(JuneContext); | ||
if (!context) { | ||
throw new Error("[useJune] must be used within a JuneProvider"); | ||
} | ||
return context; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters