Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.7 KB
Raw Blame
import { useBlockId } from '@core/block';

import { storageServiceClient } from '@service-storage/client';
import { createCallback } from '@solid-primitives/rootless';
import { createStore, type SetStoreFunction, type Store } from 'solid-js/store';

// 60 seconds
const EXPIRATION_THRESHOLD_SECONDS = 60;

type Token = string;
type TokenStore = Record<CompositeKey, Token>;
type CompositeKey = `${string}@${string}`;

const [sharedPermissionTokenStore, setSharedPermissionTokenStore] =
  createStore<TokenStore>({});

async function fetchNewToken(blockId: string) {
  const token =
    await storageServiceClient.permissionsTokens.createPermissionToken({
      document_id: blockId,
    });

  if (token.isErr()) {
    console.error('Failed to create permission token:', token.error);
    return;
  }

  return token.value.token;
}

/**
 * Whether a JWT expires within {@link EXPIRATION_THRESHOLD_SECONDS}. Treats an
 * unparseable token as expired.
 */
export function isTokenExpired(token: string) {
  try {
    const payload = JSON.parse(atob(token.split('.')[1]));
    const currentTime = Math.floor(Date.now() / 1000);
    return payload.exp < currentTime + EXPIRATION_THRESHOLD_SECONDS;
  } catch (_error) {
    return true;
  }
}

function makeCompositeKey(blockType: string, blockId: string): CompositeKey {
  return `${blockType}@${blockId}`;
}

/**
 * Fetches a permission token for the given block type and id.
 *
 * Tokens are cached in a store, and will automatically be refreshed if they are expired.
 *
 * By default the shared permission token store is used, but an optional store can be provided to use a different store.
 *
 * @param blockType The type of block to fetch the token for.
 * @param blockId The id of the block to fetch the token for.
 * @param store An optional store to use for storing the token. If not provided, the shared permission token store will be used.
 * @returns A callback that returns the token.
 */
export async function getPermissionToken(
  blockType: string,
  blockId: string,
  store?: [Store<TokenStore>, SetStoreFunction<TokenStore>]
) {
  const [tokenStore, setTokenStore] = store ?? [
    sharedPermissionTokenStore,
    setSharedPermissionTokenStore,
  ];
  const compositeKey = makeCompositeKey(blockType, blockId);

  const storedToken = tokenStore[compositeKey];

  if (!storedToken || isTokenExpired(storedToken)) {
    const token = await fetchNewToken(blockId);
    if (token) {
      setTokenStore(compositeKey, token);

      return token;
    } else {
      console.error('Failed to fetch permission token');
      return undefined;
    }
  }

  return storedToken;
}

function _useBlockPermissionToken() {
  const blockId = useBlockId();
  return createCallback(
    async () => await getPermissionToken('document', blockId)
  );
}