aboutsummaryrefslogtreecommitdiff
path: root/apps/browser-extension/utils/ui-components.ts
blob: dabe4974328f28629056e39206d52544180eef38 (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
 * UI Components Module
 * Reusable UI components for the browser extension
 */

import { ELEMENT_IDS, UI_CONFIG } from "./constants"
import type { ToastState } from "./types"

/**
 * Creates a toast notification element
 * @param state - The state of the toast (loading, success, error)
 * @returns HTMLElement - The toast element
 */
export function createToast(state: ToastState): HTMLElement {
	const toast = document.createElement("div")
	toast.id = ELEMENT_IDS.SUPERMEMORY_TOAST

	toast.style.cssText = `
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2147483647;
    background: #ffffff;
    border-radius: 9999px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    font-family: 'Space Grotesk', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    color: #374151;
    min-width: 200px;
    max-width: 300px;
    animation: slideIn 0.3s ease-out;
    box-shadow: 0 4px 24px 0 rgba(0,0,0,0.18), 0 1.5px 6px 0 rgba(0,0,0,0.12);
  `

	// Add keyframe animations and fonts if not already present
	if (!document.getElementById("supermemory-toast-styles")) {
		const style = document.createElement("style")
		style.id = "supermemory-toast-styles"
		style.textContent = `
      @font-face {
        font-family: 'Space Grotesk';
        font-style: normal;
        font-weight: 300;
        font-display: swap;
        src: url('${chrome.runtime.getURL("fonts/SpaceGrotesk-Light.ttf")}') format('truetype');
      }
      @font-face {
        font-family: 'Space Grotesk';
        font-style: normal;
        font-weight: 400;
        font-display: swap;
        src: url('${chrome.runtime.getURL("fonts/SpaceGrotesk-Regular.ttf")}') format('truetype');
      }
      @font-face {
        font-family: 'Space Grotesk';
        font-style: normal;
        font-weight: 500;
        font-display: swap;
        src: url('${chrome.runtime.getURL("fonts/SpaceGrotesk-Medium.ttf")}') format('truetype');
      }
      @font-face {
        font-family: 'Space Grotesk';
        font-style: normal;
        font-weight: 600;
        font-display: swap;
        src: url('${chrome.runtime.getURL("fonts/SpaceGrotesk-SemiBold.ttf")}') format('truetype');
      }
      @font-face {
        font-family: 'Space Grotesk';
        font-style: normal;
        font-weight: 700;
        font-display: swap;
        src: url('${chrome.runtime.getURL("fonts/SpaceGrotesk-Bold.ttf")}') format('truetype');
      }
      @keyframes slideIn {
        from { transform: translateX(100%); opacity: 0; }
        to { transform: translateX(0); opacity: 1; }
      }
      @keyframes fadeOut {
        from { transform: translateX(0); opacity: 1; }
        to { transform: translateX(100%); opacity: 0; }
      }
      @keyframes spin {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
      }
    `
		document.head.appendChild(style)
	}

	const icon = document.createElement("div")
	icon.style.cssText = "width: 20px; height: 20px; flex-shrink: 0;"

	let textElement: HTMLElement = document.createElement("span")
	textElement.style.fontWeight = "500"

	// Configure toast based on state
	switch (state) {
		case "loading":
			icon.innerHTML = `
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M12 6V2" stroke="#6366f1" stroke-width="2" stroke-linecap="round"/>
          <path d="M12 22V18" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.3"/>
          <path d="M20.49 8.51L18.36 6.38" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.7"/>
          <path d="M5.64 17.64L3.51 15.51" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.5"/>
          <path d="M22 12H18" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.8"/>
          <path d="M6 12H2" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.4"/>
          <path d="M20.49 15.49L18.36 17.62" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.9"/>
          <path d="M5.64 6.36L3.51 8.49" stroke="#6366f1" stroke-width="2" stroke-linecap="round" opacity="0.6"/>
        </svg>
      `
			icon.style.animation = "spin 1s linear infinite"
			textElement.textContent = "Adding to Memory..."
			break

		case "success": {
			const iconUrl = browser.runtime.getURL("/icon-16.png")
			icon.innerHTML = `<img src="${iconUrl}" width="20" height="20" alt="Success" style="border-radius: 2px;" />`
			textElement.textContent = "Added to Memory"
			break
		}

		case "error": {
			icon.innerHTML = `
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <circle cx="12" cy="12" r="10" fill="#ef4444"/>
          <path d="M15 9L9 15" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          <path d="M9 9L15 15" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      `
			const textContainer = document.createElement("div")
			textContainer.style.cssText =
				"display: flex; flex-direction: column; gap: 2px;"

			const mainText = document.createElement("span")
			mainText.style.cssText = "font-weight: 500; line-height: 1.2;"
			mainText.textContent = "Failed to save memory"

			const helperText = document.createElement("span")
			helperText.style.cssText =
				"font-size: 12px; color: #6b7280; font-weight: 400; line-height: 1.2;"
			helperText.textContent = "Make sure you are logged in"

			textContainer.appendChild(mainText)
			textContainer.appendChild(helperText)

			textElement = textContainer
			break
		}
	}

	toast.appendChild(icon)
	toast.appendChild(textElement)

	return toast
}

/**
 * Creates the Twitter import button
 * @param onClick - Click handler for the button
 * @returns HTMLElement - The button element
 */
export function createTwitterImportButton(onClick: () => void): HTMLElement {
	const button = document.createElement("div")
	button.id = ELEMENT_IDS.TWITTER_IMPORT_BUTTON
	button.style.cssText = `
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 2147483646;
    background: #ffffff;
    color: black;
    border: none;
    border-radius: 50px;
    padding: 10px 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s ease;
  `

	const iconUrl = browser.runtime.getURL("/icon-16.png")
	button.innerHTML = `
    <img src="${iconUrl}" width="20" height="20" alt="Save to Memory" style="border-radius: 4px;" />
    <span style="font-weight: 500; font-size: 12px;">Import Bookmarks</span>
  `

	button.addEventListener("mouseenter", () => {
		button.style.opacity = "0.8"
		button.style.boxShadow = "0 4px 12px rgba(29, 155, 240, 0.4)"
	})

	button.addEventListener("mouseleave", () => {
		button.style.opacity = "1"
		button.style.boxShadow = "0 2px 8px rgba(29, 155, 240, 0.3)"
	})

	button.addEventListener("click", onClick)

	return button
}

/**
 * Creates a save tweet element button for Twitter/X
 * @param onClick - Click handler for the button
 * @returns HTMLElement - The save button element
 */
export function createSaveTweetElement(onClick: () => void): HTMLElement {
	const iconButton = document.createElement("div")
	iconButton.style.cssText = `
    display: inline-flex;
    align-items: flex-end;
    opacity: 0.7;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    cursor: pointer;
    margin-right: 10px;
    margin-bottom: 2px;
    z-index: 1000;
  `

	const iconFileName = "/icon-16.png"
	const iconUrl = browser.runtime.getURL(iconFileName)
	iconButton.innerHTML = `
    <img src="${iconUrl}" width="20" height="20" alt="Save to Memory" style="border-radius: 4px;" />
  `

	iconButton.addEventListener("mouseenter", () => {
		iconButton.style.opacity = "1"
	})

	iconButton.addEventListener("mouseleave", () => {
		iconButton.style.opacity = "0.7"
	})

	iconButton.addEventListener("click", (event) => {
		event.stopPropagation()
		event.preventDefault()
		onClick()
	})

	return iconButton
}

/**
 * Creates a save element button for ChatGPT input bar
 * @param onClick - Click handler for the button
 * @returns HTMLElement - The save button element
 */
export function createChatGPTInputBarElement(onClick: () => void): HTMLElement {
	const iconButton = document.createElement("div")
	iconButton.style.cssText = `
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 24px;
    cursor: pointer;
    transition: opacity 0.2s ease;
    border-radius: 50%;
  `

	// Use appropriate icon based on theme
	const iconFileName = "/icon-16.png"
	const iconUrl = browser.runtime.getURL(iconFileName)
	iconButton.innerHTML = `
    <img src="${iconUrl}" width="20" height="20" alt="Save to Memory" style="border-radius: 50%;" />
  `

	iconButton.addEventListener("mouseenter", () => {
		iconButton.style.opacity = "0.8"
	})

	iconButton.addEventListener("mouseleave", () => {
		iconButton.style.opacity = "1"
	})

	iconButton.addEventListener("click", (event) => {
		event.stopPropagation()
		event.preventDefault()
		onClick()
	})

	return iconButton
}

/**
 * Creates a save element button for Claude input bar
 * @param onClick - Click handler for the button
 * @returns HTMLElement - The save button element
 */
export function createClaudeInputBarElement(onClick: () => void): HTMLElement {
	const iconButton = document.createElement("div")
	iconButton.style.cssText = `
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 32px;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 6px;
    background: transparent;
  `

	const iconFileName = "/icon-16.png"
	const iconUrl = browser.runtime.getURL(iconFileName)
	iconButton.innerHTML = `
    <img src="${iconUrl}" width="20" height="20" alt="Get Related Memories from supermemory" style="border-radius: 4px;" />
  `

	iconButton.addEventListener("mouseenter", () => {
		iconButton.style.backgroundColor = "rgba(0, 0, 0, 0.05)"
		iconButton.style.borderColor = "rgba(0, 0, 0, 0.2)"
	})

	iconButton.addEventListener("mouseleave", () => {
		iconButton.style.backgroundColor = "transparent"
		iconButton.style.borderColor = "rgba(0, 0, 0, 0.1)"
	})

	iconButton.addEventListener("click", (event) => {
		event.stopPropagation()
		event.preventDefault()
		onClick()
	})

	return iconButton
}

/**
 * Creates a save element button for T3.chat input bar
 * @param onClick - Click handler for the button
 * @returns HTMLElement - The save button element
 */
export function createT3InputBarElement(onClick: () => void): HTMLElement {
	const iconButton = document.createElement("div")
	iconButton.style.cssText = `
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 32px;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: 6px;
    background: transparent;
  `

	const iconFileName = "/icon-16.png"
	const iconUrl = browser.runtime.getURL(iconFileName)
	iconButton.innerHTML = `
    <img src="${iconUrl}" width="20" height="20" alt="Get Related Memories from supermemory" style="border-radius: 4px;" />
  `

	iconButton.addEventListener("mouseenter", () => {
		iconButton.style.backgroundColor = "rgba(0, 0, 0, 0.05)"
		iconButton.style.borderColor = "rgba(0, 0, 0, 0.2)"
	})

	iconButton.addEventListener("mouseleave", () => {
		iconButton.style.backgroundColor = "transparent"
		iconButton.style.borderColor = "rgba(0, 0, 0, 0.1)"
	})

	iconButton.addEventListener("click", (event) => {
		event.stopPropagation()
		event.preventDefault()
		onClick()
	})

	return iconButton
}

/**
 * Utility functions for DOM manipulation
 */
export const DOMUtils = {
	/**
	 * Check if current page is on specified domains
	 * @param domains - Array of domain names to check
	 * @returns boolean
	 */
	isOnDomain(domains: readonly string[]): boolean {
		return domains.includes(window.location.hostname)
	},

	/**
	 * Detect if the page is in dark mode based on color-scheme style
	 * @returns boolean - true if dark mode, false if light mode
	 */
	isDarkMode(): boolean {
		const htmlElement = document.documentElement
		const style = htmlElement.getAttribute("style")
		return style?.includes("color-scheme: dark") || false
	},

	/**
	 * Check if element exists in DOM
	 * @param id - Element ID to check
	 * @returns boolean
	 */
	elementExists(id: string): boolean {
		return !!document.getElementById(id)
	},

	/**
	 * Remove element from DOM if it exists
	 * @param id - Element ID to remove
	 */
	removeElement(id: string): void {
		const element = document.getElementById(id)
		element?.remove()
	},

	/**
	 * Show toast notification with auto-dismiss
	 * @param state - Toast state
	 * @param duration - Duration to show toast (default from config)
	 * @returns The toast element
	 */
	showToast(
		state: ToastState,
		duration: number = UI_CONFIG.TOAST_DURATION,
	): HTMLElement {
		const existingToast = document.getElementById(ELEMENT_IDS.SUPERMEMORY_TOAST)

		if ((state === "success" || state === "error") && existingToast) {
			const icon = existingToast.querySelector("div")
			const text = existingToast.querySelector("span")

			if (icon && text) {
				if (state === "success") {
					const iconUrl = browser.runtime.getURL("/icon-16.png")
					icon.innerHTML = `<img src="${iconUrl}" width="20" height="20" alt="Success" style="border-radius: 2px;" />`
					icon.style.animation = ""
					text.textContent = "Added to Memory"
				} else if (state === "error") {
					icon.innerHTML = `
						<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
							<circle cx="12" cy="12" r="10" fill="#ef4444"/>
							<path d="M15 9L9 15" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
							<path d="M9 9L15 15" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
						</svg>
					`
					icon.style.animation = ""

					const textContainer = document.createElement("div")
					textContainer.style.cssText =
						"display: flex; flex-direction: column; gap: 2px;"

					const mainText = document.createElement("span")
					mainText.style.cssText = "font-weight: 500; line-height: 1.2;"
					mainText.textContent = "Failed to save memory"

					const helperText = document.createElement("span")
					helperText.style.cssText =
						"font-size: 12px; color: #6b7280; font-weight: 400; line-height: 1.2;"
					helperText.textContent = "Make sure you are logged in"

					textContainer.appendChild(mainText)
					textContainer.appendChild(helperText)

					text.innerHTML = ""
					text.appendChild(textContainer)
				}

				// Auto-dismiss
				setTimeout(() => {
					if (document.body.contains(existingToast)) {
						existingToast.style.animation = "fadeOut 0.3s ease-out"
						setTimeout(() => {
							if (document.body.contains(existingToast)) {
								existingToast.remove()
							}
						}, 300)
					}
				}, duration)

				return existingToast
			}
		}

		const existingToasts = document.querySelectorAll(
			`#${ELEMENT_IDS.SUPERMEMORY_TOAST}`,
		)
		existingToasts.forEach((toast) => {
			toast.remove()
		})

		const toast = createToast(state)
		document.body.appendChild(toast)

		// Auto-dismiss for success and error states
		if (state === "success" || state === "error") {
			setTimeout(() => {
				if (document.body.contains(toast)) {
					toast.style.animation = "fadeOut 0.3s ease-out"
					setTimeout(() => {
						if (document.body.contains(toast)) {
							toast.remove()
						}
					}, 300)
				}
			}, duration)
		}

		return toast
	},
}