Skip to content

Commit 724de21

Browse files
committed
Edit perms for tables
1 parent 3180707 commit 724de21

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
MothershipResource,
1010
MothershipResourceType,
1111
} from '@/app/workspace/[workspaceId]/home/types'
12+
import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/providers/workspace-permissions-provider'
1213
import { ResourceActions, ResourceContent, ResourceTabs } from './components'
1314

1415
const PREVIEW_CYCLE: Record<PreviewMode, PreviewMode> = {
@@ -85,6 +86,7 @@ export const MothershipView = memo(
8586
ref
8687
) {
8788
const active = resources.find((r) => r.id === activeResourceId) ?? resources[0] ?? null
89+
const { canEdit } = useUserPermissionsContext()
8890

8991
const streamingForActive =
9092
streamingFile && active && shouldShowStreamingFilePanel(streamingFile, active)
@@ -102,7 +104,9 @@ export const MothershipView = memo(
102104
}
103105

104106
const isActivePreviewable =
105-
active?.type === 'file' && RICH_PREVIEWABLE_EXTENSIONS.has(getFileExtension(active.title))
107+
canEdit &&
108+
active?.type === 'file' &&
109+
RICH_PREVIEWABLE_EXTENSIONS.has(getFileExtension(active.title))
106110

107111
return (
108112
<div

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table/table.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ export function Table({
208208
})
209209

210210
const userPermissions = useUserPermissionsContext()
211+
const canEditRef = useRef(userPermissions.canEdit)
212+
canEditRef.current = userPermissions.canEdit
213+
211214
const {
212215
contextMenu,
213216
handleRowContextMenu: baseHandleRowContextMenu,
@@ -633,6 +636,7 @@ export function Table({
633636
const handleCellClick = useCallback((rowId: string, columnName: string) => {
634637
const column = columnsRef.current.find((c) => c.name === columnName)
635638
if (column?.type === 'boolean') {
639+
if (!canEditRef.current) return
636640
const row = rowsRef.current.find((r) => r.id === rowId)
637641
if (row) {
638642
toggleBooleanCell(rowId, columnName, row.data[columnName])
@@ -647,6 +651,7 @@ export function Table({
647651
}, [])
648652

649653
const handleCellDoubleClick = useCallback((rowId: string, columnName: string) => {
654+
if (!canEditRef.current) return
650655
const column = columnsRef.current.find((c) => c.name === columnName)
651656
if (!column || column.type === 'boolean') return
652657

@@ -739,6 +744,7 @@ export function Table({
739744

740745
if ((e.key === 'Delete' || e.key === 'Backspace') && checkedRowsRef.current.size > 0) {
741746
if (editingCellRef.current) return
747+
if (!canEditRef.current) return
742748
e.preventDefault()
743749
const checked = checkedRowsRef.current
744750
const pMap = positionMapRef.current
@@ -770,6 +776,7 @@ export function Table({
770776
const totalRows = mp + 1
771777

772778
if (e.shiftKey && e.key === 'Enter') {
779+
if (!canEditRef.current) return
773780
const row = positionMapRef.current.get(anchor.rowIndex)
774781
if (!row) return
775782
e.preventDefault()
@@ -792,6 +799,7 @@ export function Table({
792799
}
793800

794801
if (e.key === 'Enter' || e.key === 'F2') {
802+
if (!canEditRef.current) return
795803
e.preventDefault()
796804
const col = cols[anchor.colIndex]
797805
if (!col) return
@@ -809,6 +817,7 @@ export function Table({
809817
}
810818

811819
if (e.key === ' ' && !e.shiftKey) {
820+
if (!canEditRef.current) return
812821
e.preventDefault()
813822
const row = positionMapRef.current.get(anchor.rowIndex)
814823
if (row) {
@@ -861,6 +870,7 @@ export function Table({
861870
}
862871

863872
if (e.key === 'Delete' || e.key === 'Backspace') {
873+
if (!canEditRef.current) return
864874
e.preventDefault()
865875
const sel = computeNormalizedSelection(anchor, selectionFocusRef.current)
866876
if (!sel) return
@@ -888,6 +898,7 @@ export function Table({
888898
}
889899

890900
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {
901+
if (!canEditRef.current) return
891902
const col = cols[anchor.colIndex]
892903
if (!col || col.type === 'boolean') return
893904
if (col.type === 'number' && !/[\d.-]/.test(e.key)) return
@@ -957,6 +968,7 @@ export function Table({
957968
const tag = (e.target as HTMLElement).tagName
958969
if (tag === 'INPUT' || tag === 'TEXTAREA') return
959970
if (editingCellRef.current) return
971+
if (!canEditRef.current) return
960972

961973
const checked = checkedRowsRef.current
962974
const cols = columnsRef.current
@@ -1029,6 +1041,7 @@ export function Table({
10291041
const handlePaste = (e: ClipboardEvent) => {
10301042
const tag = (e.target as HTMLElement).tagName
10311043
if (tag === 'INPUT' || tag === 'TEXTAREA') return
1044+
if (!canEditRef.current) return
10321045

10331046
const currentAnchor = selectionAnchorRef.current
10341047
if (!currentAnchor || editingCellRef.current) return
@@ -1523,6 +1536,7 @@ export function Table({
15231536
<ColumnHeaderMenu
15241537
key={column.name}
15251538
column={column}
1539+
readOnly={!userPermissions.canEdit}
15261540
isRenaming={columnRename.editingId === column.name}
15271541
renameValue={
15281542
columnRename.editingId === column.name ? columnRename.editValue : ''
@@ -1541,10 +1555,12 @@ export function Table({
15411555
onResizeEnd={handleColumnResizeEnd}
15421556
/>
15431557
))}
1544-
<AddColumnButton
1545-
onClick={handleAddColumn}
1546-
disabled={addColumnMutation.isPending}
1547-
/>
1558+
{userPermissions.canEdit && (
1559+
<AddColumnButton
1560+
onClick={handleAddColumn}
1561+
disabled={addColumnMutation.isPending}
1562+
/>
1563+
)}
15481564
</tr>
15491565
)}
15501566
</thead>
@@ -2414,6 +2430,7 @@ const COLUMN_TYPE_OPTIONS: { type: string; label: string; icon: React.ElementTyp
24142430

24152431
const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
24162432
column,
2433+
readOnly,
24172434
isRenaming,
24182435
renameValue,
24192436
onRenameValueChange,
@@ -2430,6 +2447,7 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
24302447
onResizeEnd,
24312448
}: {
24322449
column: ColumnDefinition
2450+
readOnly?: boolean
24332451
isRenaming: boolean
24342452
renameValue: string
24352453
onRenameValueChange: (value: string) => void
@@ -2503,6 +2521,13 @@ const ColumnHeaderMenu = React.memo(function ColumnHeaderMenu({
25032521
className='ml-[6px] min-w-0 flex-1 border-0 bg-transparent p-0 font-medium text-[13px] text-[var(--text-primary)] outline-none focus:outline-none focus:ring-0'
25042522
/>
25052523
</div>
2524+
) : readOnly ? (
2525+
<div className='flex h-full w-full min-w-0 items-center px-[8px] py-[7px]'>
2526+
<ColumnTypeIcon type={column.type} />
2527+
<span className='ml-[6px] min-w-0 truncate font-medium text-[13px] text-[var(--text-primary)]'>
2528+
{column.name}
2529+
</span>
2530+
</div>
25062531
) : (
25072532
<DropdownMenu>
25082533
<DropdownMenuTrigger asChild>

0 commit comments

Comments
 (0)