Skip to content

Commit

Permalink
feat(devices): add type for display device type icon
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-r committed Jan 12, 2024
1 parent c786b93 commit d8be6ae
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
27 changes: 24 additions & 3 deletions src/components/ButtonAddOrEditDevice.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ActionIcon, Button, Flex, Space, TextInput } from "@mantine/core";
import {
ActionIcon,
Button,
Flex,
Select,
Space,
TextInput,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { notifications } from "@mantine/notifications";
import { IconEdit, IconPlus } from "@tabler/icons-react";
Expand All @@ -7,27 +14,36 @@ import { useTranslation } from "react-i18next";

import { db } from "../database";
import { useSetSettings, useSettings } from "../providers/Settings";
import type { RemoteDevice, Settings } from "../types/interfaces/Settings";
import type {
RemoteDevice,
RemoteDeviceType,
Settings,
} from "../types/interfaces/Settings";
import { Form } from "./Form";
import { Modal } from "./Modal";

interface ButtonAddOrEditDeviceProps {
initialValues?: {
id: string;
name: string;
type: RemoteDeviceType;
};
}

interface FormData {
id: string;
name: string;
type: RemoteDeviceType;
}

const INITIAL_VALUES = {
id: "",
name: "",
type: "desktop",
} as FormData;

const REMOTE_DEVICES_ICONS_LABEL = ["desktop", "tablet", "mobile"];

export const ButtonAddOrEditDevice: FC<ButtonAddOrEditDeviceProps> = memo(
({ initialValues }) => {
const [opened, setOpened] = useState(false);
Expand Down Expand Up @@ -79,7 +95,6 @@ export const ButtonAddOrEditDevice: FC<ButtonAddOrEditDeviceProps> = memo(
color: "green",
});

form.reset();
setOpened(false);
};

Expand Down Expand Up @@ -133,6 +148,12 @@ export const ButtonAddOrEditDevice: FC<ButtonAddOrEditDeviceProps> = memo(
label={t("modal.device.add.input.name.label")}
{...form.getInputProps("name")}
/>
<Space h="sm" />
<Select
label={t("modal.device.add.select.type.label")}
data={REMOTE_DEVICES_ICONS_LABEL}
{...form.getInputProps("type")}
/>
<Flex gap={8} justify="flex-end" mt="xl">
<Button onClick={() => setOpened(false)} color="gray">
{t("cancel")}
Expand Down
5 changes: 3 additions & 2 deletions src/components/ButtonDevicesAvailable.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ActionIcon, Box, Button, Menu, Text } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { IconDeviceDesktop, IconPlayerPlay } from "@tabler/icons-react";
import { IconDeviceDesktop } from "@tabler/icons-react";
import { type FC, memo } from "react";
import { useTranslation } from "react-i18next";

import { usePlayerAudio, usePlayerVideo } from "../providers/Player";
import { useRemoteDevices } from "../providers/Settings";
import { sendToRemoteDevice } from "../services/remotePlay";
import type { RemoteDevice } from "../types/interfaces/Settings";
import { DeviceIcon } from "./DeviceList";

interface ButtonDevicesAvailableProps {
variant: "icon" | "text";
Expand Down Expand Up @@ -65,7 +66,7 @@ export const ButtonDevicesAvailable: FC<ButtonDevicesAvailableProps> = memo(
<Menu.Dropdown>
{remoteDevices.map((device) => (
<Menu.Item
leftSection={<IconPlayerPlay size={14} />}
leftSection={<DeviceIcon type={device.type} size={16} />}
onClick={() => handleRemotePlay(device)}
>
<Text size="sm" lineClamp={1}>
Expand Down
34 changes: 30 additions & 4 deletions src/components/DeviceList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Alert, Flex, Table, Text } from "@mantine/core";
import {
IconDeviceDesktop,
IconDeviceMobile,
IconDeviceTablet,
} from "@tabler/icons-react";
import { type FC, memo, useMemo } from "react";
import { useTranslation } from "react-i18next";

import { useSettings } from "../providers/Settings";
import type { RemoteDevice } from "../types/interfaces/Settings";
import type {
RemoteDevice,
RemoteDeviceType,
} from "../types/interfaces/Settings";
import { ButtonAddOrEditDevice } from "./ButtonAddOrEditDevice";
import { ButtonDeleteDevice } from "./ButtonDeleteDevice";

Expand All @@ -28,8 +36,8 @@ export const DevicesList = memo(() => {
<Table role="list" aria-label="Devices list" highlightOnHover mb="md">
<Table.Thead>
<Table.Tr>
<Table.Th w="30%">Name</Table.Th>
<Table.Th w="50%">ID</Table.Th>
<Table.Th w="20%">Name</Table.Th>
<Table.Th w="60%">UUID</Table.Th>
<Table.Th w="20%" style={{ textAlign: "right" }}>
Actions
</Table.Th>
Expand All @@ -46,6 +54,19 @@ export const DevicesList = memo(() => {
);
});

const DeviceTypeIcon = {
desktop: IconDeviceDesktop,
tablet: IconDeviceTablet,
mobile: IconDeviceMobile,
};

export const DeviceIcon = memo(
({ type, size }: { type: RemoteDeviceType; size?: number }) => {
const Icon = DeviceTypeIcon[type];
return <Icon size={size} />;
},
);

interface DeviceItemProps {
device: RemoteDevice;
}
Expand All @@ -54,7 +75,12 @@ const DeviceItem: FC<DeviceItemProps> = memo(({ device }) => {
return (
<Table.Tr>
<Table.Td>
<strong>{device.name}</strong>
<strong>
<Flex align="center" gap="sm">
<DeviceIcon type={device.type} />
{device.name}
</Flex>
</strong>
</Table.Td>
<Table.Td>
<strong>{device.id}</strong>
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"modal.device.add.input.id.description": "The Device identifier (uuid) is available in the settings (General tab) of the application on the device you wish to add",
"modal.device.add.input.name.label": "Name",
"modal.device.add.input.name.placeholder": "Device name",
"modal.device.add.select.type.label": "Device type",
"modal.device.add.notification.message": "Device added",
"modal.device.edit.title": "Update device",
"modal.device.edit.notification.message": "Device updated",
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"modal.device.add.input.id.description": "L'identifiant de l'appareil (uuid) est disponible dans les paramètres (onglet Général) de l'application sur l'appareil que vous souhaitez ajouter",
"modal.device.add.input.name.label": "Nom",
"modal.device.add.input.name.placeholder": "Nom de l'appareil",
"modal.device.add.select.type.label": "Type d'appareil",
"modal.device.add.notification.message": "Appareil ajouté",
"modal.device.edit.title": "Modifier l'appareil",
"modal.device.edit.notification.message": "Appareil modifié",
Expand Down
3 changes: 3 additions & 0 deletions src/types/interfaces/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Instance } from "./Instance";

export type RemoteDeviceType = "desktop" | "tablet" | "mobile";

export interface RemoteDevice {
id: string;
name: string;
type: "desktop" | "tablet" | "mobile";
createdAt: string;
updatedAt: string;
}
Expand Down

0 comments on commit d8be6ae

Please sign in to comment.