Skip to content

feat: add blob support#634

Closed
NJ-2020 wants to merge 4 commits intoExpensify:mainfrom
NJ-2020:feat/add-blob-support
Closed

feat: add blob support#634
NJ-2020 wants to merge 4 commits intoExpensify:mainfrom
NJ-2020:feat/add-blob-support

Conversation

@NJ-2020
Copy link
Copy Markdown

@NJ-2020 NJ-2020 commented May 10, 2025

Details

Related Issues

$ Expensify/App#9402

Automated Tests

Unit tests were added to cover this fix.

Manual Tests

  1. Apply this changes to E/App
  2. Run & Open the App
  3. Turn off your internet
  4. Go to any report & upload any image
  5. Make sure there are no errors
  6. Refresh the App
  7. Make sure the image is still loads correctly

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
9402_android_mweb.1.1.mp4
iOS: Native
iOS: mWeb Safari
Simulator.Screen.Recording.-.iPhone.15.-.2025-05-12.at.05.40.20.mp4
MacOS: Chrome / Safari
Screen.Recording.2025-05-12.at.06.39.56.mov
MacOS: Desktop
Screen.Recording.2025-05-11.at.22.45.56.1.mov

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 10, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@NJ-2020

This comment was marked as resolved.

@NJ-2020

This comment was marked as resolved.

CLABotify added a commit to Expensify/CLA that referenced this pull request May 11, 2025
@NJ-2020 NJ-2020 marked this pull request as ready for review May 11, 2025 23:57
@NJ-2020 NJ-2020 requested a review from a team as a code owner May 11, 2025 23:57
@melvin-bot melvin-bot Bot requested review from blimpich and removed request for a team May 11, 2025 23:57
Comment thread tests/unit/onyxBlobTest.ts Outdated
Comment on lines +39 to +40
// So we can only check the value is set (not null or undefined)
expect(testBlobValue).toBeTruthy();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then we need a way to check whether it is a Blob. You can try to create a blob with this data and see it has same content as you originally passed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@blimpich
Copy link
Copy Markdown
Contributor

It looks like @deetergp is the CME for the linked issue, assigning him, and unassigning myself

@blimpich blimpich requested review from deetergp and removed request for blimpich May 12, 2025 16:53
@parasharrajat
Copy link
Copy Markdown
Member

What type is returned from Onyx when we retrieve the Blob value?

@NJ-2020
Copy link
Copy Markdown
Author

NJ-2020 commented May 15, 2025

What type is returned from Onyx when we retrieve the Blob value?

The type value that is returned by Onyx is object, because when we storing blob value into IndexedDB, it will only store all the blob values & properties, but it's no longer an instance of the Blob constructor

See more at the last point: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#things_that_dont_work_with_structured_clone

I think maybe the only we can check if the returned value is Blob or not by checking some properties in the returned object, but I am not sure if it's the best solution

@parasharrajat
Copy link
Copy Markdown
Member

But then, how will we know that a collection in Onyx is of type Blob? We need something to differentiate, and we should return blobs from Onyx when we save Blobs.

@NJ-2020
Copy link
Copy Markdown
Author

NJ-2020 commented May 15, 2025

But then, how will we know that a collection in Onyx is of type Blob? We need something to differentiate, and we should return blobs from Onyx when we save Blobs.

Ok..., please HOLD this one, give me some time I'll look into this more deeper 👀 about this one

@NJ-2020 NJ-2020 changed the title feat: add blob support HOLD feat: add blob support May 15, 2025
@parasharrajat
Copy link
Copy Markdown
Member

Feel free to raise this on slack to get confirmation and idea on how Blob support should be implemented. They might have better suggestions.

@NJ-2020 NJ-2020 changed the title HOLD feat: add blob support feat: add blob support May 18, 2025
@NJ-2020
Copy link
Copy Markdown
Author

NJ-2020 commented May 18, 2025

But then, how will we know that a collection in Onyx is of type Blob? We need something to differentiate, and we should return blobs from Onyx when we save Blobs.

Done, fixed


So basically before, when we set a value in Onyx like this one:
const blob = new Blob(["content"]);
Onyx.set("key", blob);
Onyx.get("key").then(res => console.log(res instanceof Blob)) // false

And we check if the value is instanceof blob it will return false, but if we do it like this, it will return true

const blob = new Blob(["content"]);
Onyx.set("key", {
    blobValue: blob
});

Onyx.get("key").then(res => console.log(res.blobValue instanceof Blob)) // true

It's because when we set a value on Onyx, it will remove any null values if the Onyx value is typeof object and not an array

return Storage.setItem(key, valueWithoutNullValues)

const valueWithoutNullValues = valueAfterRemoving as OnyxValue<TKey>;

const {value: valueAfterRemoving, wasRemoved} = OnyxUtils.removeNullValues(key, value);

function removeNullValues<Value extends OnyxInput<OnyxKey> | undefined>(key: OnyxKey, value: Value, shouldRemoveNestedNulls = true): RemoveNullValuesOutput<Value> {
if (value === null) {
remove(key);
return {value, wasRemoved: true};
}
if (value === undefined) {
return {value, wasRemoved: false};
}
// We can remove all null values in an object by merging it with itself
// utils.fastMerge recursively goes through the object and removes all null values
// Passing two identical objects as source and target to fastMerge will not change it, but only remove the null values
return {value: shouldRemoveNestedNulls ? utils.removeNestedNullValues(value) : value, wasRemoved: false};
}

function removeNestedNullValues<TValue extends OnyxInput<OnyxKey> | null>(value: TValue): TValue {
if (typeof value === 'object' && !Array.isArray(value)) {
const objectValue = value as Record<string, unknown>;
return fastMerge(objectValue, objectValue) as TValue;
}
return value;
}

So when we set blob value, and then we check if it's instance of blob it will return false, because some properties might be removed during set item function process

And also on the second example where we set the blob value like this, it returns true, it's because during the remove process, we only remove null values from the object itself, meaning it doesn't go to the child/grandchild properties, so when we check if it's instance of blob it will return true since there are no properties that are got removed

Onyx.set("key", {
    blobValue: blob
});

cc: @parasharrajat

@parasharrajat
Copy link
Copy Markdown
Member

That's interesting. I think we then need to make some changes to overall architecture to allow storing blobs.

I do not want your research time to go in vain. We have a many expert contributors on slack which can suggest and improve your plan saving you time and bugs.

I would suggest that you summarize the whole plan of storing the attachments on all platforms and post it on Slack so that we can get this reviewed. You can later mentioned all the steps that you have already taken and challenges along the way.

This way we can optimize the solution and save us from increasing scope.

@NJ-2020
Copy link
Copy Markdown
Author

NJ-2020 commented May 23, 2025

I will be OOO this weekend 24 - 25 May, so I will back on 26 May

@parasharrajat
Copy link
Copy Markdown
Member

@NJ-2020 is this needed? If not, let's close it.

@NJ-2020 NJ-2020 closed this Jul 23, 2025
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.

3 participants