-
Notifications
You must be signed in to change notification settings - Fork 57
feat: add a 'save' button in peripheral device settings, instead of autosave #1572
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { useCallback, useMemo } from 'react' | ||
| import { useCallback, useMemo, useState } from 'react' | ||
| import { Studios } from '../../../../collections/index.js' | ||
| import { StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids' | ||
| import { useTracker } from '../../../../lib/ReactMeteorData/ReactMeteorData.js' | ||
|
|
@@ -30,6 +30,13 @@ export function StudioIngestSubDevices({ | |
|
|
||
| const studio = useTracker(() => Studios.findOne(studioId), [studioId]) | ||
|
|
||
| const [unsavedOverrides, setUnsavedOverrides] = useState<SomeObjectOverrideOp[] | undefined>(undefined) | ||
|
|
||
| const baseSettings = useMemo( | ||
| () => studio?.peripheralDeviceSettings?.ingestDevices ?? wrapDefaultObject({}), | ||
| [studio?.peripheralDeviceSettings?.ingestDevices] | ||
| ) | ||
|
|
||
| const saveOverrides = useCallback( | ||
| (newOps: SomeObjectOverrideOp[]) => { | ||
| if (studio?._id) { | ||
|
|
@@ -43,17 +50,25 @@ export function StudioIngestSubDevices({ | |
| [studio?._id] | ||
| ) | ||
|
|
||
| const baseSettings = useMemo( | ||
| () => studio?.peripheralDeviceSettings?.ingestDevices ?? wrapDefaultObject({}), | ||
| [studio?.peripheralDeviceSettings?.ingestDevices] | ||
| ) | ||
| const settingsWithOverrides = useMemo(() => { | ||
| if (unsavedOverrides) { | ||
| return { | ||
| ...baseSettings, | ||
| overrides: unsavedOverrides, | ||
| } | ||
| } | ||
| return baseSettings | ||
| }, [baseSettings, unsavedOverrides]) | ||
|
|
||
| const overrideHelper = useOverrideOpHelper(saveOverrides, baseSettings) | ||
| const batchedOverrideHelper = useOverrideOpHelper(setUnsavedOverrides, settingsWithOverrides) | ||
| const instantSaveOverrideHelper = useOverrideOpHelper(saveOverrides, settingsWithOverrides) | ||
|
|
||
| const wrappedSubDevices = useMemo( | ||
| () => | ||
| getAllCurrentAndDeletedItemsFromOverrides<StudioIngestDevice>(baseSettings, (a, b) => a[0].localeCompare(b[0])), | ||
| [baseSettings] | ||
| getAllCurrentAndDeletedItemsFromOverrides<StudioIngestDevice>(settingsWithOverrides, (a, b) => | ||
| a[0].localeCompare(b[0]) | ||
| ), | ||
| [settingsWithOverrides] | ||
| ) | ||
|
|
||
| const filteredPeripheralDevices = useMemo( | ||
|
|
@@ -85,7 +100,42 @@ export function StudioIngestSubDevices({ | |
| 'peripheralDeviceSettings.ingestDevices.overrides': addOp, | ||
| }, | ||
| }) | ||
| }, [studioId, wrappedSubDevices]) | ||
| }, [wrappedSubDevices, settingsWithOverrides.overrides]) | ||
|
|
||
| // key is subDevice's old id, value is it's new id if it was changed | ||
| const [updatedIds, setUpdatedIds] = useState(new Map<string, string>()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would really appreciate a comment here describing what the strings in key and value of this Map actually mean, that the key is the oldId for a sub-device, and the value is the newId.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| const updateObjectId = useCallback( | ||
| (oldId: string, newId: string) => { | ||
| if (oldId === newId) return | ||
|
|
||
| batchedOverrideHelper().changeItemId(oldId, newId).commit() | ||
| setUpdatedIds((prev) => new Map(prev).set(oldId, newId)) | ||
| }, | ||
| [batchedOverrideHelper, setUpdatedIds] | ||
| ) | ||
|
|
||
| const discardChanges = useCallback(() => { | ||
| setUnsavedOverrides(undefined) | ||
| setUpdatedIds(new Map<string, string>()) | ||
| }, []) | ||
|
|
||
| const saveChanges = useCallback(() => { | ||
| if (studio?._id && unsavedOverrides) { | ||
| Studios.update(studio._id, { | ||
| $set: { | ||
| 'peripheralDeviceSettings.ingestDevices.overrides': unsavedOverrides, | ||
| }, | ||
| }) | ||
| setUnsavedOverrides(undefined) | ||
| } | ||
|
|
||
| if (updatedIds.size > 0) { | ||
| setUpdatedIds(new Map<string, string>()) | ||
| } | ||
| }, [studio?._id, unsavedOverrides]) | ||
anteeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const hasUnsavedChanges = useMemo(() => !!unsavedOverrides || updatedIds.size > 0, [unsavedOverrides, updatedIds]) | ||
|
|
||
| return ( | ||
| <div className="mb-4"> | ||
|
|
@@ -101,8 +151,14 @@ export function StudioIngestSubDevices({ | |
|
|
||
| <GenericSubDevicesTable | ||
| subDevices={wrappedSubDevices} | ||
| overrideHelper={overrideHelper} | ||
| overrideHelper={batchedOverrideHelper} | ||
| peripheralDevices={filteredPeripheralDevices} | ||
| hasUnsavedChanges={!!unsavedOverrides} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| instantSaveOverrideHelper={instantSaveOverrideHelper} | ||
| saveChanges={saveChanges} | ||
| discardChanges={discardChanges} | ||
| updateObjectId={updateObjectId} | ||
| updatedIds={updatedIds} | ||
| /> | ||
|
|
||
| <div className="my-1 mx-2"> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.