-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNewBookmarkButton.tsx
More file actions
99 lines (89 loc) · 2.41 KB
/
NewBookmarkButton.tsx
File metadata and controls
99 lines (89 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { classes, properties, useResource, useTitle } from '@tomic/react';
import React, { FormEvent, useCallback, useState } from 'react';
import { Button } from '../Button';
import {
Dialog,
DialogActions,
DialogContent,
DialogTitle,
useDialog,
} from '../Dialog';
import Field from '../forms/Field';
import { InputStyled, InputWrapper } from '../forms/InputStyles';
import { Base } from './Base';
import { useCreateAndNavigate } from './useCreateAndNavigate';
import { NewInstanceButtonProps } from './NewInstanceButtonProps';
function normalizeWebAddress(url: string) {
if (/^[http://|https://]/i.test(url)) {
return url;
}
return `https://${url}`;
}
export function NewBookmarkButton({
klass,
subtle,
icon,
IconComponent,
parent,
children,
label,
}: NewInstanceButtonProps): JSX.Element {
const resource = useResource(klass);
const [title] = useTitle(resource);
const [url, setUrl] = useState('');
const { dialogProps, show, close } = useDialog();
const createResourceAndNavigate = useCreateAndNavigate(klass, parent);
const onDone = useCallback(
(e: FormEvent) => {
e.preventDefault();
const normalizedUrl = normalizeWebAddress(url);
createResourceAndNavigate('bookmark', {
[properties.name]: 'New Bookmark',
[properties.bookmark.url]: normalizedUrl,
[properties.isA]: [classes.bookmark],
});
},
[url],
);
return (
<>
<Base
onClick={show}
title={title}
icon={icon}
IconComponent={IconComponent}
subtle={subtle}
label={label}
>
{children}
</Base>
<Dialog {...dialogProps}>
<DialogTitle>
<h1>New Bookmark</h1>
</DialogTitle>
<DialogContent>
<form onSubmit={onDone}>
<Field required label='url'>
<InputWrapper>
<InputStyled
placeholder='https://example.com'
value={url}
autoFocus={true}
onChange={e => setUrl(e.target.value)}
/>
</InputWrapper>
</Field>
</form>
</DialogContent>
<DialogActions>
<Button onClick={close} subtle>
Cancel
</Button>
<Button onClick={onDone} disabled={url.trim() === ''}>
Ok
</Button>
</DialogActions>
</Dialog>
</>
);
}