mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-05-07 18:27:54 +00:00
Format code
This commit is contained in:
parent
fbf4207680
commit
88f4ce4e4d
@ -1,123 +1,148 @@
|
|||||||
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { DndContext, closestCenter } from '@dnd-kit/core';
|
import { DndContext, closestCenter } from '@dnd-kit/core';
|
||||||
import { arrayMove, SortableContext, sortableKeyboardCoordinates, useSortable } from '@dnd-kit/sortable';
|
import {
|
||||||
import { KeyboardSensor, PointerSensor, TouchSensor, useSensor, useSensors } from '@dnd-kit/core';
|
arrayMove,
|
||||||
|
SortableContext,
|
||||||
|
sortableKeyboardCoordinates,
|
||||||
|
useSortable,
|
||||||
|
} from '@dnd-kit/sortable';
|
||||||
|
import {
|
||||||
|
KeyboardSensor,
|
||||||
|
PointerSensor,
|
||||||
|
TouchSensor,
|
||||||
|
useSensor,
|
||||||
|
useSensors,
|
||||||
|
} from '@dnd-kit/core';
|
||||||
import { CSS } from '@dnd-kit/utilities';
|
import { CSS } from '@dnd-kit/utilities';
|
||||||
import { Avatar, ButtonBase } from '@mui/material';
|
import { Avatar, ButtonBase } from '@mui/material';
|
||||||
import { AppCircle, AppCircleContainer, AppCircleLabel } from './Apps-styles';
|
import { AppCircle, AppCircleContainer, AppCircleLabel } from './Apps-styles';
|
||||||
import { getBaseApiReact, MyContext } from '../../App';
|
import { getBaseApiReact } from '../../App';
|
||||||
import { executeEvent } from '../../utils/events';
|
import { executeEvent } from '../../utils/events';
|
||||||
import { settingsLocalLastUpdatedAtom, sortablePinnedAppsAtom } from '../../atoms/global';
|
import {
|
||||||
|
settingsLocalLastUpdatedAtom,
|
||||||
|
sortablePinnedAppsAtom,
|
||||||
|
} from '../../atoms/global';
|
||||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||||
import { saveToLocalStorage } from './AppsNavBar';
|
import { saveToLocalStorage } from './AppsNavBar';
|
||||||
import { ContextMenuPinnedApps } from '../ContextMenuPinnedApps';
|
import { ContextMenuPinnedApps } from '../ContextMenuPinnedApps';
|
||||||
import LockIcon from "@mui/icons-material/Lock";
|
import LockIcon from '@mui/icons-material/Lock';
|
||||||
import { useHandlePrivateApps } from './useHandlePrivateApps';
|
import { useHandlePrivateApps } from './useHandlePrivateApps';
|
||||||
const SortableItem = ({ id, name, app, isDesktop }) => {
|
|
||||||
const {openApp} = useHandlePrivateApps()
|
|
||||||
|
|
||||||
const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id });
|
const SortableItem = ({ id, name, app, isDesktop }) => {
|
||||||
|
const { openApp } = useHandlePrivateApps();
|
||||||
|
|
||||||
|
const { attributes, listeners, setNodeRef, transform, transition } =
|
||||||
|
useSortable({ id });
|
||||||
|
|
||||||
const style = {
|
const style = {
|
||||||
|
backgroundColor: '#f9f9f9',
|
||||||
|
border: '1px solid #ccc',
|
||||||
|
borderRadius: '4px',
|
||||||
|
color: 'black',
|
||||||
|
cursor: 'grab',
|
||||||
|
marginBottom: '5px',
|
||||||
|
padding: '10px',
|
||||||
transform: CSS.Transform.toString(transform),
|
transform: CSS.Transform.toString(transform),
|
||||||
transition,
|
transition,
|
||||||
padding: '10px',
|
|
||||||
border: '1px solid #ccc',
|
|
||||||
marginBottom: '5px',
|
|
||||||
borderRadius: '4px',
|
|
||||||
backgroundColor: '#f9f9f9',
|
|
||||||
cursor: 'grab',
|
|
||||||
color: 'black'
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ContextMenuPinnedApps app={app} isMine={!!app?.isMine}>
|
<ContextMenuPinnedApps app={app} isMine={!!app?.isMine}>
|
||||||
<ButtonBase
|
<ButtonBase
|
||||||
ref={setNodeRef} {...attributes} {...listeners}
|
ref={setNodeRef}
|
||||||
|
{...attributes}
|
||||||
|
{...listeners}
|
||||||
sx={{
|
sx={{
|
||||||
width: "80px",
|
width: '80px',
|
||||||
transform: CSS.Transform.toString(transform),
|
transform: CSS.Transform.toString(transform),
|
||||||
transition,
|
transition,
|
||||||
}}
|
}}
|
||||||
onClick={async ()=> {
|
onClick={async () => {
|
||||||
if(app?.isPrivate){
|
if (app?.isPrivate) {
|
||||||
try {
|
try {
|
||||||
await openApp(app?.privateAppProperties)
|
await openApp(app?.privateAppProperties);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
executeEvent("addTab", {
|
executeEvent('addTab', {
|
||||||
data: app
|
data: app,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<AppCircleContainer sx={{
|
<AppCircleContainer
|
||||||
border: "none",
|
sx={{
|
||||||
gap: isDesktop ? '10px': '5px'
|
border: 'none',
|
||||||
}}>
|
gap: isDesktop ? '10px' : '5px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
<AppCircle
|
<AppCircle
|
||||||
sx={{
|
sx={{
|
||||||
border: "none",
|
border: 'none',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{app?.isPrivate && !app?.privateAppProperties?.logo ? (
|
{app?.isPrivate && !app?.privateAppProperties?.logo ? (
|
||||||
<LockIcon
|
<LockIcon
|
||||||
sx={{
|
sx={{
|
||||||
height: "42px",
|
height: '42px',
|
||||||
width: "42px",
|
width: '42px',
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Avatar
|
<Avatar
|
||||||
sx={{
|
sx={{
|
||||||
height: "42px",
|
height: '42px',
|
||||||
width: "42px",
|
width: '42px',
|
||||||
'& img': {
|
'& img': {
|
||||||
objectFit: 'fill',
|
objectFit: 'fill',
|
||||||
}
|
},
|
||||||
}}
|
}}
|
||||||
alt={app?.metadata?.title || app?.name}
|
alt={app?.metadata?.title || app?.name}
|
||||||
src={ app?.privateAppProperties?.logo ? app?.privateAppProperties?.logo :`${getBaseApiReact()}/arbitrary/THUMBNAIL/${
|
src={
|
||||||
|
app?.privateAppProperties?.logo
|
||||||
|
? app?.privateAppProperties?.logo
|
||||||
|
: `${getBaseApiReact()}/arbitrary/THUMBNAIL/${
|
||||||
app?.name
|
app?.name
|
||||||
}/qortal_avatar?async=true`}
|
}/qortal_avatar?async=true`
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
style={{
|
style={{
|
||||||
width: "31px",
|
width: '31px',
|
||||||
height: "auto",
|
height: 'auto',
|
||||||
}}
|
}}
|
||||||
// src={LogoSelected}
|
// src={LogoSelected}
|
||||||
alt="center-icon"
|
alt="center-icon"
|
||||||
/>
|
/>
|
||||||
</Avatar>
|
</Avatar>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</AppCircle>
|
</AppCircle>
|
||||||
{app?.isPrivate ? (
|
{app?.isPrivate ? (
|
||||||
<AppCircleLabel>
|
<AppCircleLabel>
|
||||||
{`${app?.privateAppProperties?.appName || "Private"}`}
|
{`${app?.privateAppProperties?.appName || 'Private'}`}
|
||||||
</AppCircleLabel>
|
</AppCircleLabel>
|
||||||
) : (
|
) : (
|
||||||
<AppCircleLabel>
|
<AppCircleLabel>{app?.metadata?.title || app?.name}</AppCircleLabel>
|
||||||
{app?.metadata?.title || app?.name}
|
|
||||||
</AppCircleLabel>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</AppCircleContainer>
|
</AppCircleContainer>
|
||||||
</ButtonBase>
|
</ButtonBase>
|
||||||
</ContextMenuPinnedApps>
|
</ContextMenuPinnedApps>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const SortablePinnedApps = ({ isDesktop, myWebsite, myApp, availableQapps = [] }) => {
|
export const SortablePinnedApps = ({
|
||||||
|
isDesktop,
|
||||||
|
myWebsite,
|
||||||
|
myApp,
|
||||||
|
availableQapps = [],
|
||||||
|
}) => {
|
||||||
const [pinnedApps, setPinnedApps] = useRecoilState(sortablePinnedAppsAtom);
|
const [pinnedApps, setPinnedApps] = useRecoilState(sortablePinnedAppsAtom);
|
||||||
const setSettingsLocalLastUpdated = useSetRecoilState(settingsLocalLastUpdatedAtom);
|
const setSettingsLocalLastUpdated = useSetRecoilState(
|
||||||
|
settingsLocalLastUpdatedAtom
|
||||||
|
);
|
||||||
|
|
||||||
const transformPinnedApps = useMemo(() => {
|
const transformPinnedApps = useMemo(() => {
|
||||||
|
|
||||||
// Clone the existing pinned apps list
|
// Clone the existing pinned apps list
|
||||||
let pinned = [...pinnedApps];
|
let pinned = [...pinnedApps];
|
||||||
|
|
||||||
@ -126,12 +151,17 @@ export const SortablePinnedApps = ({ isDesktop, myWebsite, myApp, availableQapp
|
|||||||
if (!appToCheck) return pinnedList;
|
if (!appToCheck) return pinnedList;
|
||||||
|
|
||||||
const existingIndex = pinnedList.findIndex(
|
const existingIndex = pinnedList.findIndex(
|
||||||
(item) => item?.service === appToCheck?.service && item?.name === appToCheck?.name
|
(item) =>
|
||||||
|
item?.service === appToCheck?.service &&
|
||||||
|
item?.name === appToCheck?.name
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingIndex !== -1) {
|
if (existingIndex !== -1) {
|
||||||
// If the app is already in the list, update it with `isMine: true`
|
// If the app is already in the list, update it with `isMine: true`
|
||||||
pinnedList[existingIndex] = { ...pinnedList[existingIndex], isMine: true };
|
pinnedList[existingIndex] = {
|
||||||
|
...pinnedList[existingIndex],
|
||||||
|
isMine: true,
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
// If not in the list, add it with `isMine: true` at the beginning
|
// If not in the list, add it with `isMine: true` at the beginning
|
||||||
pinnedList.unshift({ ...appToCheck, isMine: true });
|
pinnedList.unshift({ ...appToCheck, isMine: true });
|
||||||
@ -149,10 +179,11 @@ export const SortablePinnedApps = ({ isDesktop, myWebsite, myApp, availableQapp
|
|||||||
const findIndex = availableQapps?.findIndex(
|
const findIndex = availableQapps?.findIndex(
|
||||||
(item) => item?.service === pin?.service && item?.name === pin?.name
|
(item) => item?.service === pin?.service && item?.name === pin?.name
|
||||||
);
|
);
|
||||||
if (findIndex !== -1) return {
|
if (findIndex !== -1)
|
||||||
|
return {
|
||||||
...availableQapps[findIndex],
|
...availableQapps[findIndex],
|
||||||
...pin
|
...pin,
|
||||||
}
|
};
|
||||||
|
|
||||||
return pin;
|
return pin;
|
||||||
});
|
});
|
||||||
@ -160,7 +191,6 @@ export const SortablePinnedApps = ({ isDesktop, myWebsite, myApp, availableQapp
|
|||||||
return pinned;
|
return pinned;
|
||||||
}, [myApp, myWebsite, pinnedApps, availableQapps]);
|
}, [myApp, myWebsite, pinnedApps, availableQapps]);
|
||||||
|
|
||||||
|
|
||||||
const sensors = useSensors(
|
const sensors = useSensors(
|
||||||
useSensor(PointerSensor, {
|
useSensor(PointerSensor, {
|
||||||
activationConstraint: {
|
activationConstraint: {
|
||||||
@ -183,23 +213,39 @@ export const SortablePinnedApps = ({ isDesktop, myWebsite, myApp, availableQapp
|
|||||||
if (!over) return; // Make sure the drop target exists
|
if (!over) return; // Make sure the drop target exists
|
||||||
|
|
||||||
if (active.id !== over.id) {
|
if (active.id !== over.id) {
|
||||||
const oldIndex = transformPinnedApps.findIndex((item) => `${item?.service}-${item?.name}` === active.id);
|
const oldIndex = transformPinnedApps.findIndex(
|
||||||
const newIndex = transformPinnedApps.findIndex((item) => `${item?.service}-${item?.name}` === over.id);
|
(item) => `${item?.service}-${item?.name}` === active.id
|
||||||
|
);
|
||||||
|
const newIndex = transformPinnedApps.findIndex(
|
||||||
|
(item) => `${item?.service}-${item?.name}` === over.id
|
||||||
|
);
|
||||||
|
|
||||||
const newOrder = arrayMove(transformPinnedApps, oldIndex, newIndex);
|
const newOrder = arrayMove(transformPinnedApps, oldIndex, newIndex);
|
||||||
setPinnedApps(newOrder);
|
setPinnedApps(newOrder);
|
||||||
saveToLocalStorage('ext_saved_settings','sortablePinnedApps', newOrder)
|
saveToLocalStorage('ext_saved_settings', 'sortablePinnedApps', newOrder);
|
||||||
setSettingsLocalLastUpdated(Date.now())
|
setSettingsLocalLastUpdated(Date.now());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
|
<DndContext
|
||||||
<SortableContext items={transformPinnedApps.map((app) => `${app?.service}-${app?.name}`)}>
|
sensors={sensors}
|
||||||
|
collisionDetection={closestCenter}
|
||||||
|
onDragEnd={handleDragEnd}
|
||||||
|
>
|
||||||
|
<SortableContext
|
||||||
|
items={transformPinnedApps.map((app) => `${app?.service}-${app?.name}`)}
|
||||||
|
>
|
||||||
{transformPinnedApps.map((app) => (
|
{transformPinnedApps.map((app) => (
|
||||||
<SortableItem isDesktop={isDesktop} key={`${app?.service}-${app?.name}`} id={`${app?.service}-${app?.name}`} name={app?.name} app={app} />
|
<SortableItem
|
||||||
|
isDesktop={isDesktop}
|
||||||
|
key={`${app?.service}-${app?.name}`}
|
||||||
|
id={`${app?.service}-${app?.name}`}
|
||||||
|
name={app?.name}
|
||||||
|
app={app}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</SortableContext>
|
</SortableContext>
|
||||||
</DndContext>
|
</DndContext>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user