workspaces_registry_template/site/pages/index.js

100 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2022-12-14 15:03:50 +00:00
import { useState, useEffect } from 'react'
import Head from 'next/head'
2023-01-19 11:18:02 +00:00
import Workspace from '../components/Workspace'
2022-12-14 15:03:50 +00:00
import styles from '../styles/Home.module.css'
export default function Home({ searchText }) {
2023-01-19 11:18:02 +00:00
const [workspaces, setWorkspaces] = useState(null)
2023-08-21 13:22:30 +00:00
const [versions, setVersions] = useState(null)
const [version, setVersion] = useState(null)
2022-12-14 15:03:50 +00:00
useEffect(() => {
2023-08-21 13:22:30 +00:00
let currentVersion = localStorage.getItem("version") || null
2022-12-14 15:03:50 +00:00
fetch('list.json')
.then((res) => res.json())
2023-01-19 11:18:02 +00:00
.then((workspaces) => {
2023-08-21 13:22:30 +00:00
let wsversions = []
workspaces.workspaces.forEach((workspace) => {
if(workspace.compatibility) {
workspace.compatibility.forEach((v) => {
const value = parseFloat(v)
if(wsversions.indexOf(value) === -1) {
wsversions.push(value)
}
})
}
})
const sorted = wsversions.sort((a,b) => a-b).reverse()
setVersions(sorted)
if (currentVersion === null) {
currentVersion = sorted[0]
localStorage.setItem("version", currentVersion);
}
setVersion(currentVersion)
2023-01-19 11:18:02 +00:00
setWorkspaces(workspaces)
2022-12-14 15:03:50 +00:00
})
}, [])
2023-08-21 13:22:30 +00:00
const updateVersion = (version) => {
localStorage.setItem("version", version);
setVersion(version)
}
2023-01-19 11:18:02 +00:00
let filteredworkspaces = workspaces && workspaces.workspaces && workspaces.workspaces.length > 0 ? [...workspaces.workspaces] : [];
2023-08-21 13:22:30 +00:00
filteredworkspaces = filteredworkspaces.filter((v) => v.compatibility.some((el) => el === version + '.x'))
2022-12-14 15:03:50 +00:00
const lowerSearch = searchText && searchText.toLowerCase();
if (searchText && searchText !== "") {
2023-01-19 11:18:02 +00:00
filteredworkspaces = filteredworkspaces.filter((i) => {
const category = (i.categories && i.categories.length > 0) ? i.categories.filter((i) =>
2022-12-14 15:03:50 +00:00
i.toLowerCase().includes(lowerSearch)
) : [];
2022-12-14 15:03:50 +00:00
return (
i.name.toLowerCase().includes(lowerSearch) ||
category.length > 0
);
});
}
return (
<div className="">
<Head>
2023-01-19 11:18:02 +00:00
<title>Kasm Workspaces</title>
<meta name="description" content="List of workspaces for Kasm Webspaces" />
2022-12-14 15:03:50 +00:00
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="p-8 py-10 xl:px-20">
2023-08-21 13:22:30 +00:00
<h1 className='flex flex-wrap-reverse uppercase tracking-widest justify-center mb-10 gap-5'>
<span className='flex items-center text-lg bg-slate-100/90 rounded overflow-hidden shadow'>
<span className='flex px-3 text-xs opacity-100'>Workspaces</span>
<span className='text-white p-3 py-1 flex bg-[#2980b9]'>{workspaces && workspaces.workspacecount}</span>
</span>
<span className='flex items-center text-lg bg-slate-100/90 rounded overflow-hidden shadow'>
<span className='flex px-3 text-xs opacity-100'>Kasm Version</span>
<span className='text-white gap-3 p-3 py-1 flex items-center bg-[#2980b9]'>{versions && versions.map((v) => (
<div className={'cursor-pointer ' + (+v === +version ? 'text-white' : 'text-white/50 text-xs')} key={v} onClick={() => updateVersion(v)}>{v}</div>
))}</span>
</span>
</h1>
2022-12-14 15:03:50 +00:00
<div className="flex flex-wrap gap-1 justify-center">
2023-01-19 11:18:02 +00:00
{filteredworkspaces && filteredworkspaces.length > 0 && filteredworkspaces.map(function (workspace, i) {
return <Workspace key={workspace.sha} workspace={workspace} />
2022-12-14 15:03:50 +00:00
})}
2023-01-19 11:18:02 +00:00
{filteredworkspaces && filteredworkspaces.length === 0 && (
<p>No workspaces found {searchText !== '' && ('matching "' + searchText + '"')}</p>
2022-12-14 15:03:50 +00:00
)}
</div>
<div className={styles.grid}>
</div>
</main>
</div >
)
}