Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions packages/create-audius-app/examples/react-hono/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import { hc } from 'hono/client'
import { css } from '@emotion/react'
import { useSdk } from './hooks/useSdk'
import { useAuth } from './contexts/AuthProvider'
import type { AppType } from '..'
import { AppType } from '..'
import { Status } from './contexts/types'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = hc<AppType>('/') as any
const client = hc<AppType>('/')

export default function App() {
const { sdk } = useSdk()
Expand Down Expand Up @@ -83,12 +82,12 @@ export default function App() {
const { data: tracks } = await res.json()
setTracks(tracks ?? [])

const trackFavorites = (tracks ?? []).reduce(
(result: Record<string, boolean>, track: FullSdk.TrackFull) => ({
const trackFavorites = (tracks ?? []).reduce<Record<string, boolean>>(
(result, track) => ({
...result,
[track.id]: track.hasCurrentUserSaved
}),
{} as Record<string, boolean>
{}
)

setFavorites(trackFavorites)
Expand All @@ -110,7 +109,7 @@ export default function App() {

/**
* Favorite or unfavorite a track. This requires a user to be authenticated and granted
* write permissions to the app. Uses the client-side SDK with hedgehog wallet for signing.
* write permissions to the app
*/
const favoriteTrack =
(trackId: string, favorite = true): MouseEventHandler<HTMLButtonElement> =>
Expand All @@ -120,14 +119,18 @@ export default function App() {
setFavorites((prev) => ({ ...prev, [trackId]: favorite }))
try {
if (favorite) {
await sdk.tracks.favoriteTrack({
userId: user.id,
trackId
await client.favorite.$post({
json: {
userId: user.id,
trackId
}
})
} else {
await sdk.tracks.unfavoriteTrack({
userId: user.id,
trackId
await client.unfavorite.$post({
json: {
userId: user.id,
trackId
}
})
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { sdk } from '@audius/sdk'
import { Hedgehog } from '@audius/hedgehog'
import { createHedgehogWalletClient } from '@audius/sdk'

const apiKey = import.meta.env.VITE_AUDIUS_API_KEY as string

// Initialize Hedgehog for write operations
// For OAuth apps, hedgehog reads the wallet from localStorage set by OAuth flow
// We use no-op functions since OAuth handles authentication
const getFn = async () => ({ iv: '', cipherText: '' })
const setAuthFn = async () => ({ iv: '', cipherText: '' })
const setUserFn = async () => ({})

const hedgehog = new Hedgehog(
getFn,
setAuthFn,
setUserFn,
true, // useLocalStorage
window.localStorage
)

// Create SDK instance with hedgehog wallet for write operations
const audiusWalletClient = createHedgehogWalletClient(hedgehog)

const instance = sdk({
apiKey,
services: {
audiusWalletClient
}
apiKey
})

export const useSdk = () => ({ sdk: instance })
18 changes: 11 additions & 7 deletions packages/create-audius-app/tests/e2e/react-hono/react-hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ test('auths, fetches tracks, and favorites a track', async ({
// Set entropy so we don't need to do OTP
const entropy = process.env.CREATE_AUDIUS_APP_TEST_ENTROPY ?? ''
await context.addInitScript((entropy) => {
// Set entropy for both audius.co (OAuth) and localhost (app)
window.localStorage.setItem('hedgehog-entropy-key', entropy)
if (window.location.hostname === 'audius.co') {
window.localStorage.setItem('hedgehog-entropy-key', entropy)
}
}, entropy)

await page.goto('localhost:4173')
Expand All @@ -32,11 +33,14 @@ test('auths, fetches tracks, and favorites a track', async ({
await page.getByRole('button', { name: 'Get Tracks' }).click()

// Set up block_confirmation response listener
// The SDK makes requests to relay and then polls block_confirmation
const responsePromise = page.waitForResponse((response) => {
return (
response.url().includes('block_confirmation') && response.status() === 200
)
const responsePromise = page.waitForResponse(async (response) => {
if (
response.url().includes('favorite') ||
response.url().includes('unfavorite')
) {
const json = await response.json()
return json.trackId
}
})

const favoriteButton = page
Expand Down