aboutsummaryrefslogtreecommitdiffstats
path: root/packages/renderer/src/components/locationBar/UrlOverlay.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/renderer/src/components/locationBar/UrlOverlay.tsx')
-rw-r--r--packages/renderer/src/components/locationBar/UrlOverlay.tsx61
1 files changed, 52 insertions, 9 deletions
diff --git a/packages/renderer/src/components/locationBar/UrlOverlay.tsx b/packages/renderer/src/components/locationBar/UrlOverlay.tsx
index d590709..a71fa4e 100644
--- a/packages/renderer/src/components/locationBar/UrlOverlay.tsx
+++ b/packages/renderer/src/components/locationBar/UrlOverlay.tsx
@@ -22,7 +22,47 @@ import { styled } from '@mui/material/styles';
22import React from 'react'; 22import React from 'react';
23 23
24import getAlertColor from './getAlertColor'; 24import getAlertColor from './getAlertColor';
25import type { SplitResult } from './splitUrl'; 25
26export type SplitResult =
27 | {
28 type: 'hostOnly';
29 prefix: string;
30 host: string;
31 suffix: string;
32 }
33 | {
34 type: 'wholeString';
35 value: string;
36 }
37 | {
38 type: 'empty';
39 };
40
41function splitUrl(urlString: string | undefined): SplitResult {
42 if (urlString === undefined || urlString === '') {
43 return { type: 'empty' };
44 }
45 let url: URL;
46 try {
47 url = new URL(urlString);
48 } catch {
49 return { type: 'wholeString', value: urlString };
50 }
51 const { protocol, host, username, password, pathname, search, hash } = url;
52 if (host !== '') {
53 return {
54 type: 'hostOnly',
55 prefix: `${protocol}//${
56 username === ''
57 ? ''
58 : `${username}${password === '' ? '' : `:${password}`}@`
59 }`,
60 host,
61 suffix: `${pathname}${search}${hash}`,
62 };
63 }
64 return { type: 'wholeString', value: urlString };
65}
26 66
27const PrimaryFragment = styled('span', { 67const PrimaryFragment = styled('span', {
28 name: 'LocationOverlayInput', 68 name: 'LocationOverlayInput',
@@ -40,28 +80,31 @@ const SecondaryFragment = styled('span', {
40})); 80}));
41 81
42export default function UrlOverlay({ 82export default function UrlOverlay({
43 splitResult, 83 url,
84 alert,
44}: { 85}: {
45 splitResult: SplitResult; 86 url: string | undefined;
87 alert: boolean;
46}): JSX.Element { 88}): JSX.Element {
89 const splitResult = splitUrl(url);
47 const { type } = splitResult; 90 const { type } = splitResult;
48 switch (type) { 91 switch (type) {
49 case 'valid': { 92 case 'hostOnly': {
50 const { secure, prefix, host, suffix } = splitResult; 93 const { prefix, host, suffix } = splitResult;
51 return ( 94 return (
52 <> 95 <>
53 <SecondaryFragment>{prefix}</SecondaryFragment> 96 <SecondaryFragment>{prefix}</SecondaryFragment>
54 <PrimaryFragment alert={!secure}>{host}</PrimaryFragment> 97 <PrimaryFragment alert={alert}>{host}</PrimaryFragment>
55 <SecondaryFragment>{suffix}</SecondaryFragment> 98 <SecondaryFragment>{suffix}</SecondaryFragment>
56 </> 99 </>
57 ); 100 );
58 } 101 }
59 case 'invalid': { 102 case 'wholeString': {
60 const { value } = splitResult; 103 const { value } = splitResult;
61 return <PrimaryFragment alert>{value}</PrimaryFragment>; 104 return <PrimaryFragment alert={alert}>{value}</PrimaryFragment>;
62 } 105 }
63 case 'empty': 106 case 'empty':
64 return <PrimaryFragment alert={false} />; 107 return <PrimaryFragment alert={alert} />;
65 default: 108 default:
66 /* eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- 109 /* eslint-disable-next-line @typescript-eslint/restrict-template-expressions --
67 Error handling for impossible case. 110 Error handling for impossible case.