Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port note deletion over from understory garden #61

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/ConceptPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import WebMonetization from './WebMonetization';
export default function ConceptPage({ editorId = 'concept-page', webId, workspaceSlug, slug }) {
const myWebId = useWebId()
const conceptName = slug && urlSafeIdToConceptName(slug);
const { concept, saveConcept, note, noteError, maybeSaveNoteBody, saving, privacy} = useConceptAndNote(webId, workspaceSlug, conceptName)
const { concept, saveConcept, note, noteError, maybeSaveNoteBody, deleteConcept, saving, privacy } = useConceptAndNote(webId, workspaceSlug, conceptName)
const { profile: authorProfile } = useProfile(webId);
const { profile: currentUserProfile } = useMyProfile();
const { index } = useCombinedConceptIndexDataset(webId, workspaceSlug);
Expand All @@ -20,7 +20,7 @@ export default function ConceptPage({ editorId = 'concept-page', webId, workspac
<div>
<WebMonetization webId={webId} />
<NoteHeader concept={concept} saveConcept={saveConcept} conceptName={conceptName} authorProfile={authorProfile}
currentUserProfile={currentUserProfile} myNote={myNote} privacy={privacy}/>
currentUserProfile={currentUserProfile} myNote={myNote} privacy={privacy} deleteConcept={deleteConcept} />
<ConceptEditor webId={webId} authorWebId={webId} workspaceSlug={workspaceSlug} slug={slug} myNote={myNote}
conceptIndex={index} concept={concept} conceptName={conceptName} editorId={conceptName}
note={note} noteError={noteError} maybeSaveNoteBody={maybeSaveNoteBody} />
Expand Down
17 changes: 14 additions & 3 deletions components/NoteHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from "react"
import { FOAF, DCTERMS } from "@inrupt/vocab-common-rdf";
import { getStringNoLocale, getDatetime, getUrl, setUrl, asUrl } from "@inrupt/solid-client";
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useWebId } from 'swrlit'

import { Logo } from './logo';
Expand All @@ -10,13 +11,13 @@ import { getRelativeTime } from '../utils/time';
import { profilePath } from '../utils/uris';
import { NoteVisibilityToggle } from './toggles'
import PrivacyChanger from './PrivacyChanger'
import { UploadImage } from './icons'
import { UploadImage, Trashcan } from './icons'
import { Tooltip } from './elements'
import ImageUploadModal from './modals/ImageUpload'
import { useImageUploadUri } from "../hooks/uris";

export default function NoteHeader({ concept, saveConcept, conceptName, authorProfile, currentUserProfile, myNote, privacy }) {

export default function NoteHeader({ concept, saveConcept, deleteConcept, conceptName, authorProfile, currentUserProfile, myNote, privacy }) {
const router = useRouter()
const authorName = authorProfile && getStringNoLocale(authorProfile, FOAF.name);
const avatarImgSrc = authorProfile && getUrl(authorProfile, FOAF.img)

Expand All @@ -42,6 +43,13 @@ export default function NoteHeader({ concept, saveConcept, conceptName, authorPr
setCoverImageUploaderOpen(false)
}
const authorProfilePath = authorWebId && profilePath(authorWebId)
async function deleteAndRedirect() {
const confirmed = confirm(`Are you sure you want to delete ${conceptName}?`)
if (confirmed) {
await deleteConcept()
router.push("/")
}
}
return (
<div className="flex flex-col">
<nav className={`${bg} b-2xl flex flex-row justify-between h-32`}>
Expand Down Expand Up @@ -85,6 +93,9 @@ export default function NoteHeader({ concept, saveConcept, conceptName, authorPr
<div className="flex flex-row h-10 mr-4">
{myNote && (
<>
<button onClick={deleteAndRedirect} className="mx-4">
<Trashcan className="h-6 w-6 text-white" />
</button>
{privacyUpdatingTo ? (
<PrivacyChanger name={conceptName}
changeTo={privacyUpdatingTo} onFinished={() => setPrivacyUpdatingTo(null)} />
Expand Down
6 changes: 6 additions & 0 deletions components/icons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ export const UploadImage = (props) => (
<path d="M57 2H1c-.552 0-1 .447-1 1v44c0 .553.448 1 1 1h27V29h2.586l-6.612-6.611L7.661 36.751c-.19.167-.426.249-.661.249-.277 0-.553-.114-.751-.339-.365-.415-.325-1.047.09-1.412l17.017-14.982c.396-.348.994-.329 1.368.044l4.743 4.743 9.794-10.727c.179-.196.429-.313.694-.325.264-.006.524.083.72.262l12 11c.407.373.435 1.006.062 1.413-.373.406-1.006.435-1.413.062L40.063 16.414l-9.181 10.054L33.414 29H58V3c0-.553-.448-1-1-1zM16 19.138c-3.071 0-5.569-2.498-5.569-5.568C10.431 10.498 12.929 8 16 8s5.569 2.498 5.569 5.569c0 3.071-2.498 5.569-5.569 5.569z" />
</svg>
)

export const Trashcan = (props) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you find these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://heroicons.com/ - though I'm thinking about moving to fontawesome - they have SVG icons these days and a huge catalog https://fontawesome.com/v5.15/how-to-use/on-the-web/using-with/react

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" {...props}>
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
)
58 changes: 34 additions & 24 deletions hooks/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import equal from "fast-deep-equal/es6";
import { useThing, useWebId } from 'swrlit'
import {
createSolidDataset, createThing, getUrl, setUrl,
setThing, getThing, getBoolean
setThing, getThing, getBoolean, removeThing
} from '@inrupt/solid-client'

import { useUnderstoryContainerUri, useStorageContainer } from './uris';
Expand All @@ -15,27 +15,28 @@ import { useWorkspaceContext } from '../contexts/WorkspaceContext';
import { useConcept } from '../hooks/concepts';
import { getAndParseNoteBody, createOrUpdateSlateJSON } from '../model/note';
import { createOrUpdateConceptIndex } from '../model/concept';
import { deleteResource } from '../utils/fetch'


const appThingName = "app"

const prefsPath = "workspace/default/prefs.ttl"
const prefsWorkspaceName = "workspace"

function createNewAppResource(appContainerUri, privateAppContainerUri){
let app = createThing({name: appThingName})
function createNewAppResource(appContainerUri, privateAppContainerUri) {
let app = createThing({ name: appThingName })
let defaultWorkspace = createThing()
defaultWorkspace = setUrl(defaultWorkspace, US.publicPrefs, `${appContainerUri}${prefsPath}#${prefsWorkspaceName}`)
defaultWorkspace = setUrl(defaultWorkspace, US.privatePrefs, `${privateAppContainerUri}${prefsPath}#${prefsWorkspaceName}`)
app = setUrl(app, US.hasWorkspace, defaultWorkspace)
let resource = createSolidDataset()
resource = setThing(resource, defaultWorkspace)
resource = setThing(resource, app)
return {app, resource}
return { app, resource }
}

function createWorkspacePrefs(conceptPrefix, tagPrefix, workspacePreferencesFileUri){
let workspace = createThing({name: prefsWorkspaceName})
function createWorkspacePrefs(conceptPrefix, tagPrefix, workspacePreferencesFileUri) {
let workspace = createThing({ name: prefsWorkspaceName })
workspace = setUrl(workspace, US.conceptPrefix, conceptPrefix)
workspace = setUrl(workspace, US.tagPrefix, tagPrefix)
workspace = setUrl(workspace, US.conceptIndex, new URL("concepts.ttl", workspacePreferencesFileUri).toString())
Expand All @@ -44,20 +45,20 @@ function createWorkspacePrefs(conceptPrefix, tagPrefix, workspacePreferencesFile
return workspace
}

export function useApp(webId){
export function useApp(webId) {
const appContainerUri = useUnderstoryContainerUri(webId)

const privateAppContainerUri = useUnderstoryContainerUri(webId, 'private')
const publicWorkspacePrefsUri = appContainerUri && `${appContainerUri}${prefsPath}#${prefsWorkspaceName}`
const {save: savePublicPrefs} = useThing(publicWorkspacePrefsUri)
const { save: savePublicPrefs } = useThing(publicWorkspacePrefsUri)
const privateWorkspacePrefsUri = privateAppContainerUri && `${privateAppContainerUri}${prefsPath}#${prefsWorkspaceName}`
const {save: savePrivatePrefs} = useThing(privateWorkspacePrefsUri)
const { save: savePrivatePrefs } = useThing(privateWorkspacePrefsUri)
const appUri = appContainerUri && `${appContainerUri}app.ttl#${appThingName}`
const {thing: app, saveResource: saveAppResource, ...rest} = useThing(appUri)
const { thing: app, saveResource: saveAppResource, ...rest } = useThing(appUri)
const conceptPrefix = useConceptPrefix(webId, 'default')
const tagPrefix = useTagPrefix(webId, 'default')

async function initApp(){
async function initApp() {
const { resource: appResource } = createNewAppResource(appContainerUri, privateAppContainerUri)
await saveAppResource(appResource)
const privatePrefs = createWorkspacePrefs(conceptPrefix, tagPrefix, privateWorkspacePrefsUri)
Expand All @@ -67,10 +68,10 @@ export function useApp(webId){
console.log("initialized!")
}

return {app, initApp, saveResource: saveAppResource, ...rest}
return { app, initApp, saveResource: saveAppResource, ...rest }
}

export function useWorkspacePreferencesFileUris(webId, workspaceSlug='default'){
export function useWorkspacePreferencesFileUris(webId, workspaceSlug = 'default') {
const { app } = useApp(webId)
// we're ignoring the workspaceSlug parameter for now, but eventually we'll want to use this to get the currect workspace
const { thing: workspaceInfo } = useThing(app && getUrl(app, US.hasWorkspace))
Expand All @@ -80,17 +81,17 @@ export function useWorkspacePreferencesFileUris(webId, workspaceSlug='default'){
}
}

function useConceptPrefix(webId, workspaceSlug){
function useConceptPrefix(webId, workspaceSlug) {
const storageContainerUri = useStorageContainer(webId)
return storageContainerUri && `${storageContainerUri}${appPrefix}/${workspaceSlug}/concepts#`
}

function useTagPrefix(webId, workspaceSlug){
function useTagPrefix(webId, workspaceSlug) {
const storageContainerUri = useStorageContainer(webId)
return storageContainerUri && `${storageContainerUri}${appPrefix}/${workspaceSlug}/tags#`
}

export function useWorkspace(webId, slug, storage='public'){
export function useWorkspace(webId, slug, storage = 'public') {
const workspacePreferencesFileUris = useWorkspacePreferencesFileUris(webId, slug)
const workspacePreferencesFileUri = workspacePreferencesFileUris && workspacePreferencesFileUris[storage]
const { thing: workspace, ...rest } = useThing(workspacePreferencesFileUri)
Expand All @@ -99,17 +100,17 @@ export function useWorkspace(webId, slug, storage='public'){

}

export function useCurrentWorkspace(storage='public'){
export function useCurrentWorkspace(storage = 'public') {
const webId = useWebId()
const { slug: workspaceSlug } = useWorkspaceContext()
return useWorkspace(webId, workspaceSlug, storage)
}

function createSettings(){
return createThing({name: "settings"})
function createSettings() {
return createThing({ name: "settings" })
}

export function useAppSettings(webId){
export function useAppSettings(webId) {
const { app, resource: appResource, saveResource: saveAppResource } = useApp(webId)
const settingsUri = app && getUrl(app, US.hasSettings)
const settings = app && (settingsUri ? getThing(appResource, settingsUri) : createSettings())
Expand All @@ -118,10 +119,10 @@ export function useAppSettings(webId){
newAppResource = setThing(newAppResource, setUrl(app, US.hasSettings, newSettings))
return saveAppResource(newAppResource)
}
return { settings, save }
return { settings, save }
}

export function useDevMode(webId){
export function useDevMode(webId) {
const { settings } = useAppSettings(webId)
return settings && getBoolean(settings, US.devMode)
}
Expand Down Expand Up @@ -167,9 +168,18 @@ export function useConceptAndNote(webId, workspaceSlug, conceptName) {
}
}

async function saveConcept(newConcept){
async function saveConcept(newConcept) {
return await saveConceptIndex(setThing(conceptIndex, newConcept))
}

return { note, noteError, maybeSaveNoteBody, concept, saveConcept, saving, privacy }
async function deleteConcept() {
await Promise.all([
deleteResource(noteStorageUri),
concept && saveConceptIndex(removeThing(conceptIndex, concept))
])
// mutate to invalidate the cache for the note
mutateNote()
}

return { note, noteError, maybeSaveNoteBody, concept, saveConcept, deleteConcept, saving, privacy }
}
3 changes: 1 addition & 2 deletions model/flags.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { collapseTextChangeRangesAcrossMultipleVersions } from "typescript"

const VercelEnv = process.env.VERCEL_ENV || ''
const VercelEnv = process.env.NEXT_PUBLIC_VERCEL_ENV || '';

export function isPreviewEnv() {
console.log(VercelEnv);
return VercelEnv !== 'production';
}