add useSessionForAcctStr
This commit is contained in:
		
							parent
							
								
									634060ed46
								
							
						
					
					
						commit
						da60702403
					
				
					 2 changed files with 39 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
import {
 | 
			
		||||
  Accessor,
 | 
			
		||||
  createContext,
 | 
			
		||||
  createMemo,
 | 
			
		||||
  createRenderEffect,
 | 
			
		||||
  createResource,
 | 
			
		||||
  Signal,
 | 
			
		||||
| 
						 | 
				
			
			@ -76,3 +77,39 @@ function useSessionsRaw() {
 | 
			
		|||
  }
 | 
			
		||||
  return store;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get a session for the specific acct string.
 | 
			
		||||
 *
 | 
			
		||||
 * Acct string is a string in the pattern of `{username}@{site_with_protocol}`,
 | 
			
		||||
 * like `@thislight@https://mastodon.social`, can be used to identify (tempoarily)
 | 
			
		||||
 * an session on the tutu instance.
 | 
			
		||||
 *
 | 
			
		||||
 * The `site_with_protocol` is required.
 | 
			
		||||
 *
 | 
			
		||||
 * - If the username is present, the session matches the username and the site is returned; or,
 | 
			
		||||
 * - If the username is not present, any session on the site is returned; or,
 | 
			
		||||
 * - If no available session available for the pattern, an unauthorised session is returned.
 | 
			
		||||
 *
 | 
			
		||||
 * In an unauthorised session, the `.account` is `undefined` and the `client` is an
 | 
			
		||||
 * unauthorised client for the site. This client may not available for some operations.
 | 
			
		||||
 */
 | 
			
		||||
export function useSessionForAcctStr(acct: Accessor<string>) {
 | 
			
		||||
  const allSessions = useSessions()
 | 
			
		||||
 | 
			
		||||
  return createMemo(() => {
 | 
			
		||||
    const parts = acct().split("@", 2)
 | 
			
		||||
    const [inputUsername, inputSite] = acct().split("@", 2);
 | 
			
		||||
    const authedSession = allSessions().find(
 | 
			
		||||
      (x) =>
 | 
			
		||||
        x.account.site === inputSite &&
 | 
			
		||||
        x.account.inf?.username === inputUsername,
 | 
			
		||||
    );
 | 
			
		||||
    return (
 | 
			
		||||
      authedSession ?? {
 | 
			
		||||
        client: createUnauthorizedClient(inputSite),
 | 
			
		||||
        account: undefined,
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue