Skip to content

Commit

Permalink
Add generic address labelling
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Jul 10, 2023
1 parent 564ffd6 commit dbfe4dd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://
"""
scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

input LabelInput {
address: Address!
}

type Mutation {
approve(input: ApproveInput!): TransactionProposal!
createAccount(input: CreateAccountInput!): Account!
Expand Down Expand Up @@ -336,6 +340,7 @@ type Query {
contacts: [Contact!]!
contract(input: ContractInput!): Contract
contractFunction(input: ContractFunctionInput!): ContractFunction
label(input: LabelInput!): String
paymaster: Address!
policies: [Policy!]!
policy(input: UniquePolicyInput!): Policy
Expand Down
6 changes: 6 additions & 0 deletions api/src/features/contacts/contacts.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export class UpsertContactInput {
@Field(() => String)
label: string;
}

@InputType()
export class LabelInput {
@AddressField()
address: Address;
}
7 changes: 6 additions & 1 deletion api/src/features/contacts/contacts.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ID, Info, Mutation, Query, Resolver } from '@nestjs/graphql';
import { GraphQLResolveInfo } from 'graphql';
import { ContactInput, UpsertContactInput } from './contacts.input';
import { ContactInput, LabelInput, UpsertContactInput } from './contacts.input';
import { Contact } from './contacts.model';
import { ContactsService } from './contacts.service';
import { getShape } from '../database/database.select';
Expand All @@ -21,6 +21,11 @@ export class ContactsResolver {
return this.service.select(getShape(info));
}

@Query(() => String, { nullable: true })
async label(@Input() { address }: LabelInput) {
return this.service.label(address);
}

@Mutation(() => Contact)
async upsertContact(@Input() input: UpsertContactInput, @Info() info: GraphQLResolveInfo) {
const id = await this.service.upsert(input);
Expand Down
19 changes: 19 additions & 0 deletions api/src/features/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,23 @@ export class ContactsService {
async delete(address: Address) {
return this.db.query(e.delete(e.Contact, uniqueContact(address)).id);
}

async label(address: Address) {
const contact = e.select(e.Contact, () => ({
filter_single: { user: e.global.current_user, address },
label: true,
})).label;

const account = e.select(e.Account, () => ({
filter_single: { address },
name: true,
})).name;

const approver = e.select(e.Approver, () => ({
filter_single: { address },
label: true,
})).label;

return this.db.query(e.select(e.op(e.op(contact, '??', account), '??', approver)));
}
}
17 changes: 15 additions & 2 deletions app/src/components/address/AddressLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ import { Address } from 'lib';
import { useMemo } from 'react';
import { useMaybeToken } from '@token/useToken';
import { truncateAddr } from '~/util/format';
import { gql } from '@api/gen';
import { useSuspenseQuery } from '@apollo/client';
import { AddressLabelQuery, AddressLabelQueryVariables } from '@api/gen/graphql';

const AddressLabelDoc = gql(/* GraphQL */ `
query AddressLabel($address: Address!) {
label(input: { address: $address })
}
`);

export const useAddressLabel = <A extends Address | undefined>(address: A) => {
const token = useMaybeToken(address);
const label = useSuspenseQuery<AddressLabelQuery, AddressLabelQueryVariables>(AddressLabelDoc, {
variables: { address: address! },
skip: !address,
}).data?.label;

return useMemo(
() => (!address ? undefined : token?.name || truncateAddr(address)),
[address, token?.name],
() => (!address ? undefined : label || token?.name || truncateAddr(address)),
[label, address, token?.name],
) as A extends undefined ? string | undefined : string;
};

Expand Down

0 comments on commit dbfe4dd

Please sign in to comment.