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

- [FP-2787](https://movai.atlassian.net/browse/FP-2787): IDE - Topics - Can't open multiple Topics tabs #323

Closed
wants to merge 16 commits into from

Conversation

quirinpa
Copy link
Contributor

@quirinpa quirinpa commented Jul 12, 2024

@quirinpa quirinpa self-assigned this Jul 12, 2024
@quirinpa quirinpa requested a review from a team as a code owner July 12, 2024 11:30
@quirinpa quirinpa force-pushed the FP-2787-ide-topics-cant-open-multiple-topics-tabs branch 3 times, most recently from 5924241 to 1af29ce Compare July 18, 2024 11:15
diasdm
diasdm previously approved these changes Jul 31, 2024
diasdm
diasdm previously approved these changes Jul 31, 2024
@quirinpa quirinpa force-pushed the FP-2787-ide-topics-cant-open-multiple-topics-tabs branch from 72c9aeb to da801f3 Compare August 2, 2024 13:06
@quirinpa quirinpa requested a review from diasdm August 19, 2024 13:32
Copy link
Collaborator

@diasdm diasdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the Sonar issues pls

const toolIdData = toolIds[toolName] ?? { last: 0, free: [] };
const id = toolIdData.free.shift() || toolIdData.last++;
const tabData = { ...getToolTabData({ scope: toolName, id: toolName + "-" + id }, props) };
if (tabData.multiple) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming suggestion: multiple -> multipleTabs?
It's easy to know what it means when looking at it from the PR, but by the code itself, it's not that clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've talked about this. I think in the context we could agree it refers to tabs

return {
id: id,
rid: rid,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What rid stands for? realID? Might be good to rename it...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realId, yes, but I think it isn't needed.

@@ -356,6 +356,16 @@ const useTabLayout = (props, dockRef) => {

// Remove tab and apply new layout
tabsById.current.delete(tabId);
const nid = new Number(tabId.substring(tabId.lastIndexOf("-") + 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is nId numberId? subID?

Copy link
Contributor Author

@quirinpa quirinpa Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numberId. Renamed

@@ -356,6 +356,16 @@ const useTabLayout = (props, dockRef) => {

// Remove tab and apply new layout
tabsById.current.delete(tabId);
const nid = new Number(tabId.substring(tabId.lastIndexOf("-") + 1));
if (!isNaN(nid)) {
const toolIds = tabsById.current.get("toolIds");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the PR, but We might want to rename tabsById -> tabsByIdRef. Without the ref it looks like we are talking about the "current tab"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I don't think this should be a ref, because a global would suffice. But done.

Copy link
Collaborator

@diasdm diasdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls compute the toolId when loading the page

src/tools/index.js Outdated Show resolved Hide resolved
src/tools/index.js Outdated Show resolved Hide resolved
tabData.name += "-" + id;
tabData.title += "-" + id;
} else
tabData.id = toolName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be moved to getToolTabData?

Copy link
Contributor Author

@quirinpa quirinpa Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else part can but I don't think it should

@quirinpa
Copy link
Contributor Author

A comment is missing. David asked to compute the toolIds object instead of loading from localStorage

Copy link
Contributor

@manuelnogueiramov manuelnogueiramov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did this review BEFORE you went on vacation, and I kinda forgot to click submit review 🤦
I'm ok with this one as is :)
Good work 👍

@@ -627,6 +637,7 @@ const useTabLayout = (props, dockRef) => {
if (!tabFromMemory && !data.content) return;
const {
id,
rid,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're no longer using rid, we can get rid of it (pun intended!) 😆

Comment on lines 71 to 123
function breakToolId(tabId) {
const index = tabId.search(/\d/);
const alpha = tabId.substring(0, index - 1);

if (index < 0)
return [alpha];

const number = new Number(tabId.substring(index));

return [alpha, number];
}

function computeIds(tabsMap) {
const res = {};

for (const [tabId] of tabsMap) {
const [alpha, number] = breakToolId(tabId);
const index = tabId.search(/\d/);
if (index < 0)
continue;
const specific = res[alpha] ?? { last: 0, busy: {}, free: [] };
specific.busy[number] = 1;
if (number > specific.last)
specific.last = number;
res[alpha] = specific;
}

for (const toolName in res) {
const specific = res[toolName];
for (let j = 0; j < specific.last; j++)
if (!specific.busy[j])
specific.free.push(j);
delete specific.busy;
specific.last++;
}

return res;
}

const workspace = new Workspace();
const tabsMap = workspace.getTabs();
const toolIds = computeIds(tabsMap);

export function freeToolId(tabId) {
const [alpha, number] = breakToolId(tabId);

if (!number)
return;

const toolIdData = toolIds[alpha];
toolIdData.free.unshift(number);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually coming to review this again, I'm trying to understand this code, and looks so much more, less performant and more complex than what I did here in the src/utils/Utils.js to achieve the same thing. Can you confirm @EdwardAngeles @quirinpa ?

Copy link
Contributor Author

@quirinpa quirinpa Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That only runs when the page loads.
It is to compute the things instead of using localStorage.
After the initial time it runs, it's pretty fast.
Faster than using local storage, for sure.

The fact that goes until the last number doesn't actually add a lot of overhead since we make sure to keep those numbers low. Even if we had 10000 tabs, We wouldn't see a lot of slowness when loading the page.

Copy link
Contributor

@manuelnogueiramov manuelnogueiramov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a couple of suggestions that may improve the code. Take a look and let me know if you have any questions or would like to do a small pair session.

tl;dr;
Overall, I like the logic of your thinking. I just think you made it harder to understand with generic, non-related naming in most vars and I don't think all this code belongs in this file.

function computeIds(tabsMap) {
const res = {};

/* get a map of this format:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get from where? Maybe you meant create?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are computing. But also creating.
I don't see this as a strong reason to hold the PR

const index = tabId.search(/\d/);
if (index < 0)
continue;
const specific = res[alpha] ?? { last: 0, busy: {}, free: [] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is specific? Maybe since we're doing this only for tool tabs, why not toolTab?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting object is not a tool tab. But indeed something specific to that tool.

Comment on lines 72 to 82
function breakToolId(tabId) {
const index = tabId.search(/\d/);
const alpha = tabId.substring(0, index - 1);

if (index < 0)
return [alpha];

const number = new Number(tabId.substring(index));

return [alpha, number];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You wouldn't need this entire function if you reused the multiple property to be the tabIncrement like per mine and @diasdm's suggestion. Since you're cycling through the tabs that come from localStorage you'd have access to the entire tabData which includes the multiple, or whatever name we call that property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems overly complicated when this simple function suffices perfectly.

* to efficently organize tool ids.
*/
function computeIds(tabsMap) {
const res = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const res = {};
const toolTabIncrementMap = {};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res comes from "result". I don't think "toolTabIncrementMap is a good name.

* }
*/
for (const [tabId] of tabsMap) {
const [alpha, number] = breakToolId(tabId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [alpha, number] = breakToolId(tabId);
const [toolName, tabIncrement] = breakToolId(tabId);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you get rid of the breakToolId function, you can use something like:

const {scope, tabIncrement} = tabData;

Copy link
Contributor Author

@quirinpa quirinpa Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a strong reason to modify what we're storing there.
A strange idea to be computing part, and using localStorage to store another part.
At least lets be consistent.

Comment on lines 126 to 128
const workspace = new Workspace();
const tabsMap = workspace.getTabs();
const toolIds = computeIds(tabsMap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate that we are just floating around in the middle of this generalFunctions file. I think you might be able to come up with some better way to do this.
Also, maybe all this "tool tab" logic should be in its own file, since none of these are actual "general functions" but very specific ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is only used here. We already have too many files.

Copy link

sonarcloud bot commented Aug 28, 2024

@diasdm
Copy link
Collaborator

diasdm commented Sep 4, 2024

Replaced by #334

@diasdm diasdm closed this Sep 4, 2024
@diasdm diasdm deleted the FP-2787-ide-topics-cant-open-multiple-topics-tabs branch September 4, 2024 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants