FolioFolioDocs

SDK reference

An app's code reaches the shared drive through the frontend SDK, @planar/frontend-sdk. It only sees what the app's manifest lets it touch.

  • @planar/frontend-sdk/browser is the app window's connection: fs, application, launch, jobs and entities.
  • @planar/frontend-sdk/react has hooks over the same connection.

Show a file as it changes

An app watches a file's metadata: whether it's there and which version is current. It reads the content of the version that the metadata names.

Preview.tsx
import {useContent, useFile} from '@planar/frontend-sdk/react';

function Preview({path}) {
  const file = useFile(path);             // which version is current
  const content = useContent(file.value); // the bytes of that version
  if (file.error) return <p>{file.error.message}</p>;
  if (!file.loading && !file.value?.file) return <p>No file</p>;
  if (content.error) return <p>{content.error.message}</p>;
  if (!content.bytes) return <div className="skeleton" />;
  const blob = new Blob([content.bytes]);
  return <img src={URL.createObjectURL(blob)} alt="" />;
}

Once the window is connected, useFile has its value on the first render. While a new version loads, content.bytes keeps the previous version's bytes, so the picture stays on screen.

Save a file

A save always adds a new version and makes it current. Another app saving the same path at the same time never makes it fail.

save.ts
import {fs} from '@planar/frontend-sdk/browser';

// basedOn: the version the editor loaded. It's recorded, never checked.
const {fileId, fileRevisionId} = await fs.writeFile(
  '/images/image1.png', png, 'image/png', {basedOn: loadedRevision},
);

Without basedOn, the SDK records the version it last read for that path, or none for a new file.

Files: fs

Call Does
stat, readdir, readFile, readFileWithRevision, openRead Read a path, optionally as it was at an earlier commit
writeFile(path, bytes, mediaType, {basedOn}) Save a new version and make it current. Returns {fileId, fileRevisionId}
mkdir, rename, remove Change folders and names
watchDirectory, watchFile, watchHistory Follow a folder, a file, or a file's versions. Each returns {value, ready, stop}
readContent(version) Read the bytes of the version a watch or a listing names
pathHistory, readRevision(path, fileRevisionId) Read a file's versions, or one earlier version. Reading changes nothing
restoreVersion(path, fileRevisionId) Make an earlier version current

Versions

A file's versions come from useFileHistory. Restoring one makes it current again; its bytes don't change.

Versions.tsx
import {fs} from '@planar/frontend-sdk/browser';
import {useFileHistory} from '@planar/frontend-sdk/react';

function Versions({path}) {
  const {value} = useFileHistory(path);
  return value?.revisions.map(version => (
    <button
      key={version.fileRevisionId}
      onClick={() => fs.restoreVersion(path, version.fileRevisionId)}
    >
      {version.displayVersion}
      {version.isCurrent ? ' · current' : ''}
    </button>
  ));
}

Each version has its fileRevisionId, createdAt, displayVersion, isCurrent, what it was based on, and the job that made it. To read an earlier version without changing anything:

TypeScript
const old = await fs.readRevision(path, fileRevisionId);
// {bytes, fileId, fileRevisionId}

Hooks

Hook Returns
useDirectory(path), useFile(path), useFileHistory(path) {loading, value, error}
useContent(version) {loading, error, bytes, fileRevisionId}
useJob(jobId) {loading, job, error}
useConnection() the window's connection state

Run a job

An app starts a job that its manifest declares, then follows it by its jobId.

Resize.tsx
import {jobs} from '@planar/frontend-sdk/browser';
import {useJob} from '@planar/frontend-sdk/react';

const {jobId} = await jobs.submit(
  'sketch.resize',               // the operation, from the manifest
  {thumbnailPath},               // its parameters
  {image: '/images/image1.png'}, // its inputs
);

function JobStatus({jobId}) {
  const {loading, job, error} = useJob(jobId);
  if (error) return <span>{error.message}</span>;
  return <span>{loading ? '…' : job.state}</span>;
}

Code without React follows a job the same way:

TypeScript
const watch = jobs.watch(
  jobId,
  job => render(job.state),
  error => showError(error.message),
);
// It ends by itself at SUCCEEDED, FAILED or CANCELLED.
// watch.stop() ends it early.

A job's state is QUEUED, RUNNING, DISPATCH_UNCERTAIN, SUCCEEDED, FAILED or CANCELLED. A change shows within about two seconds, and the app runs no timers for it.

Folio fixes each input to the version current when it accepts the job. Keep the jobId (an app may store it), and read the job's state through it: never store the state. See Tasks and jobs.

Jobs: jobs

Call Does
submit(operationId, parameters, inputs, {idempotencyKey}) Start a job the manifest declares. Returns the job, with its jobId
get, list, cancel Read or cancel this app's jobs
watch(jobId, onJob, onError), watchList Follow a job until it is done, failed or cancelled. Each returns {stop, stopped}

The window: application and launch

window.ts
import {application, launch} from '@planar/frontend-sdk/browser';

application.setTitle('image1.png');
application.showFile('/images/image1.png');
application.showJob(jobId);
application.openWith('/images/image1.png'); // the person picks an app
application.close();

launch.failed('This app needs WebGL.'); // the window shows Retry
  • application.ready resolves to the installed manifest; application.context is what the app was opened on.
  • application.setTitle, showFile, showJob, openWith and close act on the app's window in the File Browser.
  • launch.connection is connecting, connected or failed; launch.failed(message) reports a start that failed.

Errors

A refused call rejects with an Error whose code says why.

TypeScript
try {
  await fs.writeFile(path, bytes, 'image/png');
} catch (error) {
  if (error.code !== 'forbidden') throw error;
  showError('This app can’t change that file.');
}
Code When
forbidden The path is outside what the manifest lets the app touch
invalidPath The path isn't a valid path
storageUnavailable Storage didn't answer, after Folio's retries
watchUnavailable A watch on the app's own folder can't be followed
jobNotFound This session can't see the job
sessionExpired The app's session ended
internalError Folio failed on its side

A request gives up after 30 seconds.