Add workspace context and custom hooks for user and team accounts (#41)
* Add workspace context and custom hooks for user and team accounts The commit introduces a new workspace context for both user and team accounts. New custom hooks, useUserWorkspace and useAccountWorkspace, were created to access this context data. These changes allow for more streamlined data access and manipulation, enabling features that depend on account context information to function more efficiently. * Upgrade pnpm action-setup to version 4 The commit updates the version of pnpm action-setup used in GitHub workflows from v2 to v4. This upgrade can provide enhancements and improved features offered by the newer version. * Update pnpm/action-setup version in workflow The commit removes the explicit "version: 8" specifier in the Github Action workflow for "pnpm/action-setup". The updated workflow now points to "pnpm/action-setup@v4" instead of "pnpm/action-setup@v2", aligning it with usage elsewhere in the workflow. * Refactor magic link auth container for URL handling The magic link auth container has been updated to use a standard URL interface for managing URL and query parameters. This improves readability and error handling in the code.URL-related operations are now conducted via the URL object.
This commit is contained in:
committed by
GitHub
parent
c5a50fa6c7
commit
8f097a4016
8
.github/workflows/workflow.yml
vendored
8
.github/workflows/workflow.yml
vendored
@@ -20,9 +20,7 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- uses: pnpm/action-setup@v2
|
||||
with:
|
||||
version: 8
|
||||
- uses: pnpm/action-setup@v4
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
@@ -62,9 +60,7 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- uses: pnpm/action-setup@v2
|
||||
with:
|
||||
version: 8
|
||||
- uses: pnpm/action-setup@v4
|
||||
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
|
||||
@@ -2,6 +2,7 @@ import { use } from 'react';
|
||||
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { UserWorkspaceContextProvider } from '@kit/accounts/components';
|
||||
import { If } from '@kit/ui/if';
|
||||
import {
|
||||
Page,
|
||||
@@ -41,7 +42,9 @@ function UserHomeLayout({ children }: React.PropsWithChildren) {
|
||||
<HomeMobileNavigation workspace={workspace} />
|
||||
</PageMobileNavigation>
|
||||
|
||||
<UserWorkspaceContextProvider value={workspace}>
|
||||
{children}
|
||||
</UserWorkspaceContextProvider>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { use } from 'react';
|
||||
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { TeamAccountWorkspaceContextProvider } from '@kit/team-accounts/components';
|
||||
import { If } from '@kit/ui/if';
|
||||
import {
|
||||
Page,
|
||||
@@ -69,7 +70,9 @@ function TeamWorkspaceLayout({
|
||||
</div>
|
||||
</PageMobileNavigation>
|
||||
|
||||
<TeamAccountWorkspaceContextProvider value={data}>
|
||||
{children}
|
||||
</TeamAccountWorkspaceContextProvider>
|
||||
</Page>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"./personal-account-dropdown": "./src/components/personal-account-dropdown.tsx",
|
||||
"./account-selector": "./src/components/account-selector.tsx",
|
||||
"./personal-account-settings": "./src/components/personal-account-settings/index.ts",
|
||||
"./components": "./src/components/index.ts",
|
||||
"./hooks/*": "./src/hooks/*.ts",
|
||||
"./api": "./src/server/api.ts"
|
||||
},
|
||||
|
||||
1
packages/features/accounts/src/components/index.ts
Normal file
1
packages/features/accounts/src/components/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './user-workspace-context';
|
||||
@@ -0,0 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { createContext } from 'react';
|
||||
|
||||
import { User } from '@supabase/supabase-js';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
interface UserWorkspace {
|
||||
accounts: Array<{
|
||||
label: string | null;
|
||||
value: string | null;
|
||||
image: string | null;
|
||||
}>;
|
||||
|
||||
workspace: {
|
||||
id: string | null;
|
||||
name: string | null;
|
||||
picture_url: string | null;
|
||||
subscription_status: Tables<'subscriptions'>['status'] | null;
|
||||
};
|
||||
|
||||
user: User;
|
||||
}
|
||||
|
||||
export const UserWorkspaceContext = createContext<UserWorkspace>(
|
||||
{} as UserWorkspace,
|
||||
);
|
||||
|
||||
export function UserWorkspaceContextProvider(
|
||||
props: React.PropsWithChildren<{
|
||||
value: UserWorkspace;
|
||||
}>,
|
||||
) {
|
||||
return (
|
||||
<UserWorkspaceContext.Provider value={props.value}>
|
||||
{props.children}
|
||||
</UserWorkspaceContext.Provider>
|
||||
);
|
||||
}
|
||||
17
packages/features/accounts/src/hooks/use-user-workspace.ts
Normal file
17
packages/features/accounts/src/hooks/use-user-workspace.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
'use client';
|
||||
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { UserWorkspaceContext } from '../components';
|
||||
|
||||
export function useUserWorkspace() {
|
||||
const ctx = useContext(UserWorkspaceContext);
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useUserWorkspace must be used within a UserWorkspaceProvider. This is only provided within the user workspace /home',
|
||||
);
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
@@ -49,8 +49,13 @@ export function MagicLinkAuthContainer({
|
||||
});
|
||||
|
||||
const onSubmit = ({ email }: { email: string }) => {
|
||||
const queryParams = inviteToken ? `?invite_token=${inviteToken}` : '';
|
||||
const emailRedirectTo = [redirectUrl, queryParams].join('');
|
||||
const url = new URL(redirectUrl);
|
||||
|
||||
if (inviteToken) {
|
||||
url.searchParams.set('invite_token', inviteToken);
|
||||
}
|
||||
|
||||
const emailRedirectTo = url.href;
|
||||
|
||||
const promise = () =>
|
||||
signInWithOtpMutation.mutateAsync({
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
"exports": {
|
||||
"./api": "./src/server/api.ts",
|
||||
"./components": "./src/components/index.ts",
|
||||
"./hooks/*": "./src/hooks/*.ts",
|
||||
"./webhooks": "./src/server/services/webhooks/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -5,3 +5,4 @@ export * from './invitations/account-invitations-table';
|
||||
export * from './settings/team-account-settings-container';
|
||||
export * from './invitations/accept-invitation-container';
|
||||
export * from './create-team-account-dialog';
|
||||
export * from './team-account-workspace-context';
|
||||
@@ -0,0 +1,27 @@
|
||||
'use client';
|
||||
|
||||
import { createContext } from 'react';
|
||||
|
||||
import { User } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
interface AccountWorkspace {
|
||||
accounts: Database['public']['Views']['user_accounts']['Row'][];
|
||||
account: Database['public']['Functions']['team_account_workspace']['Returns'][0];
|
||||
user: User;
|
||||
}
|
||||
|
||||
export const TeamAccountWorkspaceContext = createContext<AccountWorkspace>(
|
||||
{} as AccountWorkspace,
|
||||
);
|
||||
|
||||
export function TeamAccountWorkspaceContextProvider(
|
||||
props: React.PropsWithChildren<{ value: AccountWorkspace }>,
|
||||
) {
|
||||
return (
|
||||
<TeamAccountWorkspaceContext.Provider value={props.value}>
|
||||
{props.children}
|
||||
</TeamAccountWorkspaceContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
'use client';
|
||||
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { TeamAccountWorkspaceContext } from '../components';
|
||||
|
||||
/**
|
||||
* @name useAccountWorkspace
|
||||
* @description A hook to access the account workspace data.
|
||||
* @returns The account workspace data.
|
||||
*/
|
||||
export function useAccountWorkspace() {
|
||||
const ctx = useContext(TeamAccountWorkspaceContext);
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useAccountWorkspace must be used within a TeamAccountWorkspaceContext.Provider. This is only provided within the account workspace /home/[account]',
|
||||
);
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user