balibabu commited on
Commit
019279d
·
1 Parent(s): 8d992fe

Feat: Rename document name #3221 (#4544)

Browse files

### What problem does this PR solve?

Feat: Rename document name #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)

web/src/components/rename-dialog/index.tsx CHANGED
@@ -1,49 +1,39 @@
1
- import { Button } from '@/components/ui/button';
2
  import {
3
  Dialog,
4
  DialogContent,
5
  DialogFooter,
6
  DialogHeader,
7
  DialogTitle,
8
- DialogTrigger,
9
  } from '@/components/ui/dialog';
10
- import { Input } from '@/components/ui/input';
11
- import { Label } from '@/components/ui/label';
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- export function RenameDialog() {
14
  return (
15
- <Dialog>
16
- <DialogTrigger asChild>
17
- <Button variant="outline">Edit Profile</Button>
18
- </DialogTrigger>
19
  <DialogContent className="sm:max-w-[425px]">
20
  <DialogHeader>
21
- <DialogTitle>Edit profile</DialogTitle>
22
  </DialogHeader>
23
- <div className="grid gap-4 py-4">
24
- <div className="grid grid-cols-4 items-center gap-4">
25
- <Label htmlFor="name" className="text-right">
26
- Name
27
- </Label>
28
- <Input
29
- id="name"
30
- defaultValue="Pedro Duarte"
31
- className="col-span-3"
32
- />
33
- </div>
34
- <div className="grid grid-cols-4 items-center gap-4">
35
- <Label htmlFor="username" className="text-right">
36
- Username
37
- </Label>
38
- <Input
39
- id="username"
40
- defaultValue="@peduarte"
41
- className="col-span-3"
42
- />
43
- </div>
44
- </div>
45
  <DialogFooter>
46
- <Button type="submit">Save changes</Button>
 
 
47
  </DialogFooter>
48
  </DialogContent>
49
  </Dialog>
 
 
1
  import {
2
  Dialog,
3
  DialogContent,
4
  DialogFooter,
5
  DialogHeader,
6
  DialogTitle,
 
7
  } from '@/components/ui/dialog';
8
+ import { LoadingButton } from '@/components/ui/loading-button';
9
+ import { IModalProps } from '@/interfaces/common';
10
+ import { TagRenameId } from '@/pages/add-knowledge/constant';
11
+ import { useTranslation } from 'react-i18next';
12
+ import { RenameForm } from './rename-form';
13
+
14
+ export function RenameDialog({
15
+ hideModal,
16
+ initialName,
17
+ onOk,
18
+ loading,
19
+ }: IModalProps<any> & { initialName: string }) {
20
+ const { t } = useTranslation();
21
 
 
22
  return (
23
+ <Dialog open onOpenChange={hideModal}>
 
 
 
24
  <DialogContent className="sm:max-w-[425px]">
25
  <DialogHeader>
26
+ <DialogTitle>{t('common.rename')}</DialogTitle>
27
  </DialogHeader>
28
+ <RenameForm
29
+ initialName={initialName}
30
+ hideModal={hideModal}
31
+ onOk={onOk}
32
+ ></RenameForm>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  <DialogFooter>
34
+ <LoadingButton type="submit" form={TagRenameId} loading={loading}>
35
+ {t('common.save')}
36
+ </LoadingButton>
37
  </DialogFooter>
38
  </DialogContent>
39
  </Dialog>
web/src/components/rename-dialog/rename-form.tsx ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client';
2
+
3
+ import { zodResolver } from '@hookform/resolvers/zod';
4
+ import { useForm } from 'react-hook-form';
5
+ import { z } from 'zod';
6
+
7
+ import {
8
+ Form,
9
+ FormControl,
10
+ FormField,
11
+ FormItem,
12
+ FormLabel,
13
+ FormMessage,
14
+ } from '@/components/ui/form';
15
+ import { Input } from '@/components/ui/input';
16
+ import { IModalProps } from '@/interfaces/common';
17
+ import { TagRenameId } from '@/pages/add-knowledge/constant';
18
+ import { useEffect } from 'react';
19
+ import { useTranslation } from 'react-i18next';
20
+
21
+ export function RenameForm({
22
+ initialName,
23
+ hideModal,
24
+ onOk,
25
+ }: IModalProps<any> & { initialName: string }) {
26
+ const { t } = useTranslation();
27
+ const FormSchema = z.object({
28
+ name: z
29
+ .string()
30
+ .min(1, {
31
+ message: t('common.namePlaceholder'),
32
+ })
33
+ .trim(),
34
+ });
35
+
36
+ const form = useForm<z.infer<typeof FormSchema>>({
37
+ resolver: zodResolver(FormSchema),
38
+ defaultValues: { name: '' },
39
+ });
40
+
41
+ async function onSubmit(data: z.infer<typeof FormSchema>) {
42
+ const ret = await onOk?.(data.name);
43
+ if (ret) {
44
+ hideModal?.();
45
+ }
46
+ }
47
+
48
+ useEffect(() => {
49
+ form.setValue('name', initialName);
50
+ }, [form, initialName]);
51
+
52
+ return (
53
+ <Form {...form}>
54
+ <form
55
+ onSubmit={form.handleSubmit(onSubmit)}
56
+ className="space-y-6"
57
+ id={TagRenameId}
58
+ >
59
+ <FormField
60
+ control={form.control}
61
+ name="name"
62
+ render={({ field }) => (
63
+ <FormItem>
64
+ <FormLabel>{t('common.name')}</FormLabel>
65
+ <FormControl>
66
+ <Input
67
+ placeholder={t('common.namePlaceholder')}
68
+ {...field}
69
+ autoComplete="off"
70
+ />
71
+ </FormControl>
72
+ <FormMessage />
73
+ </FormItem>
74
+ )}
75
+ />
76
+ </form>
77
+ </Form>
78
+ );
79
+ }
web/src/pages/files/action-cell.tsx CHANGED
@@ -4,26 +4,49 @@ import {
4
  DropdownMenu,
5
  DropdownMenuContent,
6
  DropdownMenuItem,
7
- DropdownMenuLabel,
8
  DropdownMenuSeparator,
9
  DropdownMenuTrigger,
10
  } from '@/components/ui/dropdown-menu';
 
11
  import { IFile } from '@/interfaces/database/file-manager';
12
  import { CellContext } from '@tanstack/react-table';
13
  import { EllipsisVertical, Link2, Trash2 } from 'lucide-react';
14
  import { useCallback } from 'react';
15
- import { UseHandleConnectToKnowledgeReturnType } from './hooks';
 
 
 
 
16
 
17
  type IProps = Pick<CellContext<IFile, unknown>, 'row'> &
18
- Pick<UseHandleConnectToKnowledgeReturnType, 'showConnectToKnowledgeModal'>;
 
19
 
20
- export function ActionCell({ row, showConnectToKnowledgeModal }: IProps) {
 
 
 
 
 
21
  const record = row.original;
 
 
22
 
23
  const handleShowConnectToKnowledgeModal = useCallback(() => {
24
  showConnectToKnowledgeModal(record);
25
  }, [record, showConnectToKnowledgeModal]);
26
 
 
 
 
 
 
 
 
 
 
 
 
27
  return (
28
  <section className="flex gap-4 items-center">
29
  <Button
@@ -45,15 +68,19 @@ export function ActionCell({ row, showConnectToKnowledgeModal }: IProps) {
45
  </Button>
46
  </DropdownMenuTrigger>
47
  <DropdownMenuContent align="end">
48
- <DropdownMenuLabel>Actions</DropdownMenuLabel>
49
  <DropdownMenuItem
50
  onClick={() => navigator.clipboard.writeText(record.id)}
51
  >
52
- Copy payment ID
53
  </DropdownMenuItem>
54
  <DropdownMenuSeparator />
55
- <DropdownMenuItem>View customer</DropdownMenuItem>
56
- <DropdownMenuItem>View payment details</DropdownMenuItem>
 
 
 
 
 
57
  </DropdownMenuContent>
58
  </DropdownMenu>
59
  </section>
 
4
  DropdownMenu,
5
  DropdownMenuContent,
6
  DropdownMenuItem,
 
7
  DropdownMenuSeparator,
8
  DropdownMenuTrigger,
9
  } from '@/components/ui/dropdown-menu';
10
+ import { useDownloadFile } from '@/hooks/file-manager-hooks';
11
  import { IFile } from '@/interfaces/database/file-manager';
12
  import { CellContext } from '@tanstack/react-table';
13
  import { EllipsisVertical, Link2, Trash2 } from 'lucide-react';
14
  import { useCallback } from 'react';
15
+ import { useTranslation } from 'react-i18next';
16
+ import {
17
+ UseHandleConnectToKnowledgeReturnType,
18
+ UseRenameCurrentFileReturnType,
19
+ } from './hooks';
20
 
21
  type IProps = Pick<CellContext<IFile, unknown>, 'row'> &
22
+ Pick<UseHandleConnectToKnowledgeReturnType, 'showConnectToKnowledgeModal'> &
23
+ Pick<UseRenameCurrentFileReturnType, 'showFileRenameModal'>;
24
 
25
+ export function ActionCell({
26
+ row,
27
+ showConnectToKnowledgeModal,
28
+ showFileRenameModal,
29
+ }: IProps) {
30
+ const { t } = useTranslation();
31
  const record = row.original;
32
+ const documentId = record.id;
33
+ const { downloadFile } = useDownloadFile();
34
 
35
  const handleShowConnectToKnowledgeModal = useCallback(() => {
36
  showConnectToKnowledgeModal(record);
37
  }, [record, showConnectToKnowledgeModal]);
38
 
39
+ const onDownloadDocument = useCallback(() => {
40
+ downloadFile({
41
+ id: documentId,
42
+ filename: record.name,
43
+ });
44
+ }, [documentId, downloadFile, record.name]);
45
+
46
+ const handleShowFileRenameModal = useCallback(() => {
47
+ showFileRenameModal(record);
48
+ }, [record, showFileRenameModal]);
49
+
50
  return (
51
  <section className="flex gap-4 items-center">
52
  <Button
 
68
  </Button>
69
  </DropdownMenuTrigger>
70
  <DropdownMenuContent align="end">
 
71
  <DropdownMenuItem
72
  onClick={() => navigator.clipboard.writeText(record.id)}
73
  >
74
+ {t('common.move')}
75
  </DropdownMenuItem>
76
  <DropdownMenuSeparator />
77
+ <DropdownMenuItem onClick={handleShowFileRenameModal}>
78
+ {t('common.rename')}
79
+ </DropdownMenuItem>
80
+ <DropdownMenuSeparator />
81
+ <DropdownMenuItem onClick={onDownloadDocument}>
82
+ {t('common.download')}
83
+ </DropdownMenuItem>
84
  </DropdownMenuContent>
85
  </DropdownMenu>
86
  </section>
web/src/pages/files/files-table.tsx CHANGED
@@ -14,6 +14,7 @@ import {
14
  import { ArrowUpDown } from 'lucide-react';
15
  import * as React from 'react';
16
 
 
17
  import SvgIcon from '@/components/svg-icon';
18
  import { TableEmpty, TableSkeleton } from '@/components/table-skeleton';
19
  import { Badge } from '@/components/ui/badge';
@@ -41,7 +42,11 @@ import { getExtension } from '@/utils/document-util';
41
  import { useMemo } from 'react';
42
  import { useTranslation } from 'react-i18next';
43
  import { ActionCell } from './action-cell';
44
- import { useHandleConnectToKnowledge, useNavigateToOtherFolder } from './hooks';
 
 
 
 
45
  import { LinkToDatasetDialog } from './link-to-dataset-dialog';
46
 
47
  export function FilesTable() {
@@ -64,6 +69,14 @@ export function FilesTable() {
64
  onConnectToKnowledgeOk,
65
  connectToKnowledgeLoading,
66
  } = useHandleConnectToKnowledge();
 
 
 
 
 
 
 
 
67
 
68
  const { pagination, data, loading, setPagination } = useFetchFileList();
69
 
@@ -208,6 +221,7 @@ export function FilesTable() {
208
  <ActionCell
209
  row={row}
210
  showConnectToKnowledgeModal={showConnectToKnowledgeModal}
 
211
  ></ActionCell>
212
  );
213
  },
@@ -341,6 +355,14 @@ export function FilesTable() {
341
  loading={connectToKnowledgeLoading}
342
  ></LinkToDatasetDialog>
343
  )}
 
 
 
 
 
 
 
 
344
  </div>
345
  );
346
  }
 
14
  import { ArrowUpDown } from 'lucide-react';
15
  import * as React from 'react';
16
 
17
+ import { RenameDialog } from '@/components/rename-dialog';
18
  import SvgIcon from '@/components/svg-icon';
19
  import { TableEmpty, TableSkeleton } from '@/components/table-skeleton';
20
  import { Badge } from '@/components/ui/badge';
 
42
  import { useMemo } from 'react';
43
  import { useTranslation } from 'react-i18next';
44
  import { ActionCell } from './action-cell';
45
+ import {
46
+ useHandleConnectToKnowledge,
47
+ useNavigateToOtherFolder,
48
+ useRenameCurrentFile,
49
+ } from './hooks';
50
  import { LinkToDatasetDialog } from './link-to-dataset-dialog';
51
 
52
  export function FilesTable() {
 
69
  onConnectToKnowledgeOk,
70
  connectToKnowledgeLoading,
71
  } = useHandleConnectToKnowledge();
72
+ const {
73
+ fileRenameVisible,
74
+ showFileRenameModal,
75
+ hideFileRenameModal,
76
+ onFileRenameOk,
77
+ initialFileName,
78
+ fileRenameLoading,
79
+ } = useRenameCurrentFile();
80
 
81
  const { pagination, data, loading, setPagination } = useFetchFileList();
82
 
 
221
  <ActionCell
222
  row={row}
223
  showConnectToKnowledgeModal={showConnectToKnowledgeModal}
224
+ showFileRenameModal={showFileRenameModal}
225
  ></ActionCell>
226
  );
227
  },
 
355
  loading={connectToKnowledgeLoading}
356
  ></LinkToDatasetDialog>
357
  )}
358
+ {fileRenameVisible && (
359
+ <RenameDialog
360
+ hideModal={hideFileRenameModal}
361
+ onOk={onFileRenameOk}
362
+ initialName={initialFileName}
363
+ loading={fileRenameLoading}
364
+ ></RenameDialog>
365
+ )}
366
  </div>
367
  );
368
  }
web/src/pages/files/hooks.ts CHANGED
@@ -90,6 +90,10 @@ export const useRenameCurrentFile = () => {
90
  };
91
  };
92
 
 
 
 
 
93
  export const useSelectBreadcrumbItems = () => {
94
  const parentFolderList = useFetchParentFolderList();
95
 
 
90
  };
91
  };
92
 
93
+ export type UseRenameCurrentFileReturnType = ReturnType<
94
+ typeof useRenameCurrentFile
95
+ >;
96
+
97
  export const useSelectBreadcrumbItems = () => {
98
  const parentFolderList = useFetchParentFolderList();
99