mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-07 22:18:33 +00:00
102 lines
3.3 KiB
JavaScript
102 lines
3.3 KiB
JavaScript
import React, { useCallback } from 'react';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
import { useTheme } from 'providers/Theme';
|
|
import { saveFolderRoot } from 'providers/ReduxStore/slices/collections/actions';
|
|
import { updateTableColumnWidths } from 'providers/ReduxStore/slices/tabs';
|
|
import MultiLineEditor from 'components/MultiLineEditor';
|
|
import InfoTip from 'components/InfoTip';
|
|
import EditableTable from 'components/EditableTable';
|
|
import StyledWrapper from './StyledWrapper';
|
|
import toast from 'react-hot-toast';
|
|
import { variableNameRegex } from 'utils/common/regex';
|
|
import { setFolderVars } from 'providers/ReduxStore/slices/collections/index';
|
|
|
|
const VarsTable = ({ folder, collection, vars, varType }) => {
|
|
const dispatch = useDispatch();
|
|
const { storedTheme } = useTheme();
|
|
const tabs = useSelector((state) => state.tabs.tabs);
|
|
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
|
|
|
|
// Get column widths from Redux
|
|
const focusedTab = tabs?.find((t) => t.uid === activeTabUid);
|
|
const folderVarsWidths = focusedTab?.tableColumnWidths?.['folder-vars'] || {};
|
|
|
|
const handleColumnWidthsChange = (tableId, widths) => {
|
|
dispatch(updateTableColumnWidths({ uid: activeTabUid, tableId, widths }));
|
|
};
|
|
|
|
const onSave = () => dispatch(saveFolderRoot(collection.uid, folder.uid));
|
|
|
|
const handleVarsChange = useCallback((updatedVars) => {
|
|
dispatch(setFolderVars({
|
|
collectionUid: collection.uid,
|
|
folderUid: folder.uid,
|
|
vars: updatedVars,
|
|
type: varType
|
|
}));
|
|
}, [dispatch, collection.uid, folder.uid, varType]);
|
|
|
|
const getRowError = useCallback((row, index, key) => {
|
|
if (key !== 'name') return null;
|
|
if (!row.name || row.name.trim() === '') return null;
|
|
if (!variableNameRegex.test(row.name)) {
|
|
return 'Variable contains invalid characters. Must only contain alphanumeric characters, "-", "_", "."';
|
|
}
|
|
return null;
|
|
}, []);
|
|
|
|
const columns = [
|
|
{
|
|
key: 'name',
|
|
name: 'Name',
|
|
isKeyField: true,
|
|
placeholder: 'Name',
|
|
width: '40%'
|
|
},
|
|
{
|
|
key: 'value',
|
|
name: varType === 'request' ? 'Value' : (
|
|
<div className="flex items-center">
|
|
<span>Expr</span>
|
|
<InfoTip content="You can write any valid JS expression here" infotipId={`folder-${varType}-var`} />
|
|
</div>
|
|
),
|
|
placeholder: varType === 'request' ? 'Value' : 'Expr',
|
|
render: ({ value, onChange }) => (
|
|
<MultiLineEditor
|
|
value={value || ''}
|
|
theme={storedTheme}
|
|
onSave={onSave}
|
|
onChange={onChange}
|
|
collection={collection}
|
|
item={folder}
|
|
placeholder={!value ? (varType === 'request' ? 'Value' : 'Expr') : ''}
|
|
/>
|
|
)
|
|
}
|
|
];
|
|
|
|
const defaultRow = {
|
|
name: '',
|
|
value: '',
|
|
...(varType === 'response' ? { local: false } : {})
|
|
};
|
|
|
|
return (
|
|
<StyledWrapper className="w-full">
|
|
<EditableTable
|
|
tableId="folder-vars"
|
|
columns={columns}
|
|
rows={vars}
|
|
onChange={handleVarsChange}
|
|
defaultRow={defaultRow}
|
|
getRowError={getRowError}
|
|
columnWidths={folderVarsWidths}
|
|
onColumnWidthsChange={(widths) => handleColumnWidthsChange('folder-vars', widths)}
|
|
/>
|
|
</StyledWrapper>
|
|
);
|
|
};
|
|
|
|
export default VarsTable;
|