diff --git a/package.json b/package.json index c4c29051..413b8fd1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "intuition-chrome-extension", "displayName": "Intuition Chrome Extension", - "version": "0.1.44", + "version": "0.1.45", "description": "", "author": "THP-Lab.org", "scripts": { diff --git a/src/components/AtomForm.tsx b/src/components/AtomForm.tsx index d0184704..b81c772c 100644 --- a/src/components/AtomForm.tsx +++ b/src/components/AtomForm.tsx @@ -71,22 +71,37 @@ const AtomForm = forwardRef(function AtomForm( } }, [description]) - useEffect(() => { - if (!rawUrl) return; - - try { - const input = /^https?:\/\//i.test(rawUrl) ? rawUrl : `https://${rawUrl}`; - const parsed = new URL(input); +useEffect(() => { + if (!rawUrl) return; + + try { + const input = /^https?:\/\//i.test(rawUrl) ? rawUrl : `https://${rawUrl}`; + const parsed = new URL(input); + + const hostnameWhithoutWWW = parsed.hostname.replace(/^www\./i, ''); + + let candidate: string; + if (linkType === "domain") { + candidate = `${parsed.protocol}//${hostnameWhithoutWWW}`; + } else { + candidate = + `${parsed.protocol}//${hostnameWhithoutWWW}` + + `${parsed.pathname}${parsed.search}${parsed.hash}`; + } - if (linkType === "domain") { - setUrl(`https://${parsed.hostname}/`); - } else { - setUrl(parsed.href); - } - } catch { - setUrl(rawUrl); + if (candidate.endsWith("/") && !candidate.match(/^https?:\/\/[^/]+\/$/)) { + candidate = candidate.slice(0, -1); } - }, [rawUrl, linkType]); + + setUrl(candidate); + } catch { + let fallback = rawUrl + .replace(/^https?:\/\/www\./i, match => match.replace(/www\./i, "")) + .replace(/\/$/, ""); + + setUrl(fallback); + } +}, [rawUrl, linkType]); async function handleSubmit(e: React.FormEvent) { diff --git a/src/graphql/src/generated/index.ts b/src/graphql/src/generated/index.ts index 75357c18..5bbbe816 100644 --- a/src/graphql/src/generated/index.ts +++ b/src/graphql/src/generated/index.ts @@ -11205,7 +11205,7 @@ export type GetClaimsByAddressQuery = { } export type GetClaimsByUriQueryVariables = Exact<{ - uri?: InputMaybe + uriRegex?: InputMaybe address?: InputMaybe }> @@ -15335,9 +15335,9 @@ useGetClaimsByAddressQuery.fetcher = ( ) export const GetClaimsByUriDocument = ` - query GetClaimsByUri($uri: String, $address: String) { + query GetClaimsByUri($uriRegex: String, $address: String) { atoms( - where: {_or: [{data: {_eq: $uri}}, {value: {thing: {url: {_eq: $uri}}}}, {value: {person: {url: {_eq: $uri}}}}, {value: {organization: {url: {_eq: $uri}}}}, {value: {book: {url: {_eq: $uri}}}}]} + where: {_or: [{data: {_iregex: $uriRegex}}, {value: {thing: {url: {_iregex: $uriRegex}}}}, {value: {person: {url: {_iregex: $uriRegex}}}}, {value: {organization: {url: {_iregex: $uriRegex}}}}, {value: {book: {url: {_iregex: $uriRegex}}}}]} ) { as_object_claims_aggregate { aggregate { @@ -25906,7 +25906,10 @@ export const GetClaimsByUri = { variableDefinitions: [ { kind: "VariableDefinition", - variable: { kind: "Variable", name: { kind: "Name", value: "uri" } }, + variable: { + kind: "Variable", + name: { kind: "Name", value: "uriRegex" } + }, type: { kind: "NamedType", name: { kind: "Name", value: "String" } } }, { @@ -25948,10 +25951,13 @@ export const GetClaimsByUri = { fields: [ { kind: "ObjectField", - name: { kind: "Name", value: "_eq" }, + name: { kind: "Name", value: "_iregex" }, value: { kind: "Variable", - name: { kind: "Name", value: "uri" } + name: { + kind: "Name", + value: "uriRegex" + } } } ] @@ -25987,13 +25993,13 @@ export const GetClaimsByUri = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -26036,13 +26042,13 @@ export const GetClaimsByUri = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -26088,13 +26094,13 @@ export const GetClaimsByUri = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -26137,13 +26143,13 @@ export const GetClaimsByUri = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } diff --git a/src/graphql/src/generated/subscriptions.ts b/src/graphql/src/generated/subscriptions.ts index 7893be46..d329be8a 100644 --- a/src/graphql/src/generated/subscriptions.ts +++ b/src/graphql/src/generated/subscriptions.ts @@ -11225,7 +11225,7 @@ export type GetClaimsByAddressQuery = { } export type GetClaimsByUriQueryVariables = Exact<{ - uri?: InputMaybe + uriRegex?: InputMaybe address?: InputMaybe }> @@ -21535,7 +21535,10 @@ export const GetClaimsByUriDocument = { variableDefinitions: [ { kind: "VariableDefinition", - variable: { kind: "Variable", name: { kind: "Name", value: "uri" } }, + variable: { + kind: "Variable", + name: { kind: "Name", value: "uriRegex" } + }, type: { kind: "NamedType", name: { kind: "Name", value: "String" } } }, { @@ -21577,10 +21580,13 @@ export const GetClaimsByUriDocument = { fields: [ { kind: "ObjectField", - name: { kind: "Name", value: "_eq" }, + name: { kind: "Name", value: "_iregex" }, value: { kind: "Variable", - name: { kind: "Name", value: "uri" } + name: { + kind: "Name", + value: "uriRegex" + } } } ] @@ -21616,13 +21622,13 @@ export const GetClaimsByUriDocument = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -21665,13 +21671,13 @@ export const GetClaimsByUriDocument = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -21717,13 +21723,13 @@ export const GetClaimsByUriDocument = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -21766,13 +21772,13 @@ export const GetClaimsByUriDocument = { kind: "ObjectField", name: { kind: "Name", - value: "_eq" + value: "_iregex" }, value: { kind: "Variable", name: { kind: "Name", - value: "uri" + value: "uriRegex" } } } @@ -22552,7 +22558,7 @@ export const GetClaimsByUriDocument = { * @example * const { data, loading, error } = useGetClaimsByUriQuery({ * variables: { - * uri: // value for 'uri' + * uriRegex: // value for 'uriRegex' * address: // value for 'address' * }, * }); diff --git a/src/graphql/src/queries/claims.graphql b/src/graphql/src/queries/claims.graphql index ed79091f..56f03af2 100644 --- a/src/graphql/src/queries/claims.graphql +++ b/src/graphql/src/queries/claims.graphql @@ -58,15 +58,15 @@ query GetClaimsByAddress($address: String) { } } -query GetClaimsByUri($uri: String, $address: String) { +query GetClaimsByUri($uriRegex: String, $address: String) { atoms( where: { _or: [ - { data: { _eq: $uri } } - { value: { thing: { url: { _eq: $uri } } } } - { value: { person: { url: { _eq: $uri } } } } - { value: { organization: { url: { _eq: $uri } } } } - { value: { book: { url: { _eq: $uri } } } } + { data: { _iregex: $uriRegex } } + { value: { thing: { url: { _iregex: $uriRegex } } } } + { value: { person: { url: { _iregex: $uriRegex } } } } + { value: { organization: { url: { _iregex: $uriRegex } } } } + { value: { book: { url: { _iregex: $uriRegex } } } } ] } ) { diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 74d3ff45..169d48b7 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -9,6 +9,34 @@ import ClaimRowLite from "~src/components/ui/ClaimRowLite"; import AtomCard from "~src/components/AtomCard"; import EyeComponent from "~/src/components/3D/EyeComponent" +function normalizeUrl(input: string): string { + try { + const u = new URL(input) + let hostname = u.hostname.toLowerCase() + if (hostname.startsWith("www.")) hostname = hostname.slice(4) + let pathname = u.pathname + if (pathname.endsWith("/") && pathname.length > 1) { + pathname = pathname.slice(0, -1) + } + return `https://${hostname}${pathname}${u.search}${u.hash}` + } catch { + return input + } +} + +function buildUriRegex(rawUrl: string): string { + const canonical = normalizeUrl(rawUrl) + let withoutProto = canonical.replace(/^https?:\/\//, "") + + if (withoutProto.endsWith("/")) { + withoutProto = withoutProto.slice(0, -1) + } + const escaped = withoutProto.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + + return `^https?:\\/\\/(?:www\\.)?${escaped}\\/?$` +} + + function Home() { const { theme } = useTheme() const [currentUrl, setCurrentUrl] = useState("") @@ -25,21 +53,34 @@ function Home() { console.log(tab.url) return tab.url } + const refreshUrl = () => { - getCurrentUrl().then((url) => setCurrentUrl(url || "")) - } - useEffect(() => { - refreshUrl() - chrome.tabs.onUpdated.addListener(() => { - refreshUrl() + getCurrentUrl().then((url) => { + if (url) { + setCurrentUrl(normalizeUrl(url)) + } else { + setCurrentUrl("") + } }) + } - chrome.tabs.onActivated.addListener(() => { - refreshUrl() + useEffect(() => { + getCurrentUrl().then((url) => { + if (url) setCurrentUrl(normalizeUrl(url)) }) + chrome.tabs.onUpdated.addListener(refreshUrl) + chrome.tabs.onActivated.addListener(refreshUrl) + return () => { + chrome.tabs.onUpdated.removeListener(refreshUrl) + chrome.tabs.onActivated.removeListener(refreshUrl) + } }, []) - const { data, isLoading, error } = useGetClaimsByUriQuery({uri: currentUrl, address: walletAddress }) + const uriRegex = buildUriRegex(currentUrl) + console.log("normalized URL:", currentUrl) + console.log("uriRegex:", uriRegex) + + const { data, isLoading, error } = useGetClaimsByUriQuery({uriRegex, address: walletAddress }) const atoms = data?.atoms ?? [] console.log("current wallet address:", walletAddress); console.log("Data :", data)