-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
馃搧 added drive file preview modal (#2748)
* added drive file preview modal * link components * use drive display * link the views * added controls * revert the message viewer changes
- Loading branch information
Showing
12 changed files
with
335 additions
and
148 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
twake/frontend/src/app/features/drive/hooks/use-drive-preview.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import fileUploadApiClient from 'app/features/files/api/file-upload-api-client'; | ||
import fileUploadService from 'app/features/files/services/file-upload-service'; | ||
import { useGlobalEffect } from 'app/features/global/hooks/use-global-effect'; | ||
import { LoadingState } from 'app/features/global/state/atoms/Loading'; | ||
import { useRecoilState } from 'recoil'; | ||
import { DriveApiClient } from '../api-client/api-client'; | ||
import { DriveViewerState } from '../state/viewer'; | ||
import { DriveItem } from '../types'; | ||
|
||
export const useDrivePreviewModal = () => { | ||
const [status, setStatus] = useRecoilState(DriveViewerState); | ||
|
||
const open: (item: DriveItem) => void = (item: DriveItem) => { | ||
if (item.last_version_cache?.file_metadata?.source === 'internal') { | ||
setStatus({ item, loading: true }); | ||
} | ||
}; | ||
|
||
const close = () => setStatus({ item: null, loading: true }); | ||
|
||
return { open, close, isOpen: !!status.item }; | ||
}; | ||
|
||
export const useDrivePreview = () => { | ||
const [status, setStatus] = useRecoilState(DriveViewerState); | ||
const modal = useDrivePreviewModal(); | ||
|
||
useGlobalEffect( | ||
'useDrivePreview', | ||
async () => { | ||
if (modal.isOpen && status.item) { | ||
setStatus({ | ||
...status, | ||
loading: true, | ||
}); | ||
|
||
const details = await DriveApiClient.get(status.item.company_id, status.item.id); | ||
|
||
setStatus({ | ||
...status, | ||
details, | ||
loading: false, | ||
}); | ||
} | ||
}, | ||
[status.item?.id], | ||
); | ||
|
||
return { | ||
...modal, | ||
status, | ||
loading: status.loading, | ||
}; | ||
}; | ||
|
||
export const useDrivePreviewLoading = () => { | ||
const [loading, setLoading] = useRecoilState(LoadingState('useDrivePreviewLoading')); | ||
|
||
return { loading, setLoading }; | ||
}; | ||
|
||
export const useDrivePreviewDisplayData = () => { | ||
const { status } = useDrivePreview(); | ||
|
||
if (!status) { | ||
return {}; | ||
} | ||
|
||
const name = | ||
status.details?.item.last_version_cache.file_metadata.name || status.details?.item.name || ''; | ||
const extension = name.split('.').pop(); | ||
const type = fileUploadApiClient.mimeToType(status.details?.item.last_version_cache.file_metadata.mime || '', extension); | ||
const id = status.details?.item.last_version_cache.file_metadata.external_id || ''; | ||
const download = fileUploadService.getDownloadRoute({ | ||
companyId: status.item?.company_id || '', | ||
fileId: status.details?.item.last_version_cache.file_metadata.external_id || '' | ||
}); | ||
|
||
return { download, id, name, type, extension } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { atom } from 'recoil'; | ||
import { DriveItem, DriveItemDetails } from '../types'; | ||
|
||
export const DriveViewerState = atom<{ | ||
item: null | DriveItem; | ||
details?: DriveItemDetails; | ||
loading: boolean; | ||
}>({ | ||
key: "DriveViewerState", | ||
default: { | ||
item: null, | ||
loading: true | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
twake/frontend/src/app/views/applications/viewer/controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
twake/frontend/src/app/views/applications/viewer/drive-display.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
useDrivePreviewDisplayData, | ||
useDrivePreviewLoading, | ||
useDrivePreviewModal, | ||
} from 'app/features/drive/hooks/use-drive-preview'; | ||
import ImageDisplay from './images/display'; | ||
import VideoDisplay from './videos/display'; | ||
import PdfDisplay from './pdf/display'; | ||
import CodeDisplay from './code/display'; | ||
import ArchiveDisplay from './archive/display'; | ||
import OtherDisplay from './other/display'; | ||
|
||
export default (): React.ReactElement => { | ||
const { download, type, name, id } = useDrivePreviewDisplayData(); | ||
const { isOpen } = useDrivePreviewModal(); | ||
const { loading, setLoading } = useDrivePreviewLoading(); | ||
|
||
if (!download || !isOpen) { | ||
return <></>; | ||
} | ||
|
||
if (!type) { | ||
return ( | ||
<div className="text-white m-auto w-full text-center h-full flex items-center"> | ||
<span className="block w-full text-center">We can't display this document.</span> | ||
</div> | ||
); | ||
} | ||
|
||
switch (type) { | ||
case 'image': | ||
return <ImageDisplay loading={loading} setLoading={setLoading} download={download} />; | ||
|
||
case 'video': | ||
case 'audio': | ||
return <VideoDisplay download={download} />; | ||
|
||
case 'code': | ||
return <CodeDisplay download={download} name={name} />; | ||
|
||
case 'archive': | ||
return <ArchiveDisplay download={download} name={name} />; | ||
|
||
case 'pdf': | ||
return <PdfDisplay download={download} name={name} />; | ||
|
||
default: | ||
return <OtherDisplay download={download} name={name} id={id} />; | ||
} | ||
}; |
Oops, something went wrong.