Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #88

Merged
merged 11 commits into from
Jan 10, 2025
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@
},
"projectsRelationship": "independent",
"projects": [
"packages/@justaname.id/sdk",
"packages/@justaname.id/react",
"packages/@justaname.id/sdk",
"packages/@justaname.id/siwens",
"packages/@justweb3/ui",
"packages/@justweb3/widget",
Expand Down
50 changes: 50 additions & 0 deletions packages/@justaname.id/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
## 0.3.162 (2024-12-12)


### 🧱 Updated Dependencies

- Updated @justaname.id/sdk to 0.2.159

## 0.3.161 (2024-12-11)


### 🧱 Updated Dependencies

- Updated @justaname.id/sdk to 0.2.158

## 0.3.160 (2024-12-11)


### 🧱 Updated Dependencies

- Updated @justaname.id/sdk to 0.2.157

## 0.3.159 (2024-12-11)


### 🚀 Features

- basic but need to move to js sdk ([55edbf2](https://github.com/JustaName-id/JustaName-sdk/commit/55edbf2))

- compute height for message, using justaname react hook for identity resolution and sign in fix ([bafad7b](https://github.com/JustaName-id/JustaName-sdk/commit/bafad7b))

- chat from profile ([fb31ee7](https://github.com/JustaName-id/JustaName-sdk/commit/fb31ee7))

- console ([3ade8be](https://github.com/JustaName-id/JustaName-sdk/commit/3ade8be))

- xmtp fix nextjs config ([3900db0](https://github.com/JustaName-id/JustaName-sdk/commit/3900db0))

- chat optimization ([4a3af5e](https://github.com/JustaName-id/JustaName-sdk/commit/4a3af5e))

- siwens external package ([46e23cc](https://github.com/JustaName-id/JustaName-sdk/commit/46e23cc))


### 🧱 Updated Dependencies

- Updated @justaname.id/sdk to 0.2.156


### ❤️ Thank You

- HadiKhai

## 0.3.158 (2024-12-03)


Expand Down
4 changes: 2 additions & 2 deletions packages/@justaname.id/react/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@justaname.id/react",
"version": "0.3.158",
"version": "0.3.162",
"dependencies": {
"@ensdomains/ensjs": "4.0.2",
"@justaname.id/sdk": "0.2.155",
"@justaname.id/sdk": "0.2.159",
"axios": "^1.6.0",
"qs": "^6.12.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './usePrimaryName';
export * from './usePrimaryNameBatch';
export * from './useSetPrimaryName';

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address } from 'viem';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { getName } from '@ensdomains/ensjs/public';
import { ChainId } from '@justaname.id/sdk';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Address } from 'viem';
import { useJustaName } from '../../providers';
import { useEnsPublicClient } from '../client/useEnsPublicClient';
import { defaultOptions } from '../../query';
import { getName } from '@ensdomains/ensjs/public';
import { useEnsPublicClient } from '../client/useEnsPublicClient';
import { PrimaryNameTaskQueue } from './primary-name-task-queue';
import { buildPrimaryNameBatchKey } from './usePrimaryNameBatch';

Expand All @@ -17,6 +17,7 @@ export interface UsePrimaryNameParams {
address?: string;
chainId?: ChainId;
enabled?: boolean;
priority?: "onChain" | "offChain";
}

export interface UsePrimaryNameResult {
Expand All @@ -40,26 +41,44 @@ export const usePrimaryName = (
params?: UsePrimaryNameParams
): UsePrimaryNameResult => {
const { chainId, justaname } = useJustaName();
const _priority = params?.priority || 'offChain';
const _enabled = params?.enabled !== undefined ? params.enabled : true;
const _chainId = params?.chainId || chainId;
const { ensClient } = useEnsPublicClient({
chainId: _chainId,
});
const queryClient = useQueryClient();

const getPrimaryName = async (
const getOnChainPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {

if (!ensClient) {
throw new Error('Public client not found');
}

if (!params?.address) {
throw new Error('Address is required');
}
const taskFn = () => {
return getName(ensClient, {
address: params?.address as Address,
});
};
const reverseResolution = await PrimaryNameTaskQueue.enqueue(taskFn);
if (reverseResolution && reverseResolution?.name) {
return reverseResolution.name;
}
return '';
}

let name = '';


const getOffChainPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {
if (!params?.address) {
throw new Error('Address is required');
}
const primaryNames = queryClient.getQueryData(
buildPrimaryNameBatchKey(_chainId)
) as Record<string, string>;
Expand All @@ -76,25 +95,35 @@ export const usePrimaryName = (
chainId: _chainId,
});
if (primaryNameGetByAddressResponse.name) {
name = primaryNameGetByAddressResponse.name;
} else {
const taskFn = () => {
if (!params?.address) {
throw new Error('Address is required');
}
return getName(ensClient, {
address: params?.address as Address,
});
};

const reverseResolution = await PrimaryNameTaskQueue.enqueue(taskFn);

if (reverseResolution && reverseResolution?.name) {
name = reverseResolution.name;
return primaryNameGetByAddressResponse.name;
}else{
return '';
}
}


const getPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {
let name = '';
if(_priority === 'offChain'){
name = await getOffChainPrimaryName(_params);
if(name.length > 0){
return name;
}else{
name = await getOnChainPrimaryName(_params);
return name;
}
}else{
name = await getOnChainPrimaryName(_params);
if(name.length > 0){
return name;
}else{
name = await getOffChainPrimaryName(_params);
return name;
}
return name;
};
}
};

const query = useQuery({
...defaultOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use client';

import { SetPrimaryNameRoute } from '@justaname.id/sdk';
import { UseMutateAsyncFunction, useMutation } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useJustaName, useSubnameSignature } from '../../providers';
import { useMountedAccount } from '../account/useMountedAccount';
import { usePrimaryName } from './usePrimaryName';

export interface UseSetPrimaryNameFunctionParams {
name: string;
}

export type UseSetPrimaryNameParams = Omit<
SetPrimaryNameRoute['params'],
'name'
>;

export interface UseSetPrimaryNameResult {
setPrimaryName: UseMutateAsyncFunction<
SetPrimaryNameRoute['response'],
Error,
UseSetPrimaryNameFunctionParams
>;
isSetPrimaryNamePending: boolean;
}

export const useSetPrimaryName = (
params?: UseSetPrimaryNameParams
): UseSetPrimaryNameResult => {
const { justaname, chainId } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature();
const {refetchPrimaryName} = usePrimaryName({
address: address || '',
enabled: !!address,
});
const _chainId = useMemo(
() => params?.chainId || chainId,
[params?.chainId, chainId]
);

const mutate = useMutation({
mutationFn: async (_params: UseSetPrimaryNameFunctionParams) => {
if (!address) {
throw new Error('No address found');
}

const _name = _params.name;
const signature = await getSignature();
const primaryNameSet = await justaname.subnames.setPrimaryName(
{
address,
name: _name,
chainId: _chainId,
},
{
xAddress: address,
xSignature: signature.signature,
xMessage: signature.message,
}
);
refetchPrimaryName();
return primaryNameSet;
},
});

return {
setPrimaryName: mutate.mutateAsync,
isSetPrimaryNamePending: mutate.isPending,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const useUploadMedia = (
// const baseUrl = 'http://localhost:3000';
const baseUrl = dev
? 'https://api-staging.justaname.id'
: 'http://localhost:3000';
: 'https://api.justaname.id';
const result = await axios.post<{
result: {
data: {
Expand Down
40 changes: 40 additions & 0 deletions packages/@justaname.id/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
## 0.2.159 (2024-12-12)


### 🧱 Updated Dependencies

- Updated @justaname.id/siwens to 0.0.92

## 0.2.158 (2024-12-11)


### 🧱 Updated Dependencies

- Updated @justaname.id/siwens to 0.0.91

## 0.2.157 (2024-12-11)


### 🧱 Updated Dependencies

- Updated @justaname.id/siwens to 0.0.90

## 0.2.156 (2024-12-11)


### 🚀 Features

- chat from profile ([fb31ee7](https://github.com/JustaName-id/JustaName-sdk/commit/fb31ee7))

- chat optimization ([4a3af5e](https://github.com/JustaName-id/JustaName-sdk/commit/4a3af5e))


### 🧱 Updated Dependencies

- Updated @justaname.id/siwens to 0.0.89


### ❤️ Thank You

- HadiKhai

## 0.2.155 (2024-12-03)


Expand Down
4 changes: 2 additions & 2 deletions packages/@justaname.id/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@justaname.id/sdk",
"version": "0.2.155",
"version": "0.2.159",
"dependencies": {
"@justaname.id/siwens": "0.0.88",
"@justaname.id/siwens": "0.0.92",
"axios": "^1.6.0",
"qs": "^6.12.0"
},
Expand Down
12 changes: 10 additions & 2 deletions packages/@justaname.id/sdk/src/lib/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ import {
MAPP_REVOKE_PERMISSION_ROUTE,
} from './mapp';
import { GET_ALL_OFFCHAIN_RESOLVERS_ROUTE } from './offchain-resolver';
import { GET_PRIMARY_NAME_BY_ADDRESS_ROUTE } from './primary-name';
import { PrimaryNameGetByAddressRoute } from '../../types/primary-name';
import {
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE,
SET_PRIMARY_NAME_ROUTE,
} from './primary-name';
import {
PrimaryNameGetByAddressRoute,
SetPrimaryNameRoute,
} from '../../types/primary-name';

export interface ROUTES {
SIWE_VERIFY_MESSAGE_ROUTE: VerifyMessageRoute;
Expand All @@ -79,6 +85,7 @@ export interface ROUTES {
GET_ALL_ENS_WITH_COUNT_ROUTE: SubnameGetAllByEnsDomainWithCountRoute;
GET_ALL_OFFCHAIN_RESOLVERS_ROUTE: OffchainResolversGetAllRoute;
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE: PrimaryNameGetByAddressRoute;
SET_PRIMARY_NAME_ROUTE: SetPrimaryNameRoute;
}

export const Routes: Record<keyof ROUTES, string> = {
Expand Down Expand Up @@ -106,4 +113,5 @@ export const Routes: Record<keyof ROUTES, string> = {
GET_ALL_ENS_WITH_COUNT_ROUTE,
GET_ALL_OFFCHAIN_RESOLVERS_ROUTE,
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE,
SET_PRIMARY_NAME_ROUTE,
};
3 changes: 3 additions & 0 deletions packages/@justaname.id/sdk/src/lib/api/routes/primary-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export const PRIMARY_NAME_BASE_ROUTE = GLOBAL_PREFIX + '/ens/v1/primary-name';

export const GET_PRIMARY_NAME_BY_ADDRESS_ROUTE =
PRIMARY_NAME_BASE_ROUTE + '/address';

export const SET_PRIMARY_NAME_ROUTE =
PRIMARY_NAME_BASE_ROUTE + '/set-primary-name';
Loading
Loading