From f2176a6b310c71a1b2d24f9cbf764f98adce3672 Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Sun, 30 Aug 2020 21:36:20 +0300 Subject: [PATCH] Improve entry domain processing --- src/components/vault/Entry.js | 16 +++++++++++++--- src/components/vault/utils/domain.js | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/components/vault/utils/domain.js diff --git a/src/components/vault/Entry.js b/src/components/vault/Entry.js index 0025915..d1a436c 100644 --- a/src/components/vault/Entry.js +++ b/src/components/vault/Entry.js @@ -2,16 +2,26 @@ import React, { useContext, useState, useRef, useEffect } from 'react'; import styled, { css } from 'styled-components'; import PropTypes from 'prop-types'; import { Colors, Text, Classes, Menu, MenuItem, ContextMenu, MenuDivider } from '@blueprintjs/core'; -import extractDomain from 'extract-domain'; -import { DEFAULT_ENTRY_TYPE, EntryType } from 'buttercup/web'; +import { + DEFAULT_ENTRY_TYPE, + EntryType, + EntryURLType, + fieldsToProperties, + getEntryURLs +} from 'buttercup/web'; import { EntryFacade } from './props'; import { getFacadeField, getThemeProp } from '../../utils'; import SiteIcon from './SiteIcon'; import { useGroups } from './hooks/vault'; import { VaultContext } from './Vault'; +import { extractDomain } from './utils/domain'; function getEntryDomain(entry) { - const url = getFacadeField(entry, 'url'); + const properties = fieldsToProperties(entry.fields); + const [url] = [ + ...getEntryURLs(properties, EntryURLType.Icon), + ...getEntryURLs(properties, EntryURLType.Any) + ]; return url ? extractDomain(url) : null; } diff --git a/src/components/vault/utils/domain.js b/src/components/vault/utils/domain.js new file mode 100644 index 0000000..fb1d8e3 --- /dev/null +++ b/src/components/vault/utils/domain.js @@ -0,0 +1,7 @@ +export function extractDomain(str) { + const domainMatch = str.match(/^https?:\/\/([^\/]+)/i); + if (!domainMatch) return str; + const [, domainPortion] = domainMatch; + const [domain] = domainPortion.split(':'); + return domain; +}