Skip to content

feat(SchemaViewer): calculate column width based on data #1885

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

Merged
merged 6 commits into from
Jan 30, 2025

Conversation

Raubzeug
Copy link
Contributor

@Raubzeug Raubzeug commented Jan 29, 2025

closes #1011
Stand

CI Results

Test Status: ❌ FAILED

πŸ“Š Full Report

Total Passed Failed Flaky Skipped
262 261 1 0 0

😟 No changes in tests. πŸ˜•

Bundle Size: βœ…

Current: 80.19 MB | Main: 80.19 MB
Diff: +2.23 KB (0.00%)

βœ… Bundle size unchanged.

ℹ️ CI Information
  • Test recordings for failed tests are available in the full report.
  • Bundle size is measured for the entire 'dist' directory.
  • πŸ“Š indicates links to detailed reports.
  • πŸ”Ί indicates increase, πŸ”½ decrease, and βœ… no change in bundle size.

@@ -136,5 +159,7 @@ export function getRowTableColumns(
rowTableColumns.push(autoIncrementColumn);
}

return rowTableColumns;
console.log(normalizeColumns(rowTableColumns, data));
Copy link
Member

Choose a reason for hiding this comment

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

console.log

data: dataSlice,
name: column.name,
header: typeof column.header === 'string' ? column.header : undefined,
sortable: column.sortable === undefined,
Copy link
Member

Choose a reason for hiding this comment

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

sortable could be true, I'd just pass column.sortable here

Comment on lines +32 to +34
if (
maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING >=
MAX_COLUMN_WIDTH
Copy link
Member

Choose a reason for hiding this comment

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

sortPadding is not used here

Comment on lines 44 to 46
function hasProperty(obj: KeyValueRow | SchemaData, key: string): obj is KeyValueRow {
return key in obj;
}
Copy link
Member

Choose a reason for hiding this comment

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

This type guard actually implicitly do as KeyValueRow to any passed Object type value without any real check. Although it have no influence right now, it's generally not very good practice - it's harder to find error here than in explicit as type, since you expect checks in type guards. And it's less error proof in case we want to make some changes.

You can do following to get rid of any assertions:

  1. Set type T to Record<string, unknown> (getColumnWidth<Record<string, unknown>>)
  2. Convert SchemaData from interface to type so there will be no Index signature for type 'string' is missing error

This will allow to get rid of hasProperty helper and also will make this function more general purpose, since it won't be with specific types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Genius! I've have had so much pain with these types yesterday.

Copy link
Member

Choose a reason for hiding this comment

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

I found this difference between types and interfaces myself only yesterday. Here the issue, that you could read: microsoft/TypeScript#15300, and the most explanatory comment about it: microsoft/TypeScript#15300 (comment)

Comment on lines 30 to 31
const sortPadding =
sortable && maxColumnContentLength <= headerContentLength ? SORT_ICON_PADDING : 0;
Copy link
Member

Choose a reason for hiding this comment

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

There is a problem with such calculations. In the example your code will return 70, while it should return 78:

maxColumnContentLength = 5
headerContentLength = 4
sortable = true

sortPadding = 0

actualColumnContentLength = 10 * 5 + 20 // 70
actualHeaderContentLength = 10 * 4 + 18 + 20 // 78

You may assume, that sort icon length is equal to 2 characters and just add this to headerContentLength - there will be 2px difference, you won't need to adjust you calculations for sortPadding

const headerContentLength = typeof header === 'string' ? header.length : name.length;
const headerContentLengthWithSort = sortable ? headerContentLength + 2 : headerContentLength

Or you may compare actual widths, but not length in characters

@Raubzeug Raubzeug added this pull request to the merge queue Jan 30, 2025
Merged via the queue into main with commit 85f19c3 Jan 30, 2025
6 of 7 checks passed
@Raubzeug Raubzeug deleted the 1011-schema-column-width branch January 30, 2025 09:47
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.

Schema table - use content width for initial columns width
2 participants