import { Button, Divider, Input, List, ListItem, Radio, RadioGroup, Tooltip } from "@mui/joy"; import { isEqual } from "lodash-es"; import { useMemo, useState } from "react"; import { toast } from "react-hot-toast"; import { Link } from "react-router-dom"; import { workspaceSettingNamePrefix, useWorkspaceSettingStore } from "@/store/v1"; import { WorkspaceStorageSetting, WorkspaceStorageSetting_S3Config, WorkspaceStorageSetting_StorageType, } from "@/types/proto/api/v1/workspace_setting_service"; import { WorkspaceSettingKey } from "@/types/proto/store/workspace_setting"; import { useTranslate } from "@/utils/i18n"; import Icon from "../Icon"; const StorageSection = () => { const t = useTranslate(); const workspaceSettingStore = useWorkspaceSettingStore(); const [workspaceStorageSetting, setWorkspaceStorageSetting] = useState( WorkspaceStorageSetting.fromPartial(workspaceSettingStore.getWorkspaceSettingByKey(WorkspaceSettingKey.STORAGE)?.storageSetting || {}), ); const allowSaveStorageSetting = useMemo(() => { if (workspaceStorageSetting.uploadSizeLimitMb <= 0) { return false; } const origin = WorkspaceStorageSetting.fromPartial( workspaceSettingStore.getWorkspaceSettingByKey(WorkspaceSettingKey.STORAGE)?.storageSetting || {}, ); if (workspaceStorageSetting.storageType === WorkspaceStorageSetting_StorageType.LOCAL) { if (workspaceStorageSetting.filepathTemplate.length === 0) { return false; } } else if (workspaceStorageSetting.storageType === WorkspaceStorageSetting_StorageType.S3) { if ( workspaceStorageSetting.s3Config?.accessKeyId.length === 0 || workspaceStorageSetting.s3Config?.accessKeySecret.length === 0 || workspaceStorageSetting.s3Config?.endpoint.length === 0 || workspaceStorageSetting.s3Config?.region.length === 0 || workspaceStorageSetting.s3Config?.bucket.length === 0 ) { return false; } } return !isEqual(origin, workspaceStorageSetting); }, [workspaceStorageSetting, workspaceSettingStore.getState()]); const handleMaxUploadSizeChanged = async (event: React.FocusEvent) => { let num = parseInt(event.target.value); if (Number.isNaN(num)) { num = 0; } const update: WorkspaceStorageSetting = { ...workspaceStorageSetting, uploadSizeLimitMb: num, }; setWorkspaceStorageSetting(update); }; const handleFilepathTemplateChanged = async (event: React.FocusEvent) => { const update: WorkspaceStorageSetting = { ...workspaceStorageSetting, filepathTemplate: event.target.value, }; setWorkspaceStorageSetting(update); }; const handlePartialS3ConfigChanged = async (s3Config: Partial) => { const update: WorkspaceStorageSetting = { ...workspaceStorageSetting, s3Config: WorkspaceStorageSetting_S3Config.fromPartial({ ...workspaceStorageSetting.s3Config, ...s3Config, }), }; setWorkspaceStorageSetting(update); }; const handleS3ConfigAccessKeyIdChanged = async (event: React.FocusEvent) => { handlePartialS3ConfigChanged({ accessKeyId: event.target.value }); }; const handleS3ConfigAccessKeySecretChanged = async (event: React.FocusEvent) => { handlePartialS3ConfigChanged({ accessKeySecret: event.target.value }); }; const handleS3ConfigEndpointChanged = async (event: React.FocusEvent) => { handlePartialS3ConfigChanged({ endpoint: event.target.value }); }; const handleS3ConfigRegionChanged = async (event: React.FocusEvent) => { handlePartialS3ConfigChanged({ region: event.target.value }); }; const handleS3ConfigBucketChanged = async (event: React.FocusEvent) => { handlePartialS3ConfigChanged({ bucket: event.target.value }); }; const handleStorageTypeChanged = async (storageType: WorkspaceStorageSetting_StorageType) => { const update: WorkspaceStorageSetting = { ...workspaceStorageSetting, storageType: storageType, }; setWorkspaceStorageSetting(update); }; const saveWorkspaceStorageSetting = async () => { await workspaceSettingStore.setWorkspaceSetting({ name: `${workspaceSettingNamePrefix}${WorkspaceSettingKey.STORAGE}`, storageSetting: workspaceStorageSetting, }); toast.success("Updated"); }; return (
{t("setting.storage-section.current-storage")}
{ handleStorageTypeChanged(event.target.value as WorkspaceStorageSetting_StorageType); }} >
{t("setting.system-section.max-upload-size")}
{workspaceStorageSetting.storageType !== WorkspaceStorageSetting_StorageType.DATABASE && (
Filepath template
)} {workspaceStorageSetting.storageType === WorkspaceStorageSetting_StorageType.S3 && ( <>
Access key id
Access key secret
Endpoint
Region
Bucket
)}

{t("common.learn-more")}:

Docs - Local storage Choosing a Storage for Your Resource: Database, S3 or Local Storage?
); }; export default StorageSection;