Skip to content

Commit

Permalink
Add notifications on auth button (#68)
Browse files Browse the repository at this point in the history
* Add notifications

* Fix prettier
  • Loading branch information
petrvecera authored Feb 21, 2024
1 parent fe0e329 commit 31ef303
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src-tauri/src/plugins/cohdb/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl PluginState {
}

#[tauri::command]
async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<()> {
async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<String> {
let state = handle.state::<PluginState>();
let request = ActiveRequestState::new();

Expand All @@ -96,7 +96,8 @@ async fn authenticate<R: Runtime>(handle: AppHandle<R>) -> Result<()> {
*state.request.lock().await = Some(request);

info!("redirecting to auth URL: {auth_url}");
open(&handle.shell_scope(), auth_url, None).map_err(Shell)
open(&handle.shell_scope(), auth_url.clone(), None).map_err(Shell);
Ok(auth_url.to_string())
}

pub async fn retrieve_token<R: Runtime>(request: &str, handle: &AppHandle<R>) -> Result<()> {
Expand Down
17 changes: 13 additions & 4 deletions src/utils/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ import React from "react"
const showNotification = ({
title,
message,
type,
type = "success",
autoCloseInMs = 10000,
}: {
title: string
message: string
type: "success" | "error"
message: string | React.ReactNode
type?: "success" | "error" | "info"
autoCloseInMs?: number // Default is 10 seconds
}) => {
if (type === "success") {
notifications.show({
title: title,
message: message,
color: "green",
// 10 seconds
autoClose: 10000,
autoClose: autoCloseInMs,
icon: <IconCheck />,
})
} else if (type === "info") {
notifications.show({
title: title,
message: message,
color: "blue",
autoClose: autoCloseInMs,
})
} else {
notifications.show({
title: title,
Expand Down
32 changes: 29 additions & 3 deletions src/views/Replays.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Code,
} from "@mantine/core"
import { useEffect, useState } from "react"
import { IconCheck, IconX } from "@tabler/icons-react"
import { IconCheck, IconCopy, IconX } from "@tabler/icons-react"
import { open } from "@tauri-apps/api/dialog"
import { open as openLink } from "@tauri-apps/api/shell"
import {
Expand All @@ -33,6 +33,7 @@ import { COHDBIcon } from "../components/other/COHDB-icon"
import { showNotification } from "../utils/notifications"
import { cohdbBaseUrl, cohdbPlayerOverView } from "../utils/external-routes"
import HelperIcon from "../components/other/helper-icon"
import { writeText } from "@tauri-apps/api/clipboard"

interface CohdbUser {
name: string
Expand Down Expand Up @@ -149,9 +150,34 @@ export const Replays: React.FC = () => {
) : (
<Button
variant="default"
onClick={() => {
invoke("plugin:cohdb|authenticate")
onClick={async () => {
const authUrl = await invoke("plugin:cohdb|authenticate")
events.connect_coh_db()

showNotification({
title: "Opening browser window",
message: (
<>
If it didn't open, please copy this url into your
browser:
<Space h={"xs"} />
<Group spacing={"xs"} noWrap>
<Tooltip label="Copy">
<ActionIcon
onClick={() => {
writeText(`${authUrl}`)
}}
>
<IconCopy size="22" />
</ActionIcon>
</Tooltip>
<Code block>{`${authUrl}`}</Code>
</Group>
</>
),
type: "info",
autoCloseInMs: 20000,
})
}}
>
<span style={{ paddingRight: 10 }}>
Expand Down

0 comments on commit 31ef303

Please sign in to comment.