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

[WIP] Feat/issue 2581/list of variables are used #2784

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions companion/lib/Instance/Variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class InstanceVariable extends CoreBase {
/**
* @param {import('../Registry.js').default} registry
*/


constructor(registry) {
super(registry, 'Instance/Variable')

Expand Down Expand Up @@ -313,6 +315,74 @@ class InstanceVariable extends CoreBase {
client.onPromise('variables:instance-values', (label) => {
return this.#variableValues[label]
})

client.onPromise('variables:get-instances', (name) => {
const result = new Set()

const usageTypes = new Map([
["style", "name"],
["feedbacks", "feedbacks"],
["steps", "action"]
]);
// name -> style
// feedback -> feedbacks
// action -> steps

for (let [controlId, control] of this.registry.controls.getAllControls()) {
const parsedControl = control.toJSON();

if (typeof parsedControl != "string") {
for (const propName in parsedControl) {
if (JSON.stringify(parsedControl[propName]).includes(name)) {
const location = this.registry.page.getLocationOfControlId(controlId)
const formattedLocation = location?.pageNumber + "/" + location?.row + "/" + location?.column
const formattedUsageType = usageTypes.get(propName) == undefined ? "unknown" : usageTypes.get(propName)

result.add({
"buttonName": formattedLocation, "usageType": formattedUsageType
})
}
}
}
}

return Array.from(result)
})
}

/**
* @param {any} obj
* @param {string} str
*/
objectContainsString(obj, str) {
if (typeof obj !== 'object' || obj === null) {
return false;
}

const seenObjects = new Set(); // To handle circular references

/**
* @param {any} obj
*/
function checkObject(obj) {
if (seenObjects.has(obj)) {
return false; // Circular reference detected
}

seenObjects.add(obj);

for (const key in obj) {
if (typeof obj[key] === 'string' && obj[key].includes(str)) {
return true;
} else if (typeof obj[key] === 'object' && checkObject(obj[key])) {
return true;
}
}

return false;
}

return checkObject(obj);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion shared-lib/lib/SocketIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export interface ClientToBackendEventsMap {
'connections:get-help': (id: string) => [err: string, result: null] | [err: null, result: HelpDescription]

'variables:instance-values': (label: string) => CompanionVariableValues | undefined
'variables:get-instances': (name: string) => { buttonName: string; usageType: string }[]


'presets:subscribe': () => Record<string, Record<string, UIPresetDefinition> | undefined>
'presets:unsubscribe': () => void
Expand Down Expand Up @@ -376,4 +378,4 @@ export type ClientToBackendEventsListenMap = {
: (
...args: Parameters<ClientToBackendEventsMap[K]>
) => Promise<ReturnType<ClientToBackendEventsMap[K]>> | ReturnType<ClientToBackendEventsMap[K]>
}
}
47 changes: 46 additions & 1 deletion webui/src/Buttons/CustomVariablesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ export function CustomVariablesList({ setShowCustom }: CustomVariablesListProps)

const [newName, setNewName] = useState('')

const [variableUsage, setVariableUsage] = useState<Map<string, { buttonName: string; usageType: string }[]>>(new Map());

useEffect(() => {
const doPoll = (variableName: string) => {
return socketEmitPromise(socket, 'variables:get-instances', ["$(internal:custom_" + variableName + ")"])
.then((res) => {
setVariableUsage((prevMap) => {
return new Map(prevMap.set(variableName, res));
});
})
.catch((e) => {
console.error('Failed to retrieve instances');
});
}


const doPollAll = () => {
for (let name in allVariableNames) {
doPoll(allVariableNames[name])
}
}

doPollAll()
const interval = setInterval(doPollAll, 1000)

return () => {
clearInterval(interval)
}
}, [socket])

const doCreateNew = useCallback(
(e: FormEvent) => {
e?.preventDefault()
Expand Down Expand Up @@ -266,7 +296,6 @@ export function CustomVariablesList({ setShowCustom }: CustomVariablesListProps)
{candidates &&
candidates.map((info, index) => {
const shortname = `custom_${info.name}`

return (
<CustomVariableRow
key={info.name}
Expand All @@ -283,6 +312,7 @@ export function CustomVariablesList({ setShowCustom }: CustomVariablesListProps)
moveRow={moveRow}
setCollapsed={setPanelCollapsed}
isCollapsed={isPanelCollapsed(info.name)}
usage={variableUsage.get(info.name)}
/>
)
})}
Expand Down Expand Up @@ -334,6 +364,7 @@ interface CustomVariableRowProps {
moveRow: (itemName: string, targetName: string) => void
isCollapsed: boolean
setCollapsed: (name: string, collapsed: boolean) => void
usage: { buttonName: string; usageType: string }[] | undefined
}

function CustomVariableRow({
Expand All @@ -350,6 +381,7 @@ function CustomVariableRow({
moveRow,
isCollapsed,
setCollapsed,
usage
}: CustomVariableRowProps) {
const fullname = `internal:${shortname}`

Expand Down Expand Up @@ -458,6 +490,19 @@ function CustomVariableRow({
</>
)}
</div>
{!isCollapsed && (
<>
<div className="cell-usage">
{usage !== undefined && (
Array.from(usage).map((item: { buttonName: string; usageType: string }, index: number) => (
<div key={index}>
<p>Used by {item.buttonName} for {item.usageType}</p>
</div>
))
)}
</div>
</>
)}
</td>
</tr>
)
Expand Down
Loading