Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(documents): add rename document (#1350)

Corentin Thomassete51d64f64a01
diff --git a/apps/mobile/src/modules/documents/components/document-action-sheet.tsx b/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
index f638ccaa..50befb09 100644
--- a/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
+++ b/apps/mobile/src/modules/documents/components/document-action-sheet.tsx
@@ -6,6 +6,7 @@ import { formatBytes } from '@corentinth/chisels';
import { useQueryClient } from '@tanstack/react-query';
import { router } from 'expo-router';
import * as Sharing from 'expo-sharing';
+import { useState } from 'react';
import {
Modal,
StyleSheet,
@@ -16,7 +17,12 @@ import {
} from 'react-native';
import { useApiClient, useAuthClient } from '@/modules/api/providers/api.provider';
import { configLocalStorage } from '@/modules/config/config.local-storage';
-import { deleteDocument, fetchDocumentFile } from '@/modules/documents/documents.services';
+import { RenameDocumentDialog } from '@/modules/documents/components/rename-document-dialog';
+import {
+ deleteDocument,
+ fetchDocumentFile,
+ renameDocument,
+} from '@/modules/documents/documents.services';
import { Icon } from '@/modules/ui/components/icon';
import { useAlert } from '@/modules/ui/providers/alert-provider';
import { useThemeColor } from '@/modules/ui/providers/use-theme-color';
@@ -29,7 +35,7 @@ type DocumentActionSheetProps = {
onDeleted?: () => void;
};
-export type ActionsKey = 'view' | 'share' | 'delete';
+export type ActionsKey = 'view' | 'rename' | 'share' | 'delete';
export function DocumentActionSheet({
visible,
@@ -44,6 +50,7 @@ export function DocumentActionSheet({
const authClient = useAuthClient();
const apiClient = useApiClient();
const queryClient = useQueryClient();
+ const [isRenameDialogVisible, setIsRenameDialogVisible] = useState(false);
if (document === undefined) {
return null;
@@ -108,6 +115,44 @@ export function DocumentActionSheet({
}
};
+ const handleRename = () => {
+ // Keep the sheet mounted: onClose() would unmount this component (and the
+ // dialog with it) when the parent conditionally renders the sheet.
+ setIsRenameDialogVisible(true);
+ };
+
+ const handleRenameCancel = () => {
+ setIsRenameDialogVisible(false);
+ onClose();
+ };
+
+ const handleRenameConfirm = async (name: string) => {
+ setIsRenameDialogVisible(false);
+ onClose();
+
+ if (name === document.name) {
+ return;
+ }
+
+ try {
+ await renameDocument({
+ organizationId: document.organizationId,
+ documentId: document.id,
+ name,
+ apiClient,
+ });
+
+ await queryClient.invalidateQueries({
+ queryKey: ['organizations', document.organizationId, 'documents'],
+ });
+ } catch {
+ showAlert({
+ title: 'Error',
+ message: 'Failed to rename document',
+ });
+ }
+ };
+
const handleDelete = () => {
onClose();
@@ -163,6 +208,12 @@ export function DocumentActionSheet({
icon: 'eye',
onPress: handleView,
},
+ {
+ key: 'rename',
+ label: 'Rename',
+ icon: 'edit-2',
+ onPress: handleRename,
+ },
{
key: 'share',
label: 'Share',
@@ -180,66 +231,80 @@ export function DocumentActionSheet({
const filteredActions = actions.filter((action) => !excludedActions.includes(action.key));
return (
- <Modal visible={visible} transparent animationType="slide" onRequestClose={onClose}>
- <TouchableWithoutFeedback onPress={onClose}>
- <View style={styles.overlay}>
- <TouchableWithoutFeedback>
- <View style={styles.sheet}>
- <View style={styles.handleBar} />
+ <>
+ <RenameDocumentDialog
+ visible={isRenameDialogVisible}
+ defaultName={document.name}
+ onConfirm={handleRenameConfirm}
+ onCancel={handleRenameCancel}
+ />
- <View style={styles.header}>
- <View style={styles.fileIconContainer}>
- <Icon name="file-text" size={24} color={themeColors.primary} />
- </View>
- <View style={styles.headerContent}>
- <Text style={styles.documentName} numberOfLines={2}>
- {document.name}
- </Text>
- <Text style={styles.documentMeta}>
- {displayMimeType}
- {' · '}
- {formatBytes({ bytes: document.originalSize })}
- {' · '}
- {formatDate(document.createdAt.toISOString())}
- </Text>
+ <Modal
+ visible={visible && !isRenameDialogVisible}
+ transparent
+ animationType="slide"
+ onRequestClose={onClose}
+ >
+ <TouchableWithoutFeedback onPress={onClose}>
+ <View style={styles.overlay}>
+ <TouchableWithoutFeedback>
+ <View style={styles.sheet}>
+ <View style={styles.handleBar} />
+
+ <View style={styles.header}>
+ <View style={styles.fileIconContainer}>
+ <Icon name="file-text" size={24} color={themeColors.primary} />
+ </View>
+ <View style={styles.headerContent}>
+ <Text style={styles.documentName} numberOfLines={2}>
+ {document.name}
+ </Text>
+ <Text style={styles.documentMeta}>
+ {displayMimeType}
+ {' · '}
+ {formatBytes({ bytes: document.originalSize })}
+ {' · '}
+ {formatDate(document.createdAt.toISOString())}
+ </Text>
+ </View>
</View>
- </View>
- <View style={styles.actions}>
- {filteredActions.map((action) => (
- <TouchableOpacity
- key={action.key}
- style={styles.actionRow}
- onPress={action.onPress}
- activeOpacity={0.6}
- >
- <View style={styles.actionIconContainer}>
- <Icon
- name={action.icon}
- size={20}
- color={
- action.destructive === true
- ? themeColors.destructive
- : themeColors.foreground
- }
- />
- </View>
- <Text
- style={[
- styles.actionText,
- action.destructive === true && { color: themeColors.destructive },
- ]}
+ <View style={styles.actions}>
+ {filteredActions.map((action) => (
+ <TouchableOpacity
+ key={action.key}
+ style={styles.actionRow}
+ onPress={action.onPress}
+ activeOpacity={0.6}
>
- {action.label}
- </Text>
- </TouchableOpacity>
- ))}
+ <View style={styles.actionIconContainer}>
+ <Icon
+ name={action.icon}
+ size={20}
+ color={
+ action.destructive === true
+ ? themeColors.destructive
+ : themeColors.foreground
+ }
+ />
+ </View>
+ <Text
+ style={[
+ styles.actionText,
+ action.destructive === true && { color: themeColors.destructive },
+ ]}
+ >
+ {action.label}
+ </Text>
+ </TouchableOpacity>
+ ))}
+ </View>
</View>
- </View>
- </TouchableWithoutFeedback>
- </View>
- </TouchableWithoutFeedback>
- </Modal>
+ </TouchableWithoutFeedback>
+ </View>
+ </TouchableWithoutFeedback>
+ </Modal>
+ </>
);
}
diff --git a/apps/mobile/src/modules/documents/components/rename-document-dialog.tsx b/apps/mobile/src/modules/documents/components/rename-document-dialog.tsx
index d0c1bbbc..189c1249 100644
--- a/apps/mobile/src/modules/documents/components/rename-document-dialog.tsx
+++ b/apps/mobile/src/modules/documents/components/rename-document-dialog.tsx
@@ -1,5 +1,5 @@
import type { ThemeColors } from '@/modules/ui/theme.constants';
-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import {
Modal,
StyleSheet,
@@ -29,6 +29,7 @@ export function RenameDocumentDialog({
const { showAlert } = useAlert();
const styles = createStyles({ themeColors });
const [documentName, setDocumentName] = useState('');
+ const inputRef = useRef<TextInput>(null);
// Reset name when modal becomes visible
useEffect(() => {
@@ -37,6 +38,12 @@ export function RenameDocumentDialog({
}
}, [visible, defaultName]);
+ // autoFocus doesn't open the keyboard inside a Modal: it fires before the
+ // modal is presented, so focus manually once it is (delayed for Android)
+ const focusInput = () => {
+ setTimeout(() => inputRef.current?.focus(), 100);
+ };
+
const handleConfirm = () => {
if (documentName.trim() === '') {
showAlert({
@@ -56,7 +63,13 @@ export function RenameDocumentDialog({
};
return (
- <Modal visible={visible} transparent animationType="fade" onRequestClose={handleCancel}>
+ <Modal
+ visible={visible}
+ transparent
+ animationType="fade"
+ onRequestClose={handleCancel}
+ onShow={focusInput}
+ >
<TouchableWithoutFeedback onPress={handleCancel}>
<View style={styles.backdrop}>
<TouchableWithoutFeedback>
@@ -67,12 +80,12 @@ export function RenameDocumentDialog({
<View style={styles.inputContainer}>
<TextInput
+ ref={inputRef}
style={styles.input}
value={documentName}
onChangeText={setDocumentName}
placeholder="Enter document name"
placeholderTextColor={themeColors.mutedForeground}
- autoFocus
onSubmitEditing={handleConfirm}
/>
</View>
diff --git a/apps/mobile/src/modules/documents/documents.services.ts b/apps/mobile/src/modules/documents/documents.services.ts
index 36d242d0..7b0fa22e 100644
--- a/apps/mobile/src/modules/documents/documents.services.ts
+++ b/apps/mobile/src/modules/documents/documents.services.ts
@@ -112,6 +112,24 @@ export async function deleteDocument({
});
}
+export async function renameDocument({
+ organizationId,
+ documentId,
+ name,
+ apiClient,
+}: {
+ organizationId: string;
+ documentId: string;
+ name: string;
+ apiClient: ApiClient;
+}) {
+ await apiClient({
+ method: 'PATCH',
+ path: `/api/organizations/${organizationId}/documents/${documentId}`,
+ body: { name },
+ });
+}
+
export async function fetchDocumentFile({
document,
organizationId,