forked from microsoft/CopilotStudioSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.ts
More file actions
201 lines (176 loc) · 6.64 KB
/
Copy pathbubble.ts
File metadata and controls
201 lines (176 loc) · 6.64 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type { CopilotChatConfig } from './config'
import { DEFAULT_CONFIG } from './config'
import { initChat, type ChatHandle } from './chat'
const CHAT_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>`
const CLOSE_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>`
const REFRESH_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>`
function headerButton(innerHTML: string, ariaLabel: string): HTMLButtonElement {
const btn = document.createElement('button')
btn.innerHTML = innerHTML
btn.setAttribute('aria-label', ariaLabel)
Object.assign(btn.style, {
background: 'none',
border: 'none',
color: 'white',
cursor: 'pointer',
padding: '4px',
display: 'flex',
alignItems: 'center',
borderRadius: '4px',
})
btn.addEventListener('mouseenter', () => { btn.style.background = 'rgba(255,255,255,0.15)' })
btn.addEventListener('mouseleave', () => { btn.style.background = 'none' })
return btn
}
export function createBubble(container: HTMLElement, config: CopilotChatConfig): void {
const c = { ...DEFAULT_CONFIG, ...config }
// Wrapper — fixed position, holds bubble + panel
const wrapper = document.createElement('div')
wrapper.setAttribute('data-copilot-chat', 'root')
Object.assign(wrapper.style, {
position: 'fixed',
[c.position === 'bottom-left' ? 'left' : 'right']: '20px',
bottom: '20px',
zIndex: String(c.zIndex),
fontFamily: `'Source Sans Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif`,
})
// Panel
const panel = document.createElement('div')
Object.assign(panel.style, {
display: 'none',
flexDirection: 'column',
width: c.panelWidth,
height: c.panelHeight,
maxHeight: 'calc(100vh - 120px)',
borderRadius: '8px',
overflow: 'hidden',
boxShadow: '0 4px 16px rgba(0,0,0,0.12)',
background: '#fff',
})
// Header
const header = document.createElement('div')
Object.assign(header.style, {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
padding: '12px 16px',
background: c.headerColor,
color: 'white',
fontSize: '14px',
fontWeight: '600',
flexShrink: '0',
})
const title = document.createElement('span')
title.textContent = c.headerTitle!
const headerActions = document.createElement('div')
Object.assign(headerActions.style, { display: 'flex', gap: '4px', alignItems: 'center' })
const refreshBtn = headerButton(REFRESH_ICON, 'New conversation')
const closeBtn = headerButton(CLOSE_ICON, 'Close chat')
headerActions.append(refreshBtn, closeBtn)
header.append(title, headerActions)
// Status bar
const statusBar = document.createElement('div')
Object.assign(statusBar.style, {
display: 'none',
padding: '8px 16px',
fontSize: '12px',
color: '#666',
background: '#f8f8f8',
borderBottom: '1px solid #eee',
flexShrink: '0',
})
// WebChat container
const webchatContainer = document.createElement('div')
Object.assign(webchatContainer.style, {
flex: '1',
overflow: 'hidden',
minHeight: '0',
})
panel.append(header, statusBar, webchatContainer)
// Bubble button
const bubble = document.createElement('button')
bubble.innerHTML = CHAT_ICON
bubble.setAttribute('aria-label', 'Open chat')
Object.assign(bubble.style, {
width: '56px',
height: '56px',
borderRadius: '50%',
background: c.bubbleColor,
color: 'white',
border: 'none',
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
transition: 'transform 0.2s, box-shadow 0.2s',
})
bubble.addEventListener('mouseenter', () => {
bubble.style.transform = 'scale(1.05)'
bubble.style.boxShadow = '0 4px 12px rgba(0,0,0,0.2)'
})
bubble.addEventListener('mouseleave', () => {
bubble.style.transform = 'scale(1)'
bubble.style.boxShadow = '0 2px 8px rgba(0,0,0,0.15)'
})
wrapper.append(panel, bubble)
container.appendChild(wrapper)
// State
let isOpen = false
let chatHandle: ChatHandle | null = null
const statusMessages: Record<string, string> = {
authenticating: 'Signing in...',
connecting: 'Connecting to agent...',
rendering: 'Loading chat...',
connected: '',
error: '',
}
function onStatus(status: string) {
statusBar.textContent = statusMessages[status] || status
if (status === 'connected') {
statusBar.style.display = 'none'
const child = webchatContainer.firstElementChild as HTMLElement | null
if (child) child.style.height = '100%'
}
}
function startChat() {
statusBar.style.display = 'block'
statusBar.style.color = '#666'
statusBar.textContent = 'Connecting...'
initChat(webchatContainer, config, onStatus)
.then((handle) => {
chatHandle = handle
})
.catch((err: Error) => {
console.error('CopilotChat: initialization failed', err)
statusBar.textContent = `Error: ${err.message}`
statusBar.style.color = '#d32f2f'
})
}
function toggle() {
isOpen = !isOpen
panel.style.display = isOpen ? 'flex' : 'none'
bubble.style.display = isOpen ? 'none' : 'flex'
if (isOpen && !chatHandle) {
startChat()
}
}
function refreshConversation() {
if (!chatHandle) return
statusBar.style.display = 'block'
statusBar.style.color = '#666'
statusBar.textContent = 'Starting new conversation...'
chatHandle.refresh()
.then(() => {
statusBar.style.display = 'none'
})
.catch((err: Error) => {
console.error('CopilotChat: refresh failed', err)
statusBar.textContent = `Error: ${err.message}`
statusBar.style.color = '#d32f2f'
})
}
bubble.addEventListener('click', toggle)
closeBtn.addEventListener('click', toggle)
refreshBtn.addEventListener('click', refreshConversation)
}