Skip to main content

Function Signature

export const updateDocument = async (roomId: string, title: string) => Promise<Room | undefined>
Updates the title of an existing collaborative document. Requires write permissions on the document.

Parameters

roomId
string
required
The unique identifier of the document room to update.
title
string
required
The new title for the document.

Return Value

room
Room | undefined
Returns the updated room object with the new metadata, or undefined if an error occurs.

Usage Example

import { updateDocument } from '@/lib/actions/room.actions';

const renameDocument = async (roomId: string, newTitle: string) => {
  const updatedDoc = await updateDocument(roomId, newTitle);
  
  if (updatedDoc) {
    console.log('Document renamed to:', updatedDoc.metadata.title);
  }
};

// Example usage
await renameDocument('room_abc123', 'Q4 Planning Document');

Error Handling

The function enforces strict permission requirements:
Authentication Required: User must be authenticated to update documents.
// Error: "Unauthorized to update document"
if (!currentEmail) {
  throw new Error('Unauthorized to update document');
}
Write Permission Required: User must have room:write access to update the document.
// Error: "Insufficient permissions to update document"
const access = room.usersAccesses[currentEmail] ?? [];
if (!access.includes('room:write')) {
  throw new Error('Insufficient permissions to update document');
}

Permission Levels

Only users with room:write permissions can update document metadata. Viewers with room:read access cannot modify the title.

Implementation Details

  • Permission Check: Verifies user has write access before allowing updates
  • Metadata Update: Only updates the title field, preserving other metadata
  • Cache Revalidation: Automatically revalidates /documents/{roomId} path
  • Partial Updates: Uses Liveblocks’ partial update mechanism for metadata

Cache Behavior

revalidatePath(`/documents/${roomId}`);
After a successful update, the function revalidates the document’s specific page to ensure the UI reflects the new title immediately.
declare type RoomMetadata = {
  creatorId: string;
  email: string;
  title: string;
};

declare type AccessType = ["room:write"] | ["room:read", "room:presence:write"];

Source

Defined in lib/actions/room.actions.ts:76

Build docs developers (and LLMs) love