From 166646cccfe700673817c022bb47228d1628a39d Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Mon, 7 Aug 2023 09:16:27 +0200 Subject: [PATCH] Revert "feat: remove private prompts on related brain delete (#842)" This reverts commit 4c15fe2bfde7a2fdc59c299ef668f1ba0cd8ffa8. --- backend/core/routes/subscription_routes.py | 12 ---- .../components/SettingsTab/SettingsTab.tsx | 2 +- .../SettingsTab/components/PublicPrompts.tsx | 57 ++++++++++++++++++ .../PublicPrompts/PublicPrompts.tsx | 22 ------- .../PublicPromptsList/PublicPromptsList.tsx | 53 ----------------- .../hooks/usePublicPromptsList.ts | 59 ------------------- .../components/PublicPromptsList/index.ts | 1 - .../PublicPrompts/components/index.ts | 1 - .../PublicPrompts/hooks/usePublicPrompts.ts | 38 ------------ .../components/PublicPrompts/index.ts | 1 - .../SettingsTab/components/index.ts | 1 - 11 files changed, 58 insertions(+), 189 deletions(-) create mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/PublicPrompts.tsx delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/PublicPromptsList.tsx delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/hooks/usePublicPromptsList.ts delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/index.ts delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/index.ts delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/hooks/usePublicPrompts.ts delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/index.ts delete mode 100644 frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/index.ts diff --git a/backend/core/routes/subscription_routes.py b/backend/core/routes/subscription_routes.py index 13c4f7d0179e..4763c5430bf8 100644 --- a/backend/core/routes/subscription_routes.py +++ b/backend/core/routes/subscription_routes.py @@ -5,11 +5,9 @@ from fastapi import APIRouter, Depends, HTTPException from models.brains import Brain from models.brains_subscription_invitations import BrainSubscription -from models.prompt import PromptStatusEnum from models.users import User from pydantic import BaseModel from repository.brain.create_brain_user import create_brain_user -from repository.brain.get_brain_by_id import get_brain_by_id from repository.brain.get_brain_details import get_brain_details from repository.brain.get_brain_for_user import get_brain_for_user from repository.brain.update_user_rights import update_brain_user_rights @@ -19,8 +17,6 @@ from repository.brain_subscription.subscription_invitation_service import ( SubscriptionInvitationService, ) -from repository.prompt.delete_prompt_py_id import delete_prompt_by_id -from repository.prompt.get_prompt_by_id import get_prompt_by_id from repository.user.get_user_email_by_user_id import get_user_email_by_user_id from repository.user.get_user_id_by_user_email import get_user_id_by_user_email @@ -147,14 +143,6 @@ async def remove_user_subscription( ] if len(brain_other_owners) == 0: - # Delete its prompt if it's private - deleting_brain = get_brain_by_id(brain_id) - if deleting_brain and deleting_brain.prompt_id: - deleting_brain_prompt = get_prompt_by_id(deleting_brain.prompt_id) - if deleting_brain_prompt is not None and ( - deleting_brain_prompt.status == PromptStatusEnum.private - ): - delete_prompt_by_id(deleting_brain.prompt_id) brain.delete_brain(current_user.id) else: brain.delete_user_from_brain(current_user.id) diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/SettingsTab.tsx b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/SettingsTab.tsx index 30ceb26902d6..4415b0d6b6c2 100644 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/SettingsTab.tsx +++ b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/SettingsTab.tsx @@ -10,7 +10,7 @@ import { TextArea } from "@/lib/components/ui/TextArea"; import { models, paidModels } from "@/lib/context/BrainConfigProvider/types"; import { defineMaxTokens } from "@/lib/helpers/defineMexTokens"; -import { PublicPrompts } from "./components/PublicPrompts/PublicPrompts"; +import { PublicPrompts } from "./components/PublicPrompts"; import { useSettingsTab } from "./hooks/useSettingsTab"; type SettingsTabProps = { diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx new file mode 100644 index 000000000000..2f93e15266e5 --- /dev/null +++ b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts.tsx @@ -0,0 +1,57 @@ +import * as Accordion from "@radix-ui/react-accordion"; +import { ChangeEvent, useEffect, useState } from "react"; + +import { usePromptApi } from "@/lib/api/prompt/usePromptApi"; +import { Prompt } from "@/lib/types/Prompt"; + +type PublicPromptsProps = { + onSelect: ({ title, content }: { title: string; content: string }) => void; +}; + +export const PublicPrompts = ({ + onSelect, +}: PublicPromptsProps): JSX.Element => { + const [publicPrompts, setPublicPrompts] = useState([]); + + const { getPublicPrompts } = usePromptApi(); + + const fetchPublicPrompts = async () => { + setPublicPrompts(await getPublicPrompts()); + }; + + const handleChange = (event: ChangeEvent) => { + const selectedPrompt = publicPrompts.find( + (prompt) => prompt.id === event.target.value + ); + if (selectedPrompt) { + onSelect({ + title: selectedPrompt.title, + content: selectedPrompt.content, + }); + } + }; + + useEffect(() => { + void fetchPublicPrompts(); + }, []); + + return ( + + + Pick in public prompts + + + + + + ); +}; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/PublicPrompts.tsx b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/PublicPrompts.tsx deleted file mode 100644 index e99cc5635ac7..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/PublicPrompts.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { PublicPromptsList } from "./components/PublicPromptsList/PublicPromptsList"; -import { usePublicPrompts } from "./hooks/usePublicPrompts"; - -type PublicPromptsProps = { - onSelect: ({ title, content }: { title: string; content: string }) => void; -}; - -export const PublicPrompts = ({ - onSelect, -}: PublicPromptsProps): JSX.Element => { - const { handleChange, publicPrompts } = usePublicPrompts({ - onSelect, - }); - - return ( - - ); -}; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/PublicPromptsList.tsx b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/PublicPromptsList.tsx deleted file mode 100644 index 5a2c8a7057ce..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/PublicPromptsList.tsx +++ /dev/null @@ -1,53 +0,0 @@ -import { ChangeEvent } from "react"; - -import { Prompt } from "@/lib/types/Prompt"; - -import { usePublicPromptsList } from "./hooks/usePublicPromptsList"; - -type PublicPromptsListProps = { - options: Prompt[]; - onChange: (event: ChangeEvent) => void; - onSelect: ({ title, content }: { title: string; content: string }) => void; -}; - -export const PublicPromptsList = ({ - options, - onChange, - onSelect, -}: PublicPromptsListProps): JSX.Element => { - const { - handleOptionClick, - isOpen, - selectRef, - selectedOption, - toggleDropdown, - } = usePublicPromptsList({ - onChange, - onSelect, - }); - - return ( -
- - {isOpen && ( -
- {options.map((option) => ( -
handleOptionClick(option)} - > - {option.title} -
- ))} -
- )} -
- ); -}; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/hooks/usePublicPromptsList.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/hooks/usePublicPromptsList.ts deleted file mode 100644 index d45a28680adf..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/hooks/usePublicPromptsList.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { ChangeEvent, useEffect, useRef, useState } from "react"; - -import { Prompt } from "@/lib/types/Prompt"; - -type UsePublicPromptsListProps = { - onChange: (event: ChangeEvent) => void; - onSelect: ({ title, content }: { title: string; content: string }) => void; -}; - -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const usePublicPromptsList = ({ - onChange, - onSelect, -}: UsePublicPromptsListProps) => { - const [isOpen, setIsOpen] = useState(false); - const [selectedOption, setSelectedOption] = useState(null); - const selectRef = useRef(null); - - const toggleDropdown = () => { - setIsOpen((prevIsOpen) => !prevIsOpen); - }; - - const handleOptionClick = (option: Prompt) => { - setSelectedOption(option); - setIsOpen(false); - onChange({ - target: { value: option.id }, - } as ChangeEvent); - onSelect({ - title: option.title, - content: option.content, - }); - }; - - const handleClickOutside = (event: MouseEvent) => { - if ( - selectRef.current && - !selectRef.current.contains(event.target as Node) - ) { - setIsOpen(false); - } - }; - - useEffect(() => { - document.addEventListener("click", handleClickOutside, true); - - return () => { - document.removeEventListener("click", handleClickOutside, true); - }; - }, []); - - return { - isOpen, - selectedOption, - selectRef, - toggleDropdown, - handleOptionClick, - }; -}; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/index.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/index.ts deleted file mode 100644 index 4ce1d6e8aa9a..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/PublicPromptsList/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./PublicPromptsList"; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/index.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/index.ts deleted file mode 100644 index 4ce1d6e8aa9a..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/components/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./PublicPromptsList"; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/hooks/usePublicPrompts.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/hooks/usePublicPrompts.ts deleted file mode 100644 index 14edab8ecb12..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/hooks/usePublicPrompts.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { ChangeEvent, useEffect, useState } from "react"; - -import { usePromptApi } from "@/lib/api/prompt/usePromptApi"; -import { Prompt } from "@/lib/types/Prompt"; - -type UsePublicPromptsProps = { - onSelect: ({ title, content }: { title: string; content: string }) => void; -}; - -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const usePublicPrompts = ({ onSelect }: UsePublicPromptsProps) => { - const [publicPrompts, setPublicPrompts] = useState([]); - const { getPublicPrompts } = usePromptApi(); - - useEffect(() => { - const fetchPublicPrompts = async () => { - setPublicPrompts(await getPublicPrompts()); - }; - void fetchPublicPrompts(); - }, []); - - const handleChange = (event: ChangeEvent) => { - const selectedPrompt = publicPrompts.find( - (prompt) => prompt.id === event.target.value - ); - if (selectedPrompt) { - onSelect({ - title: selectedPrompt.title, - content: selectedPrompt.content, - }); - } - }; - - return { - publicPrompts, - handleChange, - }; -}; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/index.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/index.ts deleted file mode 100644 index 519e04261bf0..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/PublicPrompts/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./PublicPrompts"; diff --git a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/index.ts b/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/index.ts deleted file mode 100644 index 519e04261bf0..000000000000 --- a/frontend/app/brains-management/[brainId]/components/BrainManagementTabs/components/SettingsTab/components/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./PublicPrompts";