Skip to content

Commit

Permalink
feat: changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HadiKhai committed May 20, 2024
1 parent 000cf7c commit d2cf526
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 207 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions apps/express-trial/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ app.get('/api', (req, res) => {

const port = process.env.PORT || 3333;
const server = app.listen(port, async () => {
justaname = await JustaName.init({
apiKey: process.env.JUSTANAME_API_KEY as string,
justaname = JustaName.init({
apiKey: process.env.JUSTANAME_API_KEY as string
});


Expand Down
4 changes: 2 additions & 2 deletions packages/@justaname.id/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ function MyComponent() {
import { useAddSubname } from '@justaname.id/react';

function MyComponent() {
const { claimSubname, claimSubnamePending } = useAddSubname();
const { addSubname, addSubnamePending } = useAddSubname();

const handleClaim = async () => {
try {
await claimSubname({ username: 'test' });
await addSubname({ username: 'test' });
console.log('Subname claimed successfully!');
} catch (error) {
console.error('Failed to claim subname:', error);
Expand Down
1 change: 1 addition & 0 deletions packages/@justaname.id/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './lib/hooks';
export * from './lib/providers';
export * from './lib/components/SignatureOnMounted';
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import React from 'react';
import { useMountedAccount } from '../hooks/useMountedAccount';
import { useSubnameSignature } from '../hooks/useSubnameSignature';
import { defaultRoutes } from '../constants/default-routes';

export const SignatureOnMounted: React.FC<{
routes: typeof defaultRoutes,
backendUrl: string
}> = ({
routes,
backendUrl
}) =>
export const SignatureOnMounted: React.FC = () =>
{
const { address, isConnected } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes?.requestChallengeRoute || defaultRoutes.requestChallengeRoute
})
const { getSignature } = useSubnameSignature()

React.useEffect(() => {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
export const defaultRoutes = {
addSubnameRoute: '/api/subnames/add',
revokeSubnameRoute: '/api/subnames/revoke',
rejectSubnameRoute: '/api/subnames/reject',
acceptSubnameRoute: '/api/subnames/accept',
checkSubnameAvailabilityRoute: '/api/subnames/available',
requestChallengeRoute: '/api/request-challenge',
updateSubnameRoute: '/api/subnames/update',
};
40 changes: 18 additions & 22 deletions packages/@justaname.id/react/src/lib/hooks/useAcceptSubname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useJustaName } from '../providers';
import { useAccountSubnames } from './useAccountSubnames';
import { useMountedAccount } from './useMountedAccount';
import { useSubnameSignature } from './useSubnameSignature';
import { useAccountInvitations } from './useAccountInvitations';

export interface BaseAcceptSubnameRequest {
username: string;
Expand Down Expand Up @@ -41,12 +42,10 @@ export interface UseAcceptSubname {
* @returns {UseAcceptSubname} An object containing the `acceptSubname` async function to initiate the subname accept, and a boolean `acceptSubnamePending` indicating the mutation's pending state.
*/
export const useAcceptSubname = (): UseAcceptSubname => {
const { backendUrl, routes } = useJustaName();
const { justaname } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes.requestChallengeRoute,
});
const { refetchInvitations } = useAccountInvitations();
const { getSignature } = useSubnameSignature();
const { refetchSubnames } = useAccountSubnames();

const mutate = useMutation<
Expand All @@ -61,26 +60,23 @@ export const useAcceptSubname = (): UseAcceptSubname => {

const signature = await getSignature();

const response = await fetch(backendUrl + routes.acceptSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...params,
signature: signature.signature,
address: address,
message: signature.message,
}),
const accepted = await justaname.subnames.acceptSubname({
addresses: params.addresses,
chainId: params.chainId,
contentHash: params.contentHash,
ensDomain: params.ensDomain,
text: params.text,
username: params.username,
}, {
xAddress: address,
xSignature: signature.signature,
xMessage: signature.message,
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

const data: SubnameAcceptResponse = await response.json();
refetchSubnames();
return data;
refetchInvitations();

return accepted;
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useQueryClient,
} from '@tanstack/react-query';
import { SubnameGetAllByAddressResponse } from '@justaname.id/sdk';
import { buildSubnameBySubnameKey } from './useSubname';

/**
* Constructs a unique cache key for storing and retrieving subnames data associated with a wallet address.
Expand Down Expand Up @@ -82,7 +83,7 @@ export const useAccountSubnames = (

subnames?.forEach((subname: SubnameGetAllByAddressResponse) => {
queryClient.setQueryData(
buildAccountSubnamesKey(subname.subname),
buildSubnameBySubnameKey(subname.subname),
subname
);
});
Expand Down
7 changes: 2 additions & 5 deletions packages/@justaname.id/react/src/lib/hooks/useAddSubname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export interface UseAddSubname<T = any> {
export const useAddSubname = <T = any>(): UseAddSubname<T> => {
const { backendUrl, routes } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes.requestChallengeRoute,
});
const { getSignature } = useSubnameSignature();
const { refetchSubnames } = useAccountSubnames();

const mutate = useMutation<
Expand All @@ -53,7 +50,7 @@ export const useAddSubname = <T = any>(): UseAddSubname<T> => {

const signature = await getSignature();

const response = await fetch(backendUrl + routes.addSubnameRoute, {
const response = await fetch((backendUrl ?? "") + routes.addSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
83 changes: 37 additions & 46 deletions packages/@justaname.id/react/src/lib/hooks/useCommunitySubnames.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,57 @@
import { SubnameGetAllByDomainChainIdResponse } from '@justaname.id/sdk';
import {
QueryObserverResult,
RefetchOptions,
useQuery,
} from '@tanstack/react-query';
import { InfiniteData, useInfiniteQuery, UseInfiniteQueryResult } from '@tanstack/react-query';
import { useJustaName } from '../providers';
import { SubnameGetAllByDomainChainIdResponse } from '@justaname.id/sdk';

export const buildCommunitySubnamesKey = (domainName: string | undefined) => [
'COMMUNITY_SUBNAMES_BY_ADDRESS',
domainName,
];
export interface UseCommunitySubnamesOptions {
ensDomain: string;
page?: number;
limit?: number;
isClaimed?: boolean;
isClaimed: boolean;
}

interface UseCommunitySubnamesResult {
communitySubnamesResponse: SubnameGetAllByDomainChainIdResponse;
isLoading: boolean;
refetchCommunitySubnames: (
options?: RefetchOptions | undefined
) => Promise<
QueryObserverResult<
SubnameGetAllByDomainChainIdResponse | undefined,
unknown
>
>;
}
export const useCommunitySubnames = (
props: UseCommunitySubnamesOptions
): UseCommunitySubnamesResult => {
): UseInfiniteQueryResult<InfiniteData<SubnameGetAllByDomainChainIdResponse, unknown>, Error> => {
const { justaname, chainId } = useJustaName();

const query = useQuery({
return useInfiniteQuery({
queryKey: buildCommunitySubnamesKey(props.ensDomain),
queryFn: async () =>
await justaname?.subnames.getCommunitySubnamesByDomain({
queryFn: ({
pageParam: { page, limit },
}) => {
return justaname?.subnames.getCommunitySubnamesByDomain({
ensDomain: props.ensDomain,
isClaimed: true,
coinType: 60,
isClaimed: props.isClaimed,
chainId: chainId,
}),
enabled: Boolean(props.ensDomain) && Boolean(justaname),
});
page,
limit,
})
},
initialPageParam: {
page: 1,
limit: 20,
},
getNextPageParam: (p) => {
if (p.pagination.hasNextPage) {
return {
page: p.pagination.nextPage ?? 1,
limit: 20,
};
}

return {
communitySubnamesResponse: query.data ?? {
subnames: [],
pagination: {
totalCount: 0,
page: 0,
limit: 0,
totalPages: 0,
nextPage: 0,
prevPage: 0,
hasNextPage: false,
hasPrevPage: false,
},
return undefined;
},
getPreviousPageParam: (p) => {
if (p.pagination.hasPrevPage) {
return {
page: p.pagination.prevPage ?? 1,
limit: 20,
};
}
return undefined;
},
isLoading: query.isLoading,
refetchCommunitySubnames: query.refetch,
};
enabled: Boolean(props.ensDomain) && Boolean(justaname),
});
};
11 changes: 9 additions & 2 deletions packages/@justaname.id/react/src/lib/hooks/useRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { useJustaName } from '../providers';
import { QueryObserverResult, RefetchOptions, useQuery } from '@tanstack/react-query';
import { ChainId, JustaName, SubnameRecordsResponse, SanitizedRecords, sanitizeRecords } from '@justaname.id/sdk';

export const buildRecordsBySubnameKey = (subname: string, chainId: number) => ['RECORDS_BY_SUBNAME', subname, chainId]
export const buildRecordsBySubnameKey = (
subname: string,
chainId: number
) => [
'RECORDS_BY_SUBNAME',
subname,
chainId
]


export const getSubnameDetails = async (fullName: string,
Expand Down Expand Up @@ -66,7 +73,7 @@ export const useRecords = (

const query = useQuery({
queryKey: buildRecordsBySubnameKey(props.fullName, chainId),
queryFn: () => justaname ? getSubnameDetails(props.fullName, justaname, chainId, props.providerUrl) : undefined,
queryFn: () => getSubnameDetails(props.fullName, justaname, chainId, props.providerUrl)
})

return {
Expand Down
36 changes: 14 additions & 22 deletions packages/@justaname.id/react/src/lib/hooks/useRejectSubname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useJustaName } from '../providers';
import { useAccountSubnames } from './useAccountSubnames';
import { useMountedAccount } from './useMountedAccount';
import { useSubnameSignature } from './useSubnameSignature';
import { useAccountInvitations } from './useAccountInvitations';

export interface BaseRejectSubnameRequest {
username: string;
Expand Down Expand Up @@ -35,12 +36,10 @@ export interface UseRejectSubname {
* @returns {UseRejectSubname} An object containing the `rejectSubname` async function to initiate the subname reject, and a boolean `rejectSubnamePending` indicating the mutation's pending state.
*/
export const useRejectSubname = (): UseRejectSubname => {
const { backendUrl, routes } = useJustaName();
const { justaname } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes.requestChallengeRoute,
});
const { refetchInvitations } = useAccountInvitations();
const { getSignature } = useSubnameSignature();
const { refetchSubnames } = useAccountSubnames();

const mutate = useMutation<
Expand All @@ -55,26 +54,19 @@ export const useRejectSubname = (): UseRejectSubname => {

const signature = await getSignature();

const response = await fetch(backendUrl + routes.rejectSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...params,
signature: signature.signature,
address: address,
message: signature.message,
}),
const accepted = await justaname.subnames.rejectSubname({
chainId: params.chainId,
ensDomain: params.ensDomain,
username: params.username,
}, {
xAddress: address,
xSignature: signature.signature,
xMessage: signature.message,
});

if (!response.ok) {
throw new Error('Network response was not ok');
}

const data: SubnameRejectResponse = await response.json();
refetchSubnames();
return data;
refetchInvitations();
return accepted;
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export interface UseRevokeSubname<T = any> {
export const useRevokeSubname = <T = any>(): UseRevokeSubname<T> => {
const { backendUrl, routes } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes.requestChallengeRoute,
});
const { getSignature } = useSubnameSignature();
const { refetchSubnames } = useAccountSubnames();

const mutate = useMutation<
Expand All @@ -57,7 +54,7 @@ export const useRevokeSubname = <T = any>(): UseRevokeSubname<T> => {

const signature = await getSignature();

const response = await fetch(backendUrl + routes.revokeSubnameRoute, {
const response = await fetch((backendUrl ?? "") + routes.revokeSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
Loading

0 comments on commit d2cf526

Please sign in to comment.