Skip to content

Commit

Permalink
tmp2
Browse files Browse the repository at this point in the history
  • Loading branch information
patsissons committed Feb 21, 2024
1 parent a8f1c58 commit 5c03cbf
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 79 deletions.
File renamed without changes.
22 changes: 17 additions & 5 deletions src/lib/client/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, cacheExchange, fetchExchange, gql, setContextClient } from '@urql/svelte'
import { Client, cacheExchange, fetchExchange, gql } from '@urql/core'
import type { Fetch } from './types'

export function createClient(fetch?: Fetch) {
Expand All @@ -9,10 +9,6 @@ export function createClient(fetch?: Fetch) {
})
}

export function initializeGraphQL() {
setContextClient(createClient())
}

const fragments = {
event: gql`
fragment EventFragment on Event {
Expand Down Expand Up @@ -77,6 +73,22 @@ export const query = {
}
}
${fragments.event}
${fragments.token}
`,
account: gql`
query AccountQuery($address: String!, $first: Int = 5) {
account(id: $address) {
id
tokens(first: $first, orderBy: created, orderDirection: desc) {
...TokenFragment
event {
...EventFragment
}
}
}
}
${fragments.event}
${fragments.token}
`,
Expand Down
39 changes: 37 additions & 2 deletions src/lib/client/poap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { getContextClient } from '@urql/svelte'
import type { POAPEventMetadata, POAPEventWithTokens, POAPTokenWithEvent } from '$lib/types/poap'
import type {
POAPAccountWithTokens,
POAPEventMetadata,
POAPEventWithTokens,
POAPTokenWithEvent,
} from '$lib/types/poap'
import { Cache } from './cache'
import { query } from './graphql'
import { fetchJson } from './json'
Expand All @@ -18,6 +23,7 @@ export async function fetchPOAPMetadata(eventId: string) {

export interface FetchPOAPEventsVariables {
ids: number[]
first?: number
}

export interface FetchPOAPEventsData {
Expand All @@ -26,6 +32,7 @@ export interface FetchPOAPEventsData {

export function queryPOAPEvents(
ids: FetchPOAPEventsVariables['ids'],
first: FetchPOAPEventsVariables['first'],
initialData?: FetchPOAPEventsData,
) {
return withPolling<FetchPOAPEventsData, FetchPOAPEventsVariables>(
Expand All @@ -34,6 +41,7 @@ export function queryPOAPEvents(
query: query.events,
variables: {
ids,
first,
},
},
initialData,
Expand Down Expand Up @@ -69,7 +77,7 @@ export interface FetchPOAPTokenVariables {
}

export interface FetchPOAPTokenData {
token: POAPTokenWithEvent
token?: POAPTokenWithEvent | null
}

export function queryPOAPToken(
Expand All @@ -87,3 +95,30 @@ export function queryPOAPToken(
initialData,
)
}

export interface FetchPOAPAccountVariables {
address: string
first?: number
}

export interface FetchPOAPAccountData {
account?: POAPAccountWithTokens | null
}

export function queryPOAPAccount(
address: FetchPOAPAccountVariables['address'],
first: FetchPOAPAccountVariables['first'],
initialData?: FetchPOAPAccountData,
) {
return withPolling<FetchPOAPAccountData>(
{
client: getContextClient(),
query: query.account,
variables: {
address: address.toLowerCase(),
first,
},
},
initialData,
)
}
9 changes: 4 additions & 5 deletions src/lib/components/EventTokens/event-tokens.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { MaxItems } from '$lib/client/constants'
import type { POAPEventMetadata, POAPEventWithTokens, POAPToken } from '$lib/types/poap'
import { collectItems } from '$lib/utils/items'
import { Token } from '../Token'
export let events: POAPEventWithTokens[]
export let metadata: Record<string, POAPEventMetadata>
export let max = 25
export let max = MaxItems
const set = new Set<string>()
Expand All @@ -22,9 +23,7 @@
</script>

<div class="grid grid-cols-1 auto-rows-min gap-2 py-2 w-full">
{#each tokens as token}
{#key token.id}
<Token {token} event={eventMap[token.event.id]} metadata={metadata[token.event.id]} />
{/key}
{#each tokens as token (token.id)}
<Token {token} event={eventMap[token.event.id]} metadata={metadata[token.event.id]} />
{/each}
</div>
2 changes: 2 additions & 0 deletions src/lib/components/Loadable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Loadable } from './loadable.svelte'
export { default as LoadableQuery } from './loadable-query.svelte'
14 changes: 14 additions & 0 deletions src/lib/components/Loadable/loadable-query.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import type { OperationResultStore } from '@urql/svelte'
import Loadable from './loadable.svelte'
type Data = $$Generic

Check failure on line 5 in src/lib/components/Loadable/loadable-query.svelte

View workflow job for this annotation

GitHub Actions / run

'$$Generic' is not defined
// not sure why this is needed, but svelte cannot type the forwarded slot prop without it
type $$Slots = { default: { loaded: Data } }

Check warning on line 7 in src/lib/components/Loadable/loadable-query.svelte

View workflow job for this annotation

GitHub Actions / run

'$$Slots' is defined but never used
export let query: OperationResultStore<Data>
</script>

<Loadable error={$query.error} data={$query.data} let:loaded>
<slot {loaded} />
</Loadable>
23 changes: 23 additions & 0 deletions src/lib/components/Loadable/loadable.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import { ErrorMessage } from '../ErrorMessage'
import { Loading } from '../Loading'
type T = $$Generic

Check failure on line 5 in src/lib/components/Loadable/loadable.svelte

View workflow job for this annotation

GitHub Actions / run

'$$Generic' is not defined
export let error: unknown
export let data: T | null | undefined

Check failure on line 8 in src/lib/components/Loadable/loadable.svelte

View workflow job for this annotation

GitHub Actions / run

'any' overrides all other types in this union type
$: loaded = data as T

Check failure on line 10 in src/lib/components/Loadable/loadable.svelte

View workflow job for this annotation

GitHub Actions / run

Unsafe assignment of an `any` value
</script>

<div class="grid h-full">
{#if error}
<ErrorMessage {error} />
{:else if loaded}
<slot {loaded} />
{:else}
<div class="place-self-center">
<Loading />
</div>
{/if}
</div>
4 changes: 1 addition & 3 deletions src/lib/components/Token/account.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
<a
class="text-indigo-700 hover:brightness-110 hover:underline transition-all"
class:font-mono={!ens.name}
href="https://www.ondora.xyz/network/ethereum/accounts/{account.id}"
target="_blank"
rel="noopener noreferrer"
href="/account/{ens.name || account.id}"
>
{address}
</a>
Expand Down
1 change: 1 addition & 0 deletions src/lib/components/Tokens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Tokens } from './tokens.svelte'
25 changes: 25 additions & 0 deletions src/lib/components/Tokens/tokens.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
import { MaxItems } from '$lib/client/constants'
import type { FetchPOAPTokensData } from '$lib/client/poap'
import type { POAPEventMetadata, POAPTokenWithEvent } from '$lib/types/poap'
import { collectItems } from '$lib/utils/items'
import { Token } from '../Token'
export let tokenData: FetchPOAPTokensData
export let metadata: Record<string, POAPEventMetadata> = {}
export let max = MaxItems
const set = new Set<string>()
let tokens: POAPTokenWithEvent[] = []
$: tokens = collectItems(set, max, tokens, tokenData.tokens, ({ id }) => id).sort(
(a, b) => Number(b.created) - Number(a.created),
)
</script>

<div class="grid grid-cols-1 auto-rows-min gap-2 py-2 w-full">
{#each tokens as token (token.id)}
<Token {token} event={token.event} metadata={metadata[token.event.id]} />
{/each}
</div>
8 changes: 7 additions & 1 deletion src/lib/server/ens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createPublicClient, http, isAddress } from 'viem'
import { mainnet } from 'viem/chains'
import { getEnsAvatar, getEnsName, normalize, getEnsText } from 'viem/ens'
import { getEnsAvatar, getEnsName, normalize, getEnsText, getEnsAddress } from 'viem/ens'
import type { ENSRecords } from '$lib/types/ens'

const client = createPublicClient({
Expand Down Expand Up @@ -37,3 +37,9 @@ export async function fetchENS(address: string): Promise<ENSRecords> {
return `https://metadata.ens.domains/mainnet/avatar/${name}`
}
}

export function fetchReverseENS(address: string) {
if (isAddress(address)) return Promise.resolve(address)

return getEnsAddress(client, { name: normalize(address) })
}
40 changes: 33 additions & 7 deletions src/lib/server/poap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createClient, query } from '$lib/client/graphql'
import { fetchJson } from '$lib/client/json'
import type {
FetchPOAPAccountData,
FetchPOAPAccountVariables,
FetchPOAPEventsData,
FetchPOAPEventsVariables,
FetchPOAPTokenData,
FetchPOAPTokenVariables,
FetchPOAPTokensData,
FetchPOAPTokensVariables,
} from '$lib/client/poap'
Expand All @@ -25,16 +28,39 @@ export function fetchPOAPMetadata(eventId: number, fetch: Fetch) {
})
}

export function fetchPOAPEvents(ids: FetchPOAPEventsVariables['ids'], fetch: Fetch) {
return createClient(fetch).query<FetchPOAPEventsData>(query.events, { ids }).toPromise()
export function fetchPOAPEvents(
ids: FetchPOAPEventsVariables['ids'],
first: FetchPOAPEventsVariables['first'],
fetch: Fetch,
) {
return createClient(fetch)
.query<FetchPOAPEventsData, FetchPOAPEventsVariables>(query.events, { ids, first })
.toPromise()
}

export function fetchPOAPTokens(first: FetchPOAPTokensVariables['first'], fetch: Fetch) {
return createClient(fetch).query<FetchPOAPTokensData>(query.tokens, { first }).toPromise()
return createClient(fetch)
.query<FetchPOAPTokensData, FetchPOAPTokensVariables>(query.tokens, { first })
.toPromise()
}

export function fetchPOAPToken(id: FetchPOAPTokenVariables['id'], fetch: Fetch) {
return createClient(fetch)
.query<FetchPOAPTokenData, FetchPOAPTokenVariables>(query.token, { id })
.toPromise()
}

export function fetchPOAPToken(id: number, fetch: Fetch) {
return createClient(fetch).query<FetchPOAPTokenData>(query.token, { id }).toPromise()
export function fetchPOAPAccount(
address: FetchPOAPAccountVariables['address'],
first: FetchPOAPAccountVariables['first'],
fetch: Fetch,
) {
return createClient(fetch)
.query<
FetchPOAPAccountData,
FetchPOAPAccountVariables
>(query.account, { address: address.toLowerCase(), first })
.toPromise()
}

export function fetchLatestEventPOAPTokens(eventIds: number[], fetch: Fetch) {
Expand All @@ -58,7 +84,7 @@ export function fetchLatestEventPOAPTokens(eventIds: number[], fetch: Fetch) {

export function fetchLatestAccountPOAPToken(address: string, fetch: Fetch) {
return createClient(fetch)
.query<{ account: { id: string; tokens: { id: string }[] } }>(
.query<{ account: { id: string; tokens: { id: string }[] } | null | undefined }>(
gql`
query LatestEventToken($address: String!) {
account(id: $address) {
Expand All @@ -69,7 +95,7 @@ export function fetchLatestAccountPOAPToken(address: string, fetch: Fetch) {
}
}
`,
{ address },
{ address: address.toLowerCase() },
)
.toPromise()
}
5 changes: 5 additions & 0 deletions src/lib/types/poap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface POAPEventWithTokens extends POAPEvent {
tokens: POAPToken[]
}

export interface POAPAccountWithTokens {
id: string
tokens: POAPTokenWithEvent[]
}

export interface POAPEventMetadata {
id: number
fancy_id: string
Expand Down
5 changes: 3 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="ts">
import { initializeGraphQL } from '$lib/client/graphql'
import { setContextClient } from '@urql/svelte'
import { createClient } from '$lib/client/graphql'
import '$lib/styles/app.css'
initializeGraphQL()
setContextClient(createClient())
</script>

<div class="flex flex-col min-h-[100dvh] max-h-[100dvh]">
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaxItems } from '$lib/server/constants'
import { MaxItems } from '$lib/client/constants'
import { fetchPOAPTokens } from '$lib/server/poap'
import type { PageServerLoad } from './$types'

Expand Down
32 changes: 6 additions & 26 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
<script lang="ts">
import { queryPOAPTokens } from '$lib/client/poap'
import { AppFrame } from '$lib/components/AppFrame'
import { ErrorMessage } from '$lib/components/ErrorMessage'
import { Loading } from '$lib/components/Loading'
import { Token } from '$lib/components/Token'
import type { POAPTokenWithEvent } from '$lib/types/poap'
import { collectItems } from '$lib/utils/items'
import { LoadableQuery } from '$lib/components/Loadable'
import { Tokens } from '$lib/components/Tokens'
import type { PageData } from './$types'
export let data: PageData
const query = queryPOAPTokens(data.max, data.initialData).poll()
const set = new Set<string>()
let tokens: POAPTokenWithEvent[] = []
$: tokens = collectItems(set, data.max, tokens, $query.data?.tokens, ({ id }) => id)
</script>

<AppFrame context={{ tokenId: tokens[0].id }}>
<div class="grid h-full">
{#if $query.error}
<ErrorMessage error={$query.error} />
{:else if tokens}
<div class="grid grid-cols-1 auto-rows-min gap-2 py-2 w-full">
{#each tokens as token (token.id)}
<Token {token} event={token.event} />
{/each}
</div>
{:else}
<div class="place-self-center">
<Loading />
</div>
{/if}
</div>
<AppFrame context={{ tokenId: $query.data?.tokens[0].id }}>
<LoadableQuery {query} let:loaded>
<Tokens tokenData={loaded} max={data.max} />
</LoadableQuery>
</AppFrame>
Loading

0 comments on commit 5c03cbf

Please sign in to comment.