cheeaun / phanpy

A minimalistic opinionated Mastodon web client
https://phanpy.social
MIT License
1.08k stars 107 forks source link

Crash when clicking compose button #52

Closed kkoyung closed 1 year ago

kkoyung commented 1 year ago

Describe the bug I logged in my GoToSocial server with Phanpy. When I click the compose button at the right botton corner, the browser popped out the alert message "Failed to load instance configuration. Please try again.". After clicking "OK", the browser reload the web app. Therefore, I cannot create new post.

To Reproduce Steps to reproduce the behavior:

  1. Login an account on a GoToSocial server.
  2. Click the compose button at the right-bottom corner.
  3. An alert with the message "Failed to load instance configuration. Please try again." popped out.
  4. After clicking "OK", the browser reload the web app.

Expected behavior Display the compose dialog to create a new post.

Investigation The alert comes from the following code in src/components/compose.jsx:

  const currentAccount = getCurrentAccount();
  const currentAccountInfo = currentAccount.info;

  const configuration = useMemo(() => {
    try {
      const instances = store.local.getJSON('instances');
      const currentInstance = currentAccount.instanceURL.toLowerCase();
      const config = instances[currentInstance].configuration;
      console.log(config);
      return config;
    } catch (e) {
      console.error(e);
      alert('Failed to load instance configuration. Please try again.');
      // Temporary fix for corrupted data
      store.local.del('instances');
      location.reload();
      return {};
    }
  }, []);

According to the function initMasto(params) in src/app.jsx, since GoToSocial uses v1 Mastodon api, the uri "https://mydomain.com", rather than the domain "mydomain.com", is used as the index of a member of instances. So, the index of the member in instances at line 6 above contains the protocol part.

I found that in the function onSubmit in src/pages/login.jsx, the the protocol part is always removed from instanceURL. This value then becomes the value of currentAccount.instanceURL. So, the value of currentInstance at line 7 above doesn't contain the protocal part.

Hence, at line 8 above, the configuration cannot be retrieved, and then an error is catched.

Solution I tried to change a line in initMasto(params) function in app.jsx from

      instances[(domain || uri).toLowerCase()] = info;

to

      instances[(domain || uri.replace(/(^\w+:|^)\/\//, '')).toLowerCase()] = info;

to make sure no protocol part is including the index of the member of instances. It works with my GoToSocial server.

However, I am not sure whether the whole uri was used intentionally for other purposes. So, I simply create an issue, instead of a PR.

Desktop (please complete the following information):

Smartphone (please complete the following information):

GoToSocial Version GoToSocial 0.6.0 git-f9e5ec9

Phanpy version Phanpy git-305bb92

cheeaun commented 1 year ago

Thanks for the detailed bug report! I assume you're using the dev site, and I've pushed a possible fix, can you try again? 🙏

kkoyung commented 1 year ago

I've tested with the newest commit. It works fine for you now. Thanks for your quick response. I will close this issue.

Reference: the related commit for fixing this bug - Strip protocol from instance domain/uri