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

Add Twenty Shared & Fix profile image rendering #8841

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

mdrazak2001
Copy link

@mdrazak2001 mdrazak2001 commented Dec 3, 2024

PR Summary:

  1. Added Twenty Shared Package to centralize utilitiies as mentioned in Fix broken image urls in Settings > Profile and Invite To Workspace Email #8942
  2. Optimization of getImageAbsoluteURI.ts to handle edge cases

image

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR modifies the image URL construction logic to fix profile picture rendering issues, specifically addressing the "File not found" error when displaying profile images.

  • Modified packages/twenty-front/src/utils/image/getImageAbsoluteURI.ts to use direct string concatenation instead of URL object manipulation
  • Removed URL encoding/normalization to prevent issues with special characters in image URLs
  • Simplified handling of URL path construction by removing leading slash checks

💡 (2/5) Greptile learns from your feedback when you react with 👍/👎!

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile


return serverFilesUrl.toString() as ImageAbsoluteURI<T>;
};
return `${serverFilesUrl}/files/${imageUrl}` as ImageAbsoluteURI<T>;
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: No handling for leading slashes - if imageUrl starts with '/', the result will have double slashes ('//files//'). Add back the previous slash handling logic.

Copy link

github-actions bot commented Dec 3, 2024

Welcome!

Hello there, congrats on your first PR! We're excited to have you contributing to this project.
By submitting your Pull Request, you acknowledge that you agree with the terms of our Contributor License Agreement.

Generated by 🚫 dangerJS against 179c0d5

@FelixMalfait
Copy link
Member

cc @AMoreaux fyi

Thank you @mdrazak2001!
I think it might be a good opportunity to create a twenty-shared... It's a mess to have 3 times the same function 😅

Do you think you could take a shot at it today? Or should we take over on your PR?
Thanks a lot

@AMoreaux
Copy link
Contributor

AMoreaux commented Dec 3, 2024

Hello 👋

Thanks for the contribution.

Before adding the fix can you add a test including the URL pattern that broke the function?

In this file:
https://github.com/twentyhq/twenty/blob/main/packages/twenty-front/src/utils/image/__tests__/getImageAbsoluteURI.test.ts

Moreover, since you're on this file. Could you add this test? It validate the fact we manage the risk of double slash.

  it('should return fully formed url if imageUrl is a relative url and start with a /', () => {
    const imageUrl = '/XXX';
    const result = getImageAbsoluteURI(imageUrl);
    expect(result).toBe('http://localhost:3000/files/XXX');
  });

The URL API allows you to validate these kinds of use cases. You don't need to manage the double // or the validation of the URI yourself.

If you need help or have a lack of time feel free to tell me ;)

@AMoreaux AMoreaux self-assigned this Dec 3, 2024
@AMoreaux AMoreaux added -PR: awaiting author scope: front Issues that are affecting the frontend side only labels Dec 3, 2024
@mdrazak2001
Copy link
Author

cc @AMoreaux fyi

Thank you @mdrazak2001! I think it might be a good opportunity to create a twenty-shared... It's a mess to have 3 times the same function 😅

Do you think you could take a shot at it today? Or should we take over on your PR? Thanks a lot

Great suggestion! I'll work on creating a twenty-shared utility to consolidate this function across the codebase.
As part of consolidating this utility into a shared implementation, would you like me to replace all occurrences of getImageAbsoluteURI across the codebase and remove the duplicate implementations in this PR itself?

@FelixMalfait
Copy link
Member

@mdrazak2001 yes that'd be great. It might be a bit difficult to do a good configuration so let us know if you're stuck!

@mdrazak2001
Copy link
Author

mdrazak2001 commented Dec 3, 2024

Hello 👋

Thanks for the contribution.

Before adding the fix can you add a test including the URL pattern that broke the function?

In this file: https://github.com/twentyhq/twenty/blob/main/packages/twenty-front/src/utils/image/__tests__/getImageAbsoluteURI.test.ts

Moreover, since you're on this file. Could you add this test? It validate the fact we manage the risk of double slash.

  it('should return fully formed url if imageUrl is a relative url and start with a /', () => {
    const imageUrl = '/XXX';
    const result = getImageAbsoluteURI(imageUrl);
    expect(result).toBe('http://localhost:3000/files/XXX');
  });

