Files
spacedrive/interface/app/$libraryId/Explorer/ContextMenu/FilePath/OpenWith.tsx
nikec 5603392087 [ENG-972] quick view improvements (#1233)
* quick preview improvements

* fix ts

* improvements

* fix merge

* non-indexed support

* Update index.tsx

* Update pnpm-lock.yaml

* update quick preview

* sidebar icon weight

* fix thumb

* ts

* fix focus

* remove usePortal

* quick preview store

* Update index.tsx

* Update index.tsx

* cleanup

* add tooltip to name

* hide nav buttons and match explorer nav buttons

---------

Co-authored-by: Jamie Pine <32987599+jamiepine@users.noreply.github.com>
2023-09-08 11:45:37 +00:00

77 lines
1.9 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 { Platform, usePlatform } from '~/util/Platform';
import { ConditionalItem } from '../ConditionalItem';
import { useContextMenuContext } from '../context';
export default new ConditionalItem({
useCondition: () => {
const { selectedFilePaths } = useContextMenuContext();
const { getFilePathOpenWithApps, openFilePathWith } = usePlatform();
if (!getFilePathOpenWithApps || !openFilePathWith) return null;
if (selectedFilePaths.some((p) => p.is_dir)) return null;
return { getFilePathOpenWithApps, openFilePathWith };
},
Component: ({ getFilePathOpenWithApps, openFilePathWith }) => (
<Menu.SubMenu label="Open with">
<Suspense>
<Items
actions={{
getFilePathOpenWithApps,
openFilePathWith
}}
/>
</Suspense>
</Menu.SubMenu>
)
});
const Items = ({
actions
}: {
actions: Required<Pick<Platform, 'getFilePathOpenWithApps' | 'openFilePathWith'>>;
}) => {
const { selectedFilePaths } = useContextMenuContext();
const { library } = useLibraryContext();
const ids = selectedFilePaths.map((obj) => obj.id);
const items = useQuery<unknown>(
['openWith', ids],
() => actions.getFilePathOpenWithApps(library.uuid, ids),
{ suspense: true }
);
return (
<>
{Array.isArray(items.data) && items.data.length > 0 ? (
items.data.map((data, id) => (
<Menu.Item
key={id}
onClick={async () => {
try {
await actions.openFilePathWith(
library.uuid,
ids.map((id) => [id, data.url])
);
} catch (e) {
toast.error(`Failed to open file, with: ${data.url}`);
}
}}
>
{data.name}
</Menu.Item>
))
) : (
<p className="w-full text-center text-sm text-gray-400"> No apps available </p>
)}
</>
);
};