Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP-3310] Implement Refresh Functionality for FilesManager Data (by feature key) #2201

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions libs/device/feature/src/lib/api-features/api-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ export class APIFeaturesService {
}

@IpcEvent(APIFeaturesServiceEvents.FeatureData)
public async getFeatureData(feature: string): Promise<ResultObject<unknown>> {
const device = this.deviceProtocol.apiDevice
public async getFeatureData({
feature,
deviceId,
}: {
feature: string
deviceId?: DeviceId
}): Promise<ResultObject<unknown>> {
const device = deviceId
? this.deviceProtocol.getAPIDeviceById(deviceId)
: this.deviceProtocol.apiDevice

if (!device) {
return Result.failed(new AppError(GeneralError.NoDevice, ""))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { ipcRenderer } from "electron-better-ipc"
import { Feature } from "generic-view/models"
import { APIFeaturesServiceEvents } from "device/models"
import { ResultObject } from "Core/core/builder"
import { DeviceId } from "Core/device/constants/device-id"

export const getFeatureDataRequest = (
deviceId: DeviceId,
feature: string
): Promise<ResultObject<Feature["data"]>> => {
return ipcRenderer.callMain(APIFeaturesServiceEvents.FeatureData, {
deviceId,
feature,
})
}
1 change: 1 addition & 0 deletions libs/device/feature/src/lib/api-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./api-features.service"
export * from "./get-overview-data.request"
export * from "./get-overview-configuration.request"
export * from "./get-feature-configuration.request"
export * from "./get-feature-data.request"
2 changes: 1 addition & 1 deletion libs/generic-view/models/src/lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DeviceProperties } from "device-manager/models"
import { ApiConfig, MenuConfig, OverviewData } from "device/models"
import { DeviceId } from "Core/device/constants/device-id"

interface Feature<Data = Record<string, unknown>> {
export interface Feature<Data = Record<string, unknown>> {
config?: View
data?: Data
}
Expand Down
26 changes: 24 additions & 2 deletions libs/generic-view/models/src/lib/mc-file-manager-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@

import { z } from "zod"

const dataValidator = z.undefined()
const storageCategoryValidator = z.object({
spaceUsedBytes: z.number(),
spaceUsedString: z.string(),
storageCategory: z.string(),
})

export const mcFileManagerData = z.object({
storageInformation: z.array(
z.object({
storageType: z.string(),
totalSpaceBytes: z.number(),
usedSpaceBytes: z.number(),
totalSpaceString: z.string(),
usedSpaceString: z.string(),
categoriesSpaceInformation: z.record(
z.string(),
storageCategoryValidator
),
})
),
})

export type McFileManagerData = z.infer<typeof mcFileManagerData>

const configValidator = z.object({
entityTypes: z.array(z.string()).min(1),
Expand All @@ -15,6 +37,6 @@ export type McFileManagerView = z.infer<typeof configValidator>

export const mcFileManagerView = {
key: "mc-file-manager-view",
dataValidator,
dataValidator: mcFileManagerData,
configValidator,
} as const
5 changes: 4 additions & 1 deletion libs/generic-view/models/src/lib/paragraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import { z } from "zod"
import { commonTextValidators } from "./common-text-validators"

const dataValidator = z.undefined()
const dataValidator = z.object({
text: z.string().optional(),
})
export type ParagraphData = z.infer<typeof dataValidator>

const configValidator = z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ export enum FeaturesActions {
GetGenericData = "api-actions-get-generic-data",
GetAllFeatures = "api-actions-get-all-features",
GetSingleFeature = "api-actions-get-single-feature",
GetSingleFeatureConfig = "api-actions-get-single-feature-config",
GetSingleFeatureData = "api-actions-get-single-feature-data",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { createAsyncThunk } from "@reduxjs/toolkit"
import { Feature } from "generic-view/models"
import { getFeatureDataRequest } from "device/feature"
import { DeviceId } from "Core/device/constants/device-id"
import { ReduxRootState } from "Core/__deprecated__/renderer/store"
import { selectConfiguredDevice } from "../selectors"
import { FeaturesActions } from "./featues-action-keys"
import { transformDataComponents } from "./transform-data-components"

export const getGenericData = createAsyncThunk<
{
deviceId: DeviceId
feature: string
data: Feature["data"]
},
{ deviceId: DeviceId; feature: string },
{ state: ReduxRootState }
>(
FeaturesActions.GetGenericData,
async ({ deviceId, feature }, { rejectWithValue, getState }) => {
const { ok, data } = await getFeatureDataRequest(deviceId, feature)

const features = selectConfiguredDevice(getState(), deviceId)?.features
const featureConfig = features?.[feature]?.config

if (featureConfig === undefined) {
return rejectWithValue("no device")
}

if (ok) {
return {
deviceId,
feature,
data: transformDataComponents(feature, data, featureConfig),
}
}

return rejectWithValue(undefined)
}
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import { DeviceId } from "Core/device/constants/device-id"
import { ReduxRootState } from "Core/__deprecated__/renderer/store"
import { FeaturesActions } from "./featues-action-keys"
import { getOverviewData } from "./get-overview-data.actions"
import { getGenericData } from "./get-generic-data.actions"

export const getSingleFeatureData = createAsyncThunk<
undefined,
{ deviceId: DeviceId; feature: string },
{ state: ReduxRootState }
>(
FeaturesActions.GetSingleFeature,
async ({ deviceId, feature }, { getState, dispatch }) => {
async ({ deviceId, feature }, { dispatch }) => {
switch (feature) {
case "mc-overview":
await dispatch(getOverviewData({ deviceId }))
break
default:
await dispatch(getGenericData({ deviceId, feature }))
}

return undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FeaturesActions } from "./featues-action-keys"
import { getOverviewConfig } from "./get-overview-config.actions"
import { getOverviewData } from "./get-overview-data.actions"
import { getGenericConfig } from "./get-generic-config.actions"
import { getGenericData } from "./get-generic-data.actions"

export const getSingleFeatures = createAsyncThunk<
undefined,
Expand All @@ -25,6 +26,7 @@ export const getSingleFeatures = createAsyncThunk<
break
default:
await dispatch(getGenericConfig({ deviceId, feature }))
await dispatch(getGenericData({ deviceId, feature }))
break
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { Feature } from "generic-view/models"
import { View } from "generic-view/utils"
import { generatedData } from "../../../../ui/src/lib/generated"

export const transformDataComponents = (
feature: string,
data: Feature["data"],
config: View
): Feature["data"] => {
return generatedData[feature](data, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/

import { View } from "generic-view/utils"
import { generated } from "../../../../ui/src/lib/generated"
import { generatedViews } from "../../../../ui/src/lib/generated"

export const transformGenericComponents = (view: View): View => {
const fullView = { ...view }
for (const [key, { component, config, layout }] of Object.entries(fullView)) {
const transformComponent = generated[component as keyof typeof generated]
const transformComponent =
generatedViews[component as keyof typeof generatedViews]
if (transformComponent) {
Object.assign(
fullView,
Expand Down
12 changes: 12 additions & 0 deletions libs/generic-view/store/src/lib/views/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getAPIAny } from "../get-api-any"
import { getMenuConfig } from "../get-menu-config"
import { getOutboxData } from "../outbox/get-outbox-data.action"
import { getGenericConfig } from "../features/get-generic-config.actions"
import { getGenericData } from "../features/get-generic-data.actions"
import {
addDevice,
removeDevice,
Expand Down Expand Up @@ -163,6 +164,17 @@ export const genericViewsReducer = createReducer(initialState, (builder) => {
},
}
})
builder.addCase(getGenericData.fulfilled, (state, action) => {
const { deviceId, feature, data } = action.payload
state.lastResponse = action.payload
state.devices[deviceId].features = {
...state.devices[deviceId].features,
[feature]: {
config: state.devices[deviceId].features?.[feature]?.config,
data,
},
}
})
builder.addCase(setDeviceState, (state, action) => {
if (action.payload) {
const id = action.payload.id
Expand Down
14 changes: 12 additions & 2 deletions libs/generic-view/ui/src/lib/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@
*/

import {
Feature,
mcContactsView,
mcFileManagerView,
mcImportContactsButton,
} from "generic-view/models"
import { generateMcContactsView } from "./mc-contacts-view"
import { generateMcFileManagerView } from "./mc-file-manager-view/mc-file-manager-view"
import { generateMcFileManagerView } from "./mc-file-manager/mc-file-manager-view"
import { generateFileManagerData } from "./mc-file-manager/mc-file-manager-data"
import { generateMcImportContactsButton } from "./mc-import-contacts-button"
import { View } from "generic-view/utils"

export * from "./mc-import-contacts-button"
export * from "./mc-contacts-view"

export const generated = {
export const generatedViews = {
[mcContactsView.key]: generateMcContactsView,
[mcFileManagerView.key]: generateMcFileManagerView,
[mcImportContactsButton.key]: generateMcImportContactsButton,
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type generatedDataFn = (data: any, config: View) => Feature["data"]

export const generatedData: Record<string, generatedDataFn> = {
["fileManager"]: generateFileManagerData,
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ const generateFileCategoryListItem = ({
[`${id}CategoryListItemStorageText`]: {
component: "p3-component",
config: {
text: "0 KB",
text: "0",
color: "black",
textTransform: "format-bytes",
textTransformOptions: {
minUnit: "KB",
},
},
},
[`${id}CategoryListItemStorageMarker`]: {
Expand Down
Loading
Loading