Skip to content

Commit

Permalink
DEV2-4144 fix sign in flow (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirbilu authored Nov 9, 2023
1 parent 1a94338 commit 7940c11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
package com.tabnineCommon.chat

import java.awt.Desktop
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException
import javax.swing.BorderFactory
import javax.swing.JLabel
import javax.swing.SwingConstants

val CHAT_DISABLED_PAGE = """
<html>
import com.intellij.ide.BrowserUtil
import com.tabnineCommon.binary.requests.login.LoginRequest
import com.tabnineCommon.general.DependencyContainer
import javax.swing.JEditorPane
import javax.swing.event.HyperlinkEvent
import javax.swing.event.HyperlinkListener

val LOGIN_BUTTON = "<p>If you already have access to chat, please <a href=\"https://app.tabnine.com/signin\">sign in</a></p>".trimIndent()

fun createChatDisabledJPane(isLoggedIn: Boolean = false): JEditorPane {
val pane = JEditorPane(
"text/html",
"""<html>
<body style="padding: 20px;">
<div style="font-size: 12px; margin-bottom: 8px"><b>Tabnine Chat is currently in Beta</b></div>
<div style="font-size: 10px;">
<p style="margin-bottom: 8px">We understand that waiting for this awesome feature isn’t easy, but we guarantee it will be worth it.</p>
<p style="margin-bottom: 8px">Tabnine Chat will soon be available to all users, and we'll make sure to keep you informed. Thank you for your patience! <a href=""> Learn more</a></p>
<p>Received Tabnine Chat beta access? Please ensure you're signed in</p>
<p style="margin-bottom: 8px">Tabnine Chat will soon be available to all users, and we'll make sure to keep you informed. Thank you for your patience! <a href="https://www.tabnine.com/#ChatSection"> Learn more</a></p>
${if (!isLoggedIn) LOGIN_BUTTON else ""}
</div>
</body>
</html>
""".trimIndent()

fun createChatDisabledJLabel(): JLabel {
val label = JLabel(
CHAT_DISABLED_PAGE, SwingConstants.CENTER
)

label.border = BorderFactory.createEmptyBorder(10, 0, 0, 0)
label.addMouseListener(NaiveOpenLinkListener("https://www.tabnine.com/#ChatSection"))

return label
}

class NaiveOpenLinkListener(private val link: String) : MouseListener {
override fun mouseClicked(e: MouseEvent?) {
try {
Desktop.getDesktop().browse(URI(link))
return
} catch (e1: IOException) {
e1.printStackTrace()
} catch (e1: URISyntaxException) {
e1.printStackTrace()
}
}

override fun mousePressed(e: MouseEvent?) {
}

override fun mouseReleased(e: MouseEvent?) {
}

override fun mouseEntered(e: MouseEvent?) {
""".trimIndent()
).apply {
isEditable = false
isOpaque = false
}
pane.addHyperlinkListener(
HyperlinkListener { it ->
if (it.eventType !== HyperlinkEvent.EventType.ACTIVATED) {
return@HyperlinkListener
}
if (it.url.toString() == "https://www.tabnine.com/#ChatSection") {
BrowserUtil.browse(it.url.toString())
} else if (it.url.toString() == "https://app.tabnine.com/signin") {
DependencyContainer.instanceOfBinaryRequestFacade().executeRequest(LoginRequest {})
}
}
)

override fun mouseExited(e: MouseEvent?) {
}
return pane
}
19 changes: 16 additions & 3 deletions Common/src/main/java/com/tabnineCommon/chat/ChatFrame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.tabnineCommon.binary.requests.analytics.EventRequest
import com.tabnineCommon.chat.actions.TabnineActionsGroup
import com.tabnineCommon.config.Config
import com.tabnineCommon.lifecycle.BinaryCapabilitiesChangeNotifier
import com.tabnineCommon.lifecycle.BinaryStateChangeNotifier
import java.awt.BorderLayout
import java.awt.Color
import javax.swing.BorderFactory
Expand All @@ -27,13 +28,25 @@ import javax.swing.event.HyperlinkEvent
class ChatFrame(private val project: Project, private val binaryRequestFacade: BinaryRequestFacade) :
JPanel(true), Disposable {
private var capabilitiesFetched = false
private var isLoggedIn = false

init {
layout = BorderLayout()

updateDisplay()

val connection = ApplicationManager.getApplication().messageBus.connect(this)

connection.subscribe(
BinaryStateChangeNotifier.STATE_CHANGED_TOPIC,
BinaryStateChangeNotifier { state ->
isLoggedIn = state.isLoggedIn == true
ApplicationManager.getApplication().invokeLater {
updateDisplay()
}
}
)

connection.subscribe(
ChatEnabled.ENABLED_TOPIC,
ChatEnabledChanged {
Expand All @@ -59,10 +72,10 @@ class ChatFrame(private val project: Project, private val binaryRequestFacade: B
}

private fun updateDisplay() {
if (ChatEnabled.getInstance().enabled) {
if (ChatEnabled.getInstance().enabled && isLoggedIn) {
displayChat()
} else {
if (capabilitiesFetched || Config.IS_SELF_HOSTED) {
if (capabilitiesFetched || Config.IS_SELF_HOSTED || !isLoggedIn) {
displayChatNotEnabled()
} else {
displayText("Loading...")
Expand All @@ -71,7 +84,7 @@ class ChatFrame(private val project: Project, private val binaryRequestFacade: B
}

private fun displayChatNotEnabled() {
displayJLabel(createChatDisabledJLabel())
setComponents(listOf(Pair(createChatDisabledJPane(isLoggedIn), BorderLayout.CENTER)))
}

private fun displayText(text: String) {
Expand Down

0 comments on commit 7940c11

Please sign in to comment.