33import Link from "next/link" ;
44import { useRouter } from "next/navigation" ;
55import { useTransition } from "react" ;
6- import { FileText } from "lucide-react" ;
7- import { createDocumentAction } from "../../lib/actions" ;
6+ import { FileText , Pencil } from "lucide-react" ;
7+ import { createDocumentAction , renameDocumentAction } from "../../lib/actions" ;
8+ import { runAction } from "./runAction" ;
89import { getDocumentName } from "../../lib/documents" ;
9- import { ResourceCard , NewResourceCard , formatRelativeTime } from "./ResourceCard" ;
10+ import { ResourceCard , NewResourceCard , ActionButton , formatRelativeTime } from "./ResourceCard" ;
1011
1112function NewDocumentCard ( ) {
1213 const router = useRouter ( ) ;
@@ -49,7 +50,19 @@ function NewDocumentCard() {
4950 ) ;
5051}
5152
52- function DocumentCard ( { id, name, lastModified } : { id : number ; name : string | null ; lastModified : Date | null } ) {
53+ function DocumentCard ( {
54+ id,
55+ name,
56+ lastModified,
57+ onRename,
58+ disabled,
59+ } : {
60+ id : number ;
61+ name : string | null ;
62+ lastModified : Date | null ;
63+ onRename : ( id : number , currentName : string ) => void ;
64+ disabled : boolean ;
65+ } ) {
5366 const displayName = getDocumentName ( { id, name } ) ;
5467
5568 return (
@@ -58,6 +71,18 @@ function DocumentCard({ id, name, lastModified }: { id: number; name: string | n
5871 preview = { < FileText className = "h-10 w-10 text-gray-400" /> }
5972 name = { displayName }
6073 date = { lastModified ? formatRelativeTime ( lastModified ) : "No versions" }
74+ actions = {
75+ < ActionButton
76+ onClick = { ( e ) => {
77+ e . preventDefault ( ) ;
78+ onRename ( id , displayName ) ;
79+ } }
80+ disabled = { disabled }
81+ title = "Rename"
82+ >
83+ < Pencil className = "h-3 w-3" />
84+ </ ActionButton >
85+ }
6186 />
6287 </ Link >
6388 ) ;
@@ -66,6 +91,22 @@ function DocumentCard({ id, name, lastModified }: { id: number; name: string | n
6691export function DocumentList ( { documents } : {
6792 documents : { id : number ; name : string | null ; lastModified : Date | null } [ ] ;
6893} ) {
94+ const router = useRouter ( ) ;
95+ const [ isPending , startTransition ] = useTransition ( ) ;
96+
97+ function handleRename ( id : number , currentName : string ) {
98+ const newName = window . prompt ( "Rename document" , currentName ) ;
99+ if ( newName === null || newName . trim ( ) === "" || newName . trim ( ) === currentName ) return ;
100+ startTransition ( async ( ) => {
101+ const result = await runAction ( renameDocumentAction ( { id, name : newName . trim ( ) } ) ) ;
102+ if ( result . success ) {
103+ router . refresh ( ) ;
104+ } else {
105+ alert ( result . error ) ;
106+ }
107+ } ) ;
108+ }
109+
69110 return (
70111 < div className = "flex flex-col gap-4" >
71112 < h1 className = "text-2xl font-bold" > Documents</ h1 >
@@ -79,6 +120,8 @@ export function DocumentList({ documents }: {
79120 id = { doc . id }
80121 name = { doc . name }
81122 lastModified = { doc . lastModified }
123+ onRename = { handleRename }
124+ disabled = { isPending }
82125 />
83126 ) ) }
84127 </ div >
0 commit comments