Skip to content

Commit

Permalink
feat: add useConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
helciofranco committed Jan 9, 2025
1 parent 1827973 commit f3996ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
16 changes: 2 additions & 14 deletions packages/app/src/systems/CRX/hooks/useCurrentTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ export function useCurrentTab() {

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const currentTab = tabs[0];

if (currentTab?.url) {
const faviconUrl = currentTab.favIconUrl;
setFaviconUrl(faviconUrl);

try {
const url = new URL(currentTab.url);
const port = url.port ? `:${url.port}` : '';
const result = `${url.protocol}//${url.hostname}${port}`;
setOrigin(result);
} catch (_e) {
setOrigin(undefined);
}
}
setFaviconUrl(currentTab?.favIconUrl);
setOrigin(currentTab?.url);
});
}, []);

Expand Down
34 changes: 34 additions & 0 deletions packages/app/src/systems/DApp/hooks/useConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Connection } from '@fuel-wallet/types';
import { useEffect, useState } from 'react';
import { ConnectionService } from '../services';

interface UseConnectionProps {
url: string | undefined;
}

const parseUrl = (url: string): string | undefined => {
try {
const { protocol, hostname, port } = new URL(url);
return `${protocol}//${hostname}${port ? `:${port}` : ''}`;
} catch (_e) {
return undefined;
}
};

export const useConnection = ({ url = '' }: UseConnectionProps) => {
const [connection, setConnection] = useState<Connection | undefined>();

useEffect(() => {
const fetchConnection = async () => {
const origin = parseUrl(url);
const existingConnection = await ConnectionService.getConnection(origin);
setConnection(existingConnection);
};

fetchConnection();
}, [url]);

return {
connection,
};
};

0 comments on commit f3996ba

Please sign in to comment.