The URL API allows you to validate these kinds of use cases. You don't need to manage the double // or the validation of the URI yourself.

If you need help or have a lack of time feel free to tell me ;)

@AMoreaux:
Thank you for the detailed review!

  1. I will add a test case for the specific URL pattern (?) that initially caused the bug to ensure the issue is addressed.
  2. I’ll also include the additional test case you suggested, to validate proper handling of relative Image/File URLs starting with a /.

Planned Solution:
Upon reviewing, I identified that the root cause was the improper manual manipulation of pathname, which led to the incorrect encoding of ? as %3F. Based on your suggestion to leverage the URL API, I plan to implement a solution using its base URL resolution capabilities. This approach will ensure:

  • Proper handling of leading slashes.
  • Correct resolution of query parameters.
  • Prevention of unnecessary encoding issues.

I’ll make these updates shortly. Please let me know if there’s anything else you'd like me to address!

@mdrazak2001 mdrazak2001 force-pushed the fix/profile-image-display branch from 87a2031 to 5be9a09 Compare December 4, 2024 18:00
@mdrazak2001 mdrazak2001 changed the title Fix profile image rendering Add Twenty Shared & Fix profile image rendering Dec 5, 2024
@mdrazak2001
Copy link
Author

Hi @FelixMalfait @AMoreaux
I’ve committed the initial setup and created the centralized twenty-shared library as suggested.
Please review it at your convenience and let me know if you have any concerns or feedback.
Thanks!

Copy link
Contributor

@AMoreaux AMoreaux left a comment

Choose a reason for hiding this comment

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

Lots of works, thanks 🙏

Few comments. TLDR:

  • Use the barrel file
  • Remove all reference to react or tsx in twenty-shared
  • If you want you can remove the type in getImageAbsoluteURI.ts

I will test it in the browser asap.

Comment on lines 6 to 18
"include": [
"**/__mocks__/**/*",
"jest.config.ts",
"setupTests.ts",
"src/**/*.d.ts",
"src/**/*.spec.ts",
"src/**/*.spec.tsx",
"src/**/*.test.ts",
"src/**/*.test.tsx",
"tsup.config.ts",
"tsup.ui.index.tsx",
"vite.config.ts"
]
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
"include": [
"**/__mocks__/**/*",
"jest.config.ts",
"setupTests.ts",
"src/**/*.d.ts",
"src/**/*.spec.ts",
"src/**/*.spec.tsx",
"src/**/*.test.ts",
"src/**/*.test.tsx",
"tsup.config.ts",
"tsup.ui.index.tsx",
"vite.config.ts"
]
"include": [
"jest.config.ts",
"setupTests.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"vite.config.ts"
]

It seems some includes are not unused

"**/*.spec.jsx",
"**/*.test.jsx"
],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
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
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts"]

This package shouldn't include tsx components since we have twenty-ui for that.

