mirror of
https://github.com/colanode/colanode.git
synced 2026-02-24 11:59:53 +01:00
Fix workspace create account context (#294)
This commit is contained in:
@@ -4,11 +4,13 @@ import { toast } from 'sonner';
|
||||
|
||||
import { collections } from '@colanode/ui/collections';
|
||||
import { WorkspaceForm } from '@colanode/ui/components/workspaces/workspace-form';
|
||||
import { useWorkspace } from '@colanode/ui/contexts/workspace';
|
||||
import { useMutation } from '@colanode/ui/hooks/use-mutation';
|
||||
|
||||
export const WorkspaceCreate = () => {
|
||||
const workspace = useWorkspace();
|
||||
interface WorkspaceCreateProps {
|
||||
accountId: string;
|
||||
}
|
||||
|
||||
export const WorkspaceCreate = ({ accountId }: WorkspaceCreateProps) => {
|
||||
const router = useRouter();
|
||||
const { mutate, isPending } = useMutation();
|
||||
|
||||
@@ -49,7 +51,7 @@ export const WorkspaceCreate = () => {
|
||||
type: 'workspace.create',
|
||||
name: values.name,
|
||||
description: values.description,
|
||||
accountId: workspace.accountId,
|
||||
accountId: accountId,
|
||||
avatar: values.avatar ?? null,
|
||||
},
|
||||
onSuccess(output) {
|
||||
|
||||
@@ -1,13 +1,54 @@
|
||||
import { createRoute } from '@tanstack/react-router';
|
||||
import { createRoute, redirect } from '@tanstack/react-router';
|
||||
|
||||
import { collections } from '@colanode/ui/collections';
|
||||
import { buildMetadataKey } from '@colanode/ui/collections/metadata';
|
||||
import { WorkspaceCreate } from '@colanode/ui/components/workspaces/workspace-create';
|
||||
import { WorkspaceCreateTab } from '@colanode/ui/components/workspaces/workspace-create-tab';
|
||||
import { rootRoute } from '@colanode/ui/routes/root';
|
||||
|
||||
const Component = () => {
|
||||
const { accountId } = workspaceCreateRoute.useLoaderData();
|
||||
return <WorkspaceCreate accountId={accountId} />;
|
||||
};
|
||||
|
||||
export const workspaceCreateRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/create',
|
||||
component: WorkspaceCreate,
|
||||
component: Component,
|
||||
beforeLoad: () => {
|
||||
const accountsCount = collections.accounts.size;
|
||||
if (accountsCount === 0) {
|
||||
throw redirect({ to: '/auth/login', replace: true });
|
||||
}
|
||||
},
|
||||
loader: () => {
|
||||
const accounts = collections.accounts.map((account) => account);
|
||||
const workspaces = collections.workspaces.map((workspace) => workspace);
|
||||
|
||||
const lastWorkspaceMetadataKey = buildMetadataKey('app', 'workspace');
|
||||
const lastWorkspace = collections.metadata.get(lastWorkspaceMetadataKey);
|
||||
if (lastWorkspace) {
|
||||
const workspace = workspaces.find(
|
||||
(workspace) => workspace.userId === lastWorkspace.value
|
||||
);
|
||||
|
||||
if (workspace) {
|
||||
return {
|
||||
accountId: workspace.accountId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const firstAccount = accounts[0];
|
||||
if (firstAccount) {
|
||||
return {
|
||||
accountId: firstAccount.id,
|
||||
};
|
||||
}
|
||||
|
||||
// this should never happen
|
||||
throw new Error('No accounts found');
|
||||
},
|
||||
context: () => {
|
||||
return {
|
||||
tab: <WorkspaceCreateTab />,
|
||||
|
||||
Reference in New Issue
Block a user