Skip to content

Commit

Permalink
feat(sdk): show user preferences and allow user to switch themes (#662)
Browse files Browse the repository at this point in the history
* refactor: remove `temp` from sdk options and create function to get userNavItems

* feat: add flag to show preferneces page

* feat: hide sidebar options from user preferences
  • Loading branch information
rsbh authored Jul 4, 2024
1 parent 0955545 commit 2c0f3a5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ export default function UserPreferences() {
label="Theme"
text="Customise your interface color scheme."
name="theme"
defaultValue="light"
defaultValue={theme}
values={themeOptions}
onSelection={value => setTheme(value)}
/>
<Separator></Separator>
<PreferencesSelection
{/* <PreferencesSelection
label="Sidebar"
text="Select the default state of product sidebar."
name="sidebar"
defaultValue="open"
values={sidebarOptions}
onSelection={value => console.log(value)}
/>
<Separator></Separator>
<Separator></Separator> */}
</Flex>
</Flex>
);
Expand Down
32 changes: 24 additions & 8 deletions sdks/js/packages/core/react/components/organization/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ import ConfirmPlanChange from './plans/confirm-change';
interface OrganizationProfileProps {
organizationId: string;
defaultRoute?: string;
tempShowBilling?: boolean;
tempShowTokens?: boolean;
showBilling?: boolean;
showTokens?: boolean;
showPreferences?: boolean;
hideToast?: boolean;
}

const routerContext = new RouterContext<
Pick<
OrganizationProfileProps,
'organizationId' | 'tempShowBilling' | 'tempShowTokens' | 'hideToast'
| 'organizationId'
| 'showBilling'
| 'showTokens'
| 'hideToast'
| 'showPreferences'
>
>();

Expand Down Expand Up @@ -296,14 +301,20 @@ const routeTree = rootRoute.addChildren([

const router = new Router({
routeTree,
context: { organizationId: '', tempShowBilling: false, tempShowTokens: false }
context: {
organizationId: '',
showBilling: false,
showTokens: false,
showPreferences: false
}
});

export const OrganizationProfile = ({
organizationId,
defaultRoute = '/',
tempShowBilling = false,
tempShowTokens = false,
showBilling = false,
showTokens = false,
showPreferences = false,
hideToast = false
}: OrganizationProfileProps) => {
const memoryHistory = createMemoryHistory({
Expand All @@ -313,9 +324,14 @@ export const OrganizationProfile = ({
const memoryRouter = new Router({
routeTree,
history: memoryHistory,
context: { organizationId, tempShowBilling, tempShowTokens, hideToast }
context: {
organizationId,
showBilling,
showTokens,
hideToast,
showPreferences
}
});

return <RouterProvider router={memoryRouter} />;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export type NavigationItemsTypes = {
};

interface getOrganizationNavItemsOptions {
tempShowBilling?: boolean;
tempShowTokens?: boolean;
showBilling?: boolean;
showTokens?: boolean;
canSeeBilling?: boolean;
}

interface getUserNavItemsOptions {
showPreferences?: boolean;
}

export const getOrganizationNavItems = (
options: getOrganizationNavItemsOptions = {}
) =>
Expand Down Expand Up @@ -45,27 +49,30 @@ export const getOrganizationNavItems = (
{
name: 'Billing',
to: '/billing',
show: options?.tempShowBilling && options?.canSeeBilling
show: options?.showBilling && options?.canSeeBilling
},
{
name: 'Tokens',
to: '/tokens',
show: options?.tempShowTokens
show: options?.showTokens
},
{
name: 'Plans',
to: '/plans',
show: options?.tempShowBilling
show: options?.showBilling
}
].filter(nav => nav.show) as NavigationItemsTypes[];

export const userNavItems = [
{
name: 'Profile',
to: '/profile'
}
// {
// name: 'Preferences',
// to: '/preferences'
// }
] as NavigationItemsTypes[];
export const getUserNavItems = (options: getUserNavItemsOptions = {}) =>
[
{
name: 'Profile',
to: '/profile',
show: true
},
{
name: 'Preferences',
to: '/preferences',
show: options?.showPreferences
}
].filter(nav => nav.show) as NavigationItemsTypes[];
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Link, useRouterContext, useRouterState } from '@tanstack/react-router';
import React, { useCallback, useMemo, useState } from 'react';
import organization from '~/react/assets/organization.png';
import user from '~/react/assets/user.png';
import { getOrganizationNavItems, userNavItems } from './helpers';
import { getOrganizationNavItems, getUserNavItems } from './helpers';

// @ts-ignore
import { MagnifyingGlassIcon } from '@radix-ui/react-icons';
Expand All @@ -21,9 +21,10 @@ import styles from './sidebar.module.css';
export const Sidebar = () => {
const [search, setSearch] = useState('');
const routerState = useRouterState();
const { organizationId, tempShowBilling, tempShowTokens } = useRouterContext({
from: '__root__'
});
const { organizationId, showBilling, showTokens, showPreferences } =
useRouterContext({
from: '__root__'
});

const isActive = useCallback(
(path: string) =>
Expand Down Expand Up @@ -61,11 +62,19 @@ export const Sidebar = () => {
const organizationNavItems = useMemo(
() =>
getOrganizationNavItems({
tempShowBilling: tempShowBilling,
showBilling: showBilling,
canSeeBilling: canSeeBilling,
tempShowTokens: tempShowTokens
showTokens: showTokens
}),
[tempShowBilling, canSeeBilling, tempShowTokens]
[showBilling, canSeeBilling, showTokens]
);

const userNavItems = useMemo(
() =>
getUserNavItems({
showPreferences: showPreferences
}),
[]
);

return (
Expand Down

0 comments on commit 2c0f3a5

Please sign in to comment.