This section installs the functionality to store and retrieve data using Kepler.
A completed version of this part can be found in the example repository (02_storage).
By using Kepler via the storage module within SSX, you hereby agree to the Kepler Developer Terms found here.
This step will show you how to use the storage module feature. This feature will allow you to provide storage for your users, who can authorize access by using their keys.
You don't need to install any new dependencies, as SSX natively supports Kepler.
You can create the component file by yourself or by running the bash command below:
touchcomponents/KeplerStorageComponent.tsx
Then add the following to my-app/components/KeplerStorageComponent.tsx file:
"use client";import { SSX } from"@spruceid/ssx";import { useEffect, useState } from"react";interfaceIKeplerStorageComponent { ssx:SSX}constKeplerStorageComponent= ({ ssx }:IKeplerStorageComponent) => {const [key,setKey] =useState<string>('');const [value,setValue] =useState<string>('');const [contentList,setContentList] =useState<Array<string>>([]);const [viewingContent,setViewingContent] =useState<string|null>(null);const [loading,setLoading] =useState<boolean>(false);useEffect(() => {getContentList(); }, []);constgetContentList=async () => {setLoading(true);let { data } =awaitssx.storage.list(); data =data.filter((d:string) =>d.includes('/content/'))setContentList(data);setLoading(false); };consthandlePostContent=async (key:string, value:string) => {if (!key ||!value) {alert('Invalid key or value');return; }constformatedKey='content/'+key.replace(/\ /g,'_');setLoading(true);awaitssx.storage.put(formatedKey, value);setContentList((prevList) => [...prevList,`my-app/${formatedKey}`]);setKey('');setValue('');setLoading(false); };consthandleGetContent=async (content:string) => {setLoading(true);constcontentName=content.replace('my-app/','')const { data } =awaitssx.storage.get(contentName);setViewingContent(`${content}:\n${data}`);setLoading(false); };consthandleDeleteContent=async (content:string) => {setLoading(true);constcontentName=content.replace('my-app/','')awaitssx.storage.delete(contentName);setContentList((prevList) =>prevList.filter((c) => c !== content));setLoading(false); };return ( <divstyle={{ marginTop:50 }}> <h2>Storage Module</h2> <p>Store your data in Kepler Orbit</p> <pstyle={{ maxWidth:500, fontSize:12 }}> Kepler is a decentralized <b>key-value</b> storage system that uses DIDs and Authorization Capabilities to define Orbits,
where your data lives, and who has access. In this example we will store a value (string) indexed by a key (string).
</p> <inputtype="text"placeholder="Key"value={key}onChange={(e) =>setKey(e.target.value)}disabled={loading} /> <br /> <inputtype="text"placeholder="Value"value={value}onChange={(e) =>setValue(e.target.value)}disabled={loading} /> <br /> <buttononClick={() =>handlePostContent(key, value)}disabled={loading}style={{ marginTop:15 }} > <span> POST </span> </button> <p><b>My Kepler data</b></p> <table> <tbody> {contentList?.map((content, i) => <trkey={i}> <td> {content} </td> <td> <buttononClick={() =>handleGetContent(content)}disabled={loading} > <span> GET </span> </button> </td> <td> <buttononClick={() =>handleDeleteContent(content)}disabled={loading} > <span> DELETE </span> </button> </td> </tr>)} </tbody> </table> <prestyle={{ marginTop:25, marginBottom:0 }}> {viewingContent} </pre> </div> );}exportdefault KeplerStorageComponent;
Now update the SSXComponent to import the storage component module by adding the following into my-app/components/SSXComponent.tsx file: