import { TldrawUiMenuItem } from "tldraw"
import { DefaultToolbar, DefaultToolbarContent } from "tldraw"
import { useTools } from "tldraw"
import { useEditor } from "tldraw"
import { useState, useEffect } from "react"
import { useDialogs } from "tldraw"
import { SettingsDialog } from "./SettingsDialog"
import { AuthDialog } from "./AuthDialog"
import { useAuth, clearSession } from "../context/AuthContext"
export function CustomToolbar() {
const editor = useEditor()
const tools = useTools()
const [isReady, setIsReady] = useState(false)
const [hasApiKey, setHasApiKey] = useState(false)
const { addDialog, removeDialog } = useDialogs()
const { session, updateSession } = useAuth()
const [showProfilePopup, setShowProfilePopup] = useState(false)
useEffect(() => {
if (editor && tools) {
setIsReady(true)
}
}, [editor, tools])
const checkApiKeys = () => {
const settings = localStorage.getItem("openai_api_key")
try {
if (settings) {
try {
const { keys } = JSON.parse(settings)
const hasValidKey = keys && Object.values(keys).some(key => typeof key === 'string' && key.trim() !== '')
setHasApiKey(hasValidKey)
} catch (e) {
const hasValidKey = typeof settings === 'string' && settings.trim() !== ''
setHasApiKey(hasValidKey)
}
} else {
setHasApiKey(false)
}
} catch (e) {
setHasApiKey(false)
}
}
// Initial check
useEffect(() => {
checkApiKeys()
}, [])
// Periodic check
useEffect(() => {
const interval = setInterval(checkApiKeys, 5000)
return () => clearInterval(interval)
}, [])
const handleLogout = () => {
// Clear the session
clearSession()
// Update the auth context
updateSession({
username: '',
authed: false,
backupCreated: null,
})
// Close the popup
setShowProfilePopup(false)
}
if (!isReady) return null
return (
{showProfilePopup && session.authed && (
Hello, {session.username}!
{!session.backupCreated && (
Remember to back up your encryption keys to prevent data loss!
)}
)}
{tools["VideoChat"] && (
)}
{tools["ChatBox"] && (
)}
{tools["Embed"] && (
)}
{tools["SlideShape"] && (
)}
{tools["Markdown"] && (
)}
{tools["MycrozineTemplate"] && (
)}
{tools["Prompt"] && (
)}
)
}