ignorePatterns: ['!**/*'],
overrides: [
{
files: ['*.ts', '*.tsx'],
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
files: ['*.ts', '*.tsx'],
files: ['*.ts'],

We will not have tsx in this package;

@@ -0,0 +1,15 @@
module.exports = {
extends: ['../../.eslintrc.cjs', '../../.eslintrc.react.cjs'],
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
extends: ['../../.eslintrc.cjs', '../../.eslintrc.react.cjs'],
extends: ['../../.eslintrc.cjs'],

We will not use react in this package;

Comment on lines 23 to 27
global: {
statements: 60,
lines: 60,
functions: 50,
},
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
global: {
statements: 60,
lines: 60,
functions: 50,
},
global: {
statements: 95,
lines: 95,
functions: 95,
},

Since this package will be shared and should contain only easy-to-test functions we can have a high level of coverage.

Comment on lines 1 to 3
type ImageAbsoluteURI<T extends string | null | undefined> = T extends string
? string
: null;
Copy link
Contributor

Choose a reason for hiding this comment

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

We talked internally, and we would like to remove this type. It will impact the function's return type.
I planned to do it myself but if you want to do it feel free :)

@@ -17,7 +17,8 @@
"baseUrl": ".",
"paths": {
"twenty-emails": ["packages/twenty-emails/src/index.ts"],
"twenty-ui": ["packages/twenty-ui/src/index.ts"]
"twenty-ui": ["packages/twenty-ui/src/index.ts"],
"twenty-shared": ["packages/twenty-shared/dist"]
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
"twenty-shared": ["packages/twenty-shared/dist"]
"twenty-shared": ["packages/twenty-shared/src/index.ts"]

Please target the barrel file here packages/twenty-shared/src/index.ts instead of targeting dist

@@ -10,7 +10,8 @@
"types": ["node"],
"outDir": "../../.cache/tsc",
"paths": {
"@ui/*": ["packages/twenty-ui/src/*"]
"@ui/*": ["packages/twenty-ui/src/*"],
"twenty-shared": ["packages/twenty-shared/dist"]
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
"twenty-shared": ["packages/twenty-shared/dist"]
"twenty-shared": ["packages/twenty-shared/src/*"]

Please target the barrel file here packages/twenty-shared/src/index.ts instead of targeting dist

"baseUrl": "."
"baseUrl": ".",
"paths": {
"twenty-shared": ["packages/twenty-shared/dist"]
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
"twenty-shared": ["packages/twenty-shared/dist"]
"twenty-shared": ["packages/twenty-shared/src/*"]

@@ -24,7 +24,8 @@
"@/*": ["packages/twenty-front/src/modules/*"],
"~/*": ["packages/twenty-front/src/*"],
"twenty-ui": ["packages/twenty-ui/src/index.ts"],
"@ui/*": ["packages/twenty-ui/src/*"]
"@ui/*": ["packages/twenty-ui/src/*"],
"twenty-shared": ["packages/twenty-shared/src"]
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
"twenty-shared": ["packages/twenty-shared/src"]
"twenty-shared": ["packages/twenty-shared/src/index.ts"]

@mdrazak2001 mdrazak2001 requested a review from AMoreaux December 7, 2024 05:50
…profile-image-display

# Conflicts:
#	packages/twenty-emails/src/emails/send-invite-link.email.tsx
#	packages/twenty-emails/src/utils/getImageAbsoluteURI.ts
#	packages/twenty-front/src/modules/auth/components/Logo.tsx
#	packages/twenty-front/src/modules/navigation/components/AppNavigationDrawer.tsx
#	packages/twenty-front/src/modules/object-metadata/utils/getAvatarUrl.ts
#	packages/twenty-front/src/modules/ui/input/components/ImageInput.tsx
#	packages/twenty-front/src/modules/ui/navigation/navigation-drawer/components/MultiWorkspaceDropdownButton.tsx
#	packages/twenty-front/src/modules/ui/utilities/page-favicon/components/PageFavicon.tsx
#	packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminFeatureFlags.tsx
#	packages/twenty-front/src/pages/settings/serverless-functions/__stories__/SettingsServerlessFunctionDetail.stories.tsx
#	packages/twenty-ui/src/display/avatar/components/Avatar.tsx
#	packages/twenty-ui/src/utilities/image/__tests__/getImageAbsoluteURI.test.ts
#	packages/twenty-ui/src/utilities/image/getImageAbsoluteURI.ts
Refactor the code to streamline the usage of `getImageAbsoluteURI` by storing its result in a variable before rendering. Cleanup unnecessary imports and minor code improvements were applied across several files. This should enhance code readability and maintainability.
Refactor URI handling in `getImageAbsoluteURI` by separating imageUrl and serverUrl checks for better clarity. Update favicon link in HTML and ensure consistency by encompassing logo checking logic inside the JSX return statement. Additional import of `useGetPublicWorkspaceDataBySubdomain` in `WorkspaceProviderEffect` to manage loading state.
Copy link
Contributor

@AMoreaux AMoreaux left a comment

Choose a reason for hiding this comment

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

Perfect 💯

Thanks a lot for your contribution. I am waiting for the approval of @FelixMalfait for the next steps.

@charlesBochet
Copy link
Member

@mdrazak2001 thank you! I'll merge this one today. I have unfortunately merged another fix in the meantime but your PR is bringing more, so I'll rebase it on main and merge it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-PR: awaiting review scope: front Issues that are affecting the frontend side only
Projects
Status: 🔖 Planned
Development

Successfully merging this pull request may close these issues.

4 participants