diff --git a/src/components/wrappers/Organization/OrganizationCombobox.tsx b/src/components/wrappers/Organization/OrganizationCombobox.tsx index 8bf5aa80..4ed5dd47 100644 --- a/src/components/wrappers/Organization/OrganizationCombobox.tsx +++ b/src/components/wrappers/Organization/OrganizationCombobox.tsx @@ -1,10 +1,11 @@ "use client" -import {useEffect} from "react"; +import {useEffect, useState} from "react"; import {ComboBox} from "@/components/wrappers/combobox"; import {Organization} from "@prisma/client"; import {useStore} from "@/state-management/store"; +import {getCurrentOrganizationSlug, setCurrentOrganizationSlug} from "@/features/dashboard/organization"; export type organizationComboBoxProps = { organizations: Organization[] @@ -14,33 +15,50 @@ export type organizationComboBoxProps = { export function OrganizationComboBox(props: organizationComboBoxProps) { - const {organizationId, moveToAnotherOrganization} = useStore((state) => state); + // const {organizationId, moveToAnotherOrganization} = useStore((state) => state); + + const [organizationSlug, setOrganizationSlug] = useState() const {organizations, defaultOrganization} = props useEffect(() => { - if (organizationId == "") { - moveToAnotherOrganization(defaultOrganization.id) - } else { - const organization = organizations.find(organization => organization.id === organizationId) - if (!organization) moveToAnotherOrganization(defaultOrganization.id) - } - }, [organizationId]) + getCurrentOrganizationSlug().then(slug => { + + console.log("sssssss", slug) + if (slug == "") { + setOrganizationSlug(defaultOrganization.slug) + setCurrentOrganizationSlug(defaultOrganization.slug) + } else { + setOrganizationSlug(slug) + const organization = organizations.find(organization => organization.slug === slug) + if (!organization) { + setOrganizationSlug(defaultOrganization.slug) + setCurrentOrganizationSlug(defaultOrganization.slug) + } + } + }) + + }, [organizationSlug]) const values = organizations.map(organization => { return ({ - value: organization.id, + value: organization.slug, label: organization.name, }) }) - const onValueChange = async (id: string) => moveToAnotherOrganization(id) + const onValueChange = (slug: string) => { + if (organizationSlug !== slug) { + setOrganizationSlug(slug) + setCurrentOrganizationSlug(slug) + } + } return ( ) } \ No newline at end of file diff --git a/src/components/wrappers/combobox.tsx b/src/components/wrappers/combobox.tsx index 949e4eff..9f6137d0 100644 --- a/src/components/wrappers/combobox.tsx +++ b/src/components/wrappers/combobox.tsx @@ -21,6 +21,7 @@ import { } from "@/components/ui/popover" import {FormControl} from "@/components/ui/form"; import {SidebarMenuButton} from "@/components/ui/sidebar"; +import {Separator} from "@/components/ui/separator"; export type comboBoxProps = { values: Array<{ value: string, label: string }> @@ -33,7 +34,7 @@ export type comboBoxProps = { export function ComboBox(props: comboBoxProps) { - const {values: choices, defaultValue: defaultChoice = "", onValueChange, searchField = false} = props; + const {values: choices, defaultValue: defaultChoice = "", onValueChange, searchField = false, sideBar = false} = props; const [value, setValue] = useState() @@ -46,7 +47,7 @@ export function ComboBox(props: comboBoxProps) { return ( - {props.sideBar ? + {sideBar ? {value ? choices.find((choice) => choice.value === value)?.label @@ -69,10 +70,7 @@ export function ComboBox(props: comboBoxProps) { - + {searchField ? : null} @@ -100,6 +98,12 @@ export function ComboBox(props: comboBoxProps) { + + {sideBar ? + + + Create new organization + + : null} ) diff --git a/src/features/dashboard/organization.ts b/src/features/dashboard/organization.ts new file mode 100644 index 00000000..b5e74205 --- /dev/null +++ b/src/features/dashboard/organization.ts @@ -0,0 +1,14 @@ +'use server'; + +import {cookies} from 'next/headers'; + + +const COOKIE_NAME = 'PORTABASE_ORGANIZATION_SLUG'; + +export async function getCurrentOrganizationSlug() { + return (await cookies()).get(COOKIE_NAME)?.value || "default"; +} + +export async function setCurrentOrganizationSlug(slug: string) { + return (await cookies()).set(COOKIE_NAME, slug).get(COOKIE_NAME)?.value; +} \ No newline at end of file