npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-save-session

v0.3.2

Published

React components to save a web session in local IndexedDB storage

Downloads

2

Readme

React-save-session

React components to manage session saving in browser's local storage using IndexedDB.

This package contains two components: <SaveSession/> to manage session saving and <ListSessions /> to display and retrieve saved sessions.

Install

npm i react-save-session

This repo is created using create-react-app. After downloading and install dependancies with npm install, run npm start to start an example on how to use this package.

Usage

import {SaveSession,ListSessions} from 'react-save-session'

A real example using this package can be found here:

https://frlender.github.io/comut-viz-app/

SaveSession

In its basic form, the <SaveSession /> component consits of a save button and an editable textarea displaying the session name. The interface of the <SaveSession /> component is:

interface SaveSessionProps<T>{
    dbName: string,
    getData: ()=>T
    sessName?: string,
    editTextWidth?: number
    editTextHeight?: number
    buttonClass?: string
    editTextStyle?: {},
    notification?: boolean
    notificationDelay?: number
    download?:(x:SessRecord<T>)=>void
    uid?:string,
    gc?: number
    setGc?: (x:React.SetStateAction<number>)=>void
    format?: ...
}

<T>: TypeScript interface for the saved session data. It is the users' responsibilty to make sure the session data are savable in IndexedDB or be able converted into a JSON string.

dbName: The database name of the underlying IndexedDB into which the session will be saved. It is normally the name of your website or any uniquely identifiable string.

getData: A function that returns the session data.

sessName: The default session name to be dispalyed in the editable textarea. Defaults to "Session".

editTextWidth: the width of the editable textarea.

editTextHeight: the height of the editable textarea.

buttonClass: the class names passed to the button(s) in the component.

editTextStyle: the styles applied to the editable textarea.

notification: whether to show a notification flag that will disappear after a short delay to notify that a session has been saved.

notificationDelay: defaults to 800ms.

download: If true, show also the download button. Clicking on the button will download the session data as a SessionRecord in a JSON file. If a function is passed, the function will be given a SessionRecord and handles how to save the SessionRecord into a file. The interface of SessionRecord is showed below. time is the local time when the session is saved.

interface SessRecord<T>{
    uid: string,
    time: Date,
    sessName: string,
    data: T,
}

uid: The UUID of a session. If uid is passed and a session with this uid was previously saved in the IndexedDB, the old record will be overwritten after clicking the save button. If uid is not passed, a new one is created for the session.

gc: A global counter. With the setGC function, it controls update of the <ListSessions /> component after a session is saved. gc should be a state variable created in a parent component common to <SaveSession /> and <ListSessions />. Usually, users do not need to set this variable as the two components are commonly placed in different pages (views) of an app. Navigating between pages (views) should automatically update <ListSessions />.

ListSessions

The interface of the <SaveSession /> component is

interface ListSessionsProps<T>{
    dbName: string,
    enter: (x:SessRecord<T>)=>void,
    gc?: number,
    format?: ...
}

dbName: The same database name as in <SaveSession />.

enter: A function to be executed after a session link is clicked and the session is retrived from the database. The function is given a SessionRecord as input and manages to reconstruct the session from the recored.

gc: A global counter. It should be a state variable the change of which updates the component.

CSS

The elements in the <SaveSession /> component have the following CSS class names set:

.rss-save-session: on the span element wrapping all other elements.

.rss-save-session-saved: on the span element serving as the notification flag.

The buttons and editable textarea can be styled using the buttonClass and editTextStyle properties of the component.


The elements in the <ListSessions /> component have the following CSS class names set:

.rss-list-sessions: on the div element wrapping all other elements.

.rss-list-sessions-item: on the div element representing a saved session in the list that displays names and saved time of saved sessions.

.rss-list-sessions-item-name-time: on the a element displaying the name and time of a saved session.

.rss-list-sessions-item-name: on the span element displaying the name of a saved session.

.rss-list-sessions-item-time: on the span element displaying the time of a saved session.

.rss-list-sessions-item-remove: on the a element that deletes a saved session..

Customization

React-save-session allows full customization of displayed JSX. Both components have a format property that accepts a function that accepts internal variables and functions as input and returns JSX. The interfaces are:

interface SaveSessionProps<T>{
    ...
    format?: (
            getRecord: ()=>SessRecord<T>,
            download:(x:SessRecord<T>)=>void,
            save:()=>void,
            notification: boolean,
            saved: boolean,
            sessName: string,
            setSessName:(x:React.SetStateAction<string>)=>void,
            buttonClass: string,
            editTextStyle: {}) => JSX.Element[],
}
interface SaveSessionProps<T>{
    ...
    format?: (sessions:SessRecord<T>[],
                remove:(x:string)=>void,
                enter:(x:SessRecord<T>)=>void
            ) => JSX.Element[],
}

The default JSX formatters can be found at src/SaveSession.tsx and used as examples.