Files
spacedrive/interface/app/$libraryId/Explorer/ContextMenu/OpenWith.tsx
Artsiom Voitas 2d78edef4d Added even more i18n translation keys (#2453)
* more translation keys

* added i18n keys for future ObjectKindEnum translation

* more keys

* added more keys

* synced all new translation keys with all languages, translated keys on Belarusian and Russian

* added translation for objectkinds in overview

* added translation function for objectkinds

* added more keys to german locale

* renamed 'asc' and 'desc' keys

* rolled back changes

* added missed key

* there are much more keys, than you can imagine

* fixed misspelling

* removed console.log

* removed function "pluralize", added required plural words keys for each language

* fixed condition, which could've lead to undefined value

* hide filter description for boolean filters
2024-05-04 16:16:49 +00:00

126 lines
3.0 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { Suspense } from 'react';
import { useLibraryContext } from '@sd/client';
import { toast } from '@sd/ui';
import { Menu } from '~/components/Menu';
import { useLocale } from '~/hooks';
import { Platform, usePlatform } from '~/util/Platform';
import { ConditionalItem } from './ConditionalItem';
import { useContextMenuContext } from './context';
export default new ConditionalItem({
useCondition: () => {
const { selectedFilePaths, selectedEphemeralPaths } = useContextMenuContext();
const {
getFilePathOpenWithApps,
openFilePathWith,
getEphemeralFilesOpenWithApps,
openEphemeralFileWith
} = usePlatform();
if (
!getFilePathOpenWithApps ||
!openFilePathWith ||
!getEphemeralFilesOpenWithApps ||
!openEphemeralFileWith
)
return null;
if (selectedFilePaths.some((p) => p.is_dir) || selectedEphemeralPaths.some((p) => p.is_dir))
return null;
return {
getFilePathOpenWithApps,
openFilePathWith,
getEphemeralFilesOpenWithApps,
openEphemeralFileWith
};
},
Component: ({
getFilePathOpenWithApps,
openFilePathWith,
getEphemeralFilesOpenWithApps,
openEphemeralFileWith
}) => {
const { t } = useLocale();
return (
<Menu.SubMenu label={t('open_with')}>
<Suspense>
<Items
actions={{
getFilePathOpenWithApps,
openFilePathWith,
getEphemeralFilesOpenWithApps,
openEphemeralFileWith
}}
/>
</Suspense>
</Menu.SubMenu>
);
}
});
const Items = ({
actions
}: {
actions: Required<
Pick<
Platform,
| 'getFilePathOpenWithApps'
| 'openFilePathWith'
| 'getEphemeralFilesOpenWithApps'
| 'openEphemeralFileWith'
>
>;
}) => {
const { selectedFilePaths, selectedEphemeralPaths } = useContextMenuContext();
const { library } = useLibraryContext();
const ids = selectedFilePaths.map((obj) => obj.id);
const paths = selectedEphemeralPaths.map((obj) => obj.path);
const { t } = useLocale();
const items = useQuery<unknown>(
['openWith', ids, paths],
() => {
if (ids.length > 0) return actions.getFilePathOpenWithApps(library.uuid, ids);
else if (paths.length > 0) return actions.getEphemeralFilesOpenWithApps(paths);
else return { data: [] };
},
{ suspense: true }
);
return (
<>
{Array.isArray(items.data) && items.data.length > 0 ? (
items.data.map((data, index) => (
<Menu.Item
key={index}
onClick={async () => {
try {
if (ids.length > 0) {
await actions.openFilePathWith(
library.uuid,
ids.map((id) => [id, data.url])
);
} else if (paths.length > 0) {
await actions.openEphemeralFileWith(
paths.map((path) => [path, data.url])
);
}
} catch (e) {
toast.error(t('failed_to_open_file_with', { data: data.url }));
}
}}
>
{data.name}
</Menu.Item>
))
) : (
<p className="w-full text-center text-sm text-gray-400">{t('no_apps_available')}</p>
)}
</>
);
};