Skip to content

Commit

Permalink
Use file info to determine the attached file mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
hdiniz committed Dec 3, 2024
1 parent 0e7d338 commit 0d39296
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
25 changes: 2 additions & 23 deletions components/FilesViewerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type FilesViewerModalProps = {
files?: {
url: string;
name?: string;
info?: { width: number };
info?: { width: number; type: string };
}[];
openFileUrl?: string;
allowOutsideInteraction?: boolean;
Expand All @@ -148,7 +148,6 @@ export default function FilesViewerModal({
const intl = useIntl();
const initialIndex = openFileUrl ? files?.findIndex(f => f.url === openFileUrl) : 0;
const [selectedIndex, setSelectedIndex] = useState(initialIndex);
const [selectedItemContentType, setSelectedItemContentType] = React.useState(null);

React.useEffect(() => {
if (openFileUrl) {
Expand All @@ -166,32 +165,12 @@ export default function FilesViewerModal({
useKeyBoardShortcut({ callback: onArrowLeft, keyMatch: ARROW_LEFT_KEY });

const selectedItem = files?.length ? files?.[selectedIndex] : null;
const selectedItemContentType = selectedItem?.info?.type;

const nbFiles = files?.length || 0;
const hasMultipleFiles = nbFiles > 1;
const contentWrapperRef = React.useRef(null);

React.useEffect(() => {
async function fetchMeta() {
const response = await fetch(selectedItem.url, {
method: 'GET',
redirect: 'follow',
headers: {
Range: 'bytes=0-0',
},
});

setSelectedItemContentType(response.headers.get('Content-Type'));
}

if (selectedItem) {
fetchMeta();
} else {
setSelectedItemContentType(null);
}
return () => setSelectedItemContentType(null);
}, [selectedItem]);

const renderFile = ({ url, name }: { url: string; name?: string; info?: { width: number } }, contentWrapperRef) => {
let content = null;

Expand Down
4 changes: 4 additions & 0 deletions components/expenses/graphql/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export const expensePageExpenseFieldsFragment = gql`
url
file {
id
type
... on ImageFileInfo {
width
}
Expand All @@ -335,6 +336,7 @@ export const expensePageExpenseFieldsFragment = gql`
name
info {
id
type
name
size
... on ImageFileInfo {
Expand Down Expand Up @@ -799,6 +801,7 @@ export const expensesListAdminFieldsFragment = gql`
amount
file {
id
type
... on ImageFileInfo {
width
}
Expand All @@ -815,6 +818,7 @@ export const expensesListAdminFieldsFragment = gql`
name
info {
id
type
... on ImageFileInfo {
width
}
Expand Down

0 comments on commit 0d39296

Please sign in to comment.