diff --git a/frontend/src/components/RobotAvatar/index.tsx b/frontend/src/components/RobotAvatar/index.tsx index bf9160c2c..93d06a501 100644 --- a/frontend/src/components/RobotAvatar/index.tsx +++ b/frontend/src/components/RobotAvatar/index.tsx @@ -3,9 +3,8 @@ import SmoothImage from 'react-smooth-image'; import { Avatar, Badge, Tooltip } from '@mui/material'; import { SendReceiveIcon } from '../Icons'; import placeholder from './placeholder.json'; -// import { robohash } from './RobohashGenerator'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; -import { roboidentitiesClient } from '../../services/Roboidentities'; +import { roboidentitiesClient } from '../../services/Roboidentities/Web'; interface Props { shortAlias?: string | undefined; diff --git a/frontend/src/models/Coordinator.model.ts b/frontend/src/models/Coordinator.model.ts index 6df6c5509..ef1abb19d 100644 --- a/frontend/src/models/Coordinator.model.ts +++ b/frontend/src/models/Coordinator.model.ts @@ -6,12 +6,11 @@ import { type Order, type Garage, } from '.'; -import { roboidentitiesClient } from '../services/Roboidentities'; +import { roboidentitiesClient } from '../services/Roboidentities/Web'; import { apiClient } from '../services/api'; import { validateTokenEntropy } from '../utils'; import { compareUpdateLimit } from './Limit.model'; import { defaultOrder } from './Order.model'; -// import { robohash } from '../components/RobotAvatar/RobohashGenerator'; export interface Contact { nostr?: string | undefined; diff --git a/frontend/src/models/Slot.model.ts b/frontend/src/models/Slot.model.ts index 47a21c80f..e9bf4c467 100644 --- a/frontend/src/models/Slot.model.ts +++ b/frontend/src/models/Slot.model.ts @@ -1,7 +1,6 @@ import { sha256 } from 'js-sha256'; import { Robot, type Order } from '.'; -// import { robohash } from '../components/RobotAvatar/RobohashGenerator'; -import { roboidentitiesClient } from '../services/Roboidentities'; +import { roboidentitiesClient } from '../services/Roboidentities/Web'; class Slot { constructor(token: string, shortAliases: string[], robotAttributes: Record) { diff --git a/frontend/src/services/Roboidentities/Native.ts b/frontend/src/services/Roboidentities/Native.ts new file mode 100644 index 000000000..250b9a81a --- /dev/null +++ b/frontend/src/services/Roboidentities/Native.ts @@ -0,0 +1,4 @@ +import RoboidentitiesClientNativeClient from './RoboidentitiesNativeClient'; +import { RoboidentitiesClient } from './type'; + +export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientNativeClient(); diff --git a/frontend/src/services/Roboidentities/RoboidentitiesNativeClient/index.ts b/frontend/src/services/Roboidentities/RoboidentitiesNativeClient/index.ts index be8c32525..85b55b8b5 100644 --- a/frontend/src/services/Roboidentities/RoboidentitiesNativeClient/index.ts +++ b/frontend/src/services/Roboidentities/RoboidentitiesNativeClient/index.ts @@ -1,4 +1,4 @@ -import { type RoboidentitiesClient } from '..'; +import { type RoboidentitiesClient } from '../type'; class RoboidentitiesNativeClient implements RoboidentitiesClient { private robonames: Record = {}; @@ -19,26 +19,24 @@ class RoboidentitiesNativeClient implements RoboidentitiesClient { } }; - public generateRobohash: (initialString: string, size: string) => Promise = async ( - initialString, - size, - ) => { - const key = `${initialString};${size === 'small' ? 80 : 256}`; + public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise = + async (initialString, size) => { + const key = `${initialString};${size === 'small' ? 80 : 256}`; - if (this.robohashes[key]) { - return this.robohashes[key]; - } else { - const response = await window.NativeRobosats?.postMessage({ - category: 'roboidentities', - type: 'robohash', - detail: key, - }); - const result = response ? Object.values(response)[0] : ''; - const image = `data:image/png;base64,${result}`; - this.robohashes[key] = image; - return image; - } - }; + if (this.robohashes[key]) { + return this.robohashes[key]; + } else { + const response = await window.NativeRobosats?.postMessage({ + category: 'roboidentities', + type: 'robohash', + detail: key, + }); + const result = response ? Object.values(response)[0] : ''; + const image = `data:image/png;base64,${result}`; + this.robohashes[key] = image; + return image; + } + }; } export default RoboidentitiesNativeClient; diff --git a/frontend/src/components/RobotAvatar/RobohashGenerator.ts b/frontend/src/services/Roboidentities/RoboidentitiesWebClient/RobohashGenerator.ts similarity index 100% rename from frontend/src/components/RobotAvatar/RobohashGenerator.ts rename to frontend/src/services/Roboidentities/RoboidentitiesWebClient/RobohashGenerator.ts diff --git a/frontend/src/services/Roboidentities/RoboidentitiesWebClient/index.ts b/frontend/src/services/Roboidentities/RoboidentitiesWebClient/index.ts index 643577b71..20e2e1375 100644 --- a/frontend/src/services/Roboidentities/RoboidentitiesWebClient/index.ts +++ b/frontend/src/services/Roboidentities/RoboidentitiesWebClient/index.ts @@ -1,21 +1,18 @@ -import { type RoboidentitiesClient } from '..'; -// import { generate_roboname } from 'robo-identities-wasm'; +import { type RoboidentitiesClient } from '../type'; +import { generate_roboname } from 'robo-identities-wasm'; +import { robohash } from './RobohashGenerator'; class RoboidentitiesClientWebClient implements RoboidentitiesClient { public generateRoboname: (initialString: string) => Promise = async (initialString) => { return new Promise(async (resolve, _reject) => { - // resolve(generate_roboname(initialString)) + resolve(generate_roboname(initialString)); }); }; - public generateRobohash: (initialString: string, size: string) => Promise = async ( - initialString, - size, - ) => { - return new Promise(async (resolve, _reject) => { - // resolve(generate_roboname(initialString)) - }); - }; + public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise = + async (initialString, size) => { + return robohash.generate(initialString, size); + }; } export default RoboidentitiesClientWebClient; diff --git a/frontend/src/components/RobotAvatar/robohash.worker.ts b/frontend/src/services/Roboidentities/RoboidentitiesWebClient/robohash.worker.ts similarity index 100% rename from frontend/src/components/RobotAvatar/robohash.worker.ts rename to frontend/src/services/Roboidentities/RoboidentitiesWebClient/robohash.worker.ts diff --git a/frontend/src/services/Roboidentities/Web.ts b/frontend/src/services/Roboidentities/Web.ts new file mode 100644 index 000000000..8730491c6 --- /dev/null +++ b/frontend/src/services/Roboidentities/Web.ts @@ -0,0 +1,4 @@ +import RoboidentitiesClientWebClient from './RoboidentitiesWebClient'; +import { RoboidentitiesClient } from './type'; + +export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientWebClient(); diff --git a/frontend/src/services/Roboidentities/index.ts b/frontend/src/services/Roboidentities/index.ts deleted file mode 100644 index b5568ddc0..000000000 --- a/frontend/src/services/Roboidentities/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import RoboidentitiesClientNativeClient from './RoboidentitiesNativeClient'; -import RoboidentitiesClientWebClient from './RoboidentitiesWebClient'; - -export interface RoboidentitiesClient { - generateRoboname: (initialString: string) => Promise; - generateRobohash: (initialString: string, size: string) => Promise; -} - -export const roboidentitiesClient: RoboidentitiesClient = - // If userAgent has "RoboSats", we assume the app is running inside of the - // react-native-web view of the RoboSats Android app. - window.navigator.userAgent.includes('robosats') - ? new RoboidentitiesClientNativeClient() - : new RoboidentitiesClientWebClient(); diff --git a/frontend/src/services/Roboidentities/type.ts b/frontend/src/services/Roboidentities/type.ts new file mode 100644 index 000000000..4a54ea996 --- /dev/null +++ b/frontend/src/services/Roboidentities/type.ts @@ -0,0 +1,4 @@ +export interface RoboidentitiesClient { + generateRoboname: (initialString: string) => Promise; + generateRobohash: (initialString: string, size: 'small' | 'large') => Promise; +} diff --git a/frontend/webpack.config.ts b/frontend/webpack.config.ts index d3625542f..bd52b05d7 100644 --- a/frontend/webpack.config.ts +++ b/frontend/webpack.config.ts @@ -56,6 +56,15 @@ const configMobile: Configuration = { async: true, }, }, + { + test: path.resolve(__dirname, 'src/services/Roboidentities/Web.ts'), + loader: 'file-replace-loader', + options: { + condition: 'if-replacement-exists', + replacement: path.resolve(__dirname, 'src/services/Roboidentities/Native.ts'), + async: true, + }, + }, { test: path.resolve(__dirname, 'src/components/RobotAvatar/placeholder.json'), loader: 'file-replace-loader',