Skip to content

Commit

Permalink
chore(ui): settings page to load relevant tab (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren authored Nov 30, 2023
1 parent 07ca8cd commit 61e8640
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 160 deletions.
10 changes: 6 additions & 4 deletions keep-ui/app/settings/api-key-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ interface ApiKeyResponse {

interface Props {
accessToken: string;
selectedTab: string;
}

export default function ApiKeySettings({ accessToken }: Props) {
export default function ApiKeySettings({ accessToken, selectedTab }: Props) {
const apiUrl = getApiURL();
const { data, error, isLoading } = useSWR<ApiKeyResponse>(
`${apiUrl}/settings/apikey`,
(url) => fetcher(url, accessToken)
selectedTab === "api-key" ? `${apiUrl}/settings/apikey` : null,
(url) => fetcher(url, accessToken),
{ revalidateOnFocus: false }
);

if (isLoading) return <Loading />;
Expand All @@ -28,7 +30,7 @@ export default function ApiKeySettings({ accessToken }: Props) {
const copyBlockApiKeyProps = {
theme: { ...a11yLight },
language: "text",
text: data?.apiKey || '',
text: data?.apiKey || "",
codeBlock: true,
showLineNumbers: false,
};
Expand Down
61 changes: 50 additions & 11 deletions keep-ui/app/settings/settings.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,40 @@ import {
GlobeAltIcon,
UserGroupIcon,
EnvelopeIcon,
KeyIcon
KeyIcon,
} from "@heroicons/react/24/outline";
import UsersSettings from "./users-settings";
import WebhookSettings from "./webhook-settings";
import APIKeySettings from "./api-key-settings";
import { useSession } from "next-auth/react"
import { useSession } from "next-auth/react";
import Loading from "app/loading";
import SmtpSettings from "./smtp-settings";
import { useRouter } from "next/navigation";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useState } from "react";

export default function SettingsPage() {
const { data: session, status } = useSession();
const router = useRouter();
const searchParams = useSearchParams()!;
const pathname = usePathname();
const [selectedTab, setSelectedTab] = useState<string>(
searchParams?.get("selectedTab") || "users"
);

const handleTabChange = (tab: string) => {
setSelectedTab(tab);
router.push(`${pathname}?selectedTab=${tab}`);
};

// TODO: more robust way to handle this
const tabIndex =
selectedTab === "users"
? 0
: selectedTab === "webhook"
? 1
: selectedTab === "smtp"
? 2
: 3;

if (status === "loading") return <Loading />;
if (status === "unauthenticated") router.push("/signin");
Expand All @@ -28,28 +49,46 @@ export default function SettingsPage() {
* Think about a proper way to implement it.
*/
return (
<TabGroup>
<TabGroup index={tabIndex}>
<TabList color="orange">
<Tab icon={UserGroupIcon}>Users</Tab>
<Tab icon={GlobeAltIcon}>Webhook</Tab>
<Tab icon={EnvelopeIcon}>SMTP</Tab>
<Tab icon={KeyIcon}>Api Key</Tab>
<Tab icon={UserGroupIcon} onClick={() => handleTabChange("users")}>
Users
</Tab>
<Tab icon={GlobeAltIcon} onClick={() => handleTabChange("webhook")}>
Webhook
</Tab>
<Tab icon={EnvelopeIcon} onClick={() => handleTabChange("smtp")}>
SMTP
</Tab>
<Tab icon={KeyIcon} onClick={() => handleTabChange("api-key")}>
API Key
</Tab>
</TabList>
<TabPanels>
<TabPanel>
<UsersSettings
accessToken={session?.accessToken!}
currentUser={session?.user}
selectedTab={selectedTab}
/>
</TabPanel>
<TabPanel>
<WebhookSettings accessToken={session?.accessToken!} />
<WebhookSettings
accessToken={session?.accessToken!}
selectedTab={selectedTab}
/>
</TabPanel>
<TabPanel>
<SmtpSettings accessToken={session?.accessToken!} />
<SmtpSettings
accessToken={session?.accessToken!}
selectedTab={selectedTab}
/>
</TabPanel>
<TabPanel>
<APIKeySettings accessToken={session?.accessToken!} />
<APIKeySettings
accessToken={session?.accessToken!}
selectedTab={selectedTab}
/>
</TabPanel>
</TabPanels>
</TabGroup>
Expand Down
Loading

1 comment on commit 61e8640

@vercel
Copy link

@vercel vercel bot commented on 61e8640 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

keep – ./

keep-keephq.vercel.app
keep-eight.vercel.app
keep-git-main-keephq.vercel.app
platform.keephq.dev

Please sign in to comment.