-
Notifications
You must be signed in to change notification settings - Fork 3
/
toolbar.tsx
183 lines (164 loc) · 5.11 KB
/
toolbar.tsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use client";
import { ElementRef, useRef, useState } from "react";
import { ImageIcon, Smile, X } from "lucide-react";
import { useMutation } from "convex/react";
import TextareaAutosize from "react-textarea-autosize";
import { useCoverImage } from "@/hooks/use-cover-image";
import { Doc } from "@/convex/_generated/dataModel";
import { Button } from "@/components/ui/button";
import { api } from "@/convex/_generated/api";
import { IconPicker } from "./icon-picker";
interface ToolbarProps {
initialData: Doc<"documents">;
preview?: boolean;
}
export const Toolbar = ({ initialData, preview }: ToolbarProps) => {
const inputRef = useRef<ElementRef<"textarea">>(null);
const [isEditing, setIsEditing] = useState(false);
const [value, setValue] = useState(initialData.title);
const update = useMutation(api.documents.update);
const removeIcon = useMutation(api.documents.removeIcon);
const coverImage = useCoverImage();
const enableInput = () => {
if (preview) return;
setIsEditing(true);
setTimeout(() => {
setValue(initialData.title);
inputRef.current?.focus();
}, 0);
};
const disableInput = () => setIsEditing(false);
const onInput = (value: string) => {
setValue(value);
//onInput 함수 발동되고 데이터베이스랑 연결되며 update 시작
update({
id: initialData._id,
title: value || "Untitled",
});
};
const onKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === "Enter") {
//새로고침 방지
event.preventDefault();
disableInput();
}
};
const onIconSelect = (icon: string) => {
update({
id: initialData._id,
icon,
});
};
const onRemoveIcon = () => {
removeIcon({
id: initialData._id,
});
};
const contentSummary = async () => {
if(!!initialData.content) {
const contentString = JSON.parse(initialData.content).reduce(
(acc1: string, cur1: any) =>
(acc1 +=
cur1.content.reduce(
(acc2: string, cur2: any) => (acc2 += cur2.text),
''
) + '\n'),
''
);
let url = new URL('http://localhost:3000/api/openai');
let param = { content: contentString };
url.search = new URLSearchParams(param).toString();
const summaryResponse = await fetch(url);
const summaryJson = await summaryResponse.json();
const summary = summaryJson.result;
update({
id: initialData._id,
summary,
});
}
}
return (
<div className="pl-[54px] group relative">
{!!initialData.icon && !preview && (
<div className="flex items-center gap-x-2 group/icon pt-6">
<IconPicker onChange={onIconSelect}>
<p className="text-6xl hover:opacity-75 transition">
{initialData.icon}
</p>
</IconPicker>
<Button
onClick={onRemoveIcon}
className="rounded-full opacity-0 group-hover/icon:opacity-100 transition text-muted-foreground text-xs"
variant="outline"
size="icon"
>
<X className="h-4 w-4" />
</Button>
</div>
)}
{!!initialData.icon && preview && (
<p className="text-6xl pt-6">{initialData.icon}</p>
)}
<div className="opacity-0 group-hover:opacity-100 flex items-center gap-x-1 py-4">
{!initialData.icon && !preview && (
//image선택하는 창 여는걸 커스텀훅으로 개발
<IconPicker asChild onChange={onIconSelect}>
<Button
className="text-muted-foreground text-xs"
variant="outline"
size="sm"
>
<Smile className="h-4 w-4 mr-2" />
Add icon
</Button>
</IconPicker>
)}
{!initialData.coverImage && !preview && (
<Button
onClick={coverImage.onOpen}
className="text-muted-foreground text-xs"
variant="outline"
size="sm"
>
<ImageIcon className="h-4 w-4 mr-2" />
Add cover
</Button>
)}
{!preview && (
<Button
onClick={contentSummary}
className="text-muted-foreground text-xs"
variant="outline"
size="sm"
>
AI summary
</Button>
)}
</div>
{isEditing && !preview ? (
<TextareaAutosize
ref={inputRef}
onBlur={disableInput}
onKeyDown={onKeyDown}
value={value}
onChange={(e) => onInput(e.target.value)}
className="text-5xl bg-transparent font-bold break-words outline-none text-[#3F3F3F] dark:text-[#CFCFCF] resize-none"
/>
) : (
<div
onClick={enableInput}
className="pb-[11.5px] text-5xl font-bold break-words outline-none text-[#3F3F3F] dark:text-[#CFCFCF]"
>
{initialData.title}
</div>
)}
{!!initialData.summary && (
<div>
<span>{initialData.summary}</span>
<br></br>
<br></br>
</div>
)}
</div>
);
};