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 c6d8b3e
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 80 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module.exports = {
es2017: true,
node: true,
},
globals: {
$$Generic: 'readable',
$$Slots: 'readable',
},
overrides: [
{
files: ['*.svelte'],
Expand All @@ -38,15 +42,19 @@ module.exports = {
},
],
rules: {
// this rule is interfering with $$Generic, so we'll disable it for now
// see: https://github.com/sveltejs/eslint-plugin-svelte/issues/541
'@typescript-eslint/no-redundant-type-constituents': 'off',
'@typescript-eslint/no-unused-vars': [
'warn',
'error',
{
// allow unused function parameters that start with an underscore
argsIgnorePattern: '^_',
// allow destructuring of unused array elements that start with an underscore
destructuredArrayIgnorePattern: '^_',
// allow destructuring of unused fields in order to shrink an object shape
ignoreRestSiblings: true,
varsIgnorePattern: '^\\$\\$(Props|Events|Slots)$',
},
],
},
Expand Down
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'
13 changes: 13 additions & 0 deletions src/lib/components/Loadable/loadable-query.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import type { OperationResultStore } from '@urql/svelte'
import Loadable from './loadable.svelte'
type Data = $$Generic
type $$Slots = { default: { loaded: Data } }
export let query: OperationResultStore<Data>
</script>

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

<div class="grid h-full">
{#if error}
<ErrorMessage {error} />
{:else if data}
<slot loaded={data} />
{: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
Loading

0 comments on commit c6d8b3e

Please sign in to comment.