Skip to content

Commit 92d8566

Browse files
committed
Open new folders without dialog
1 parent 4bcbd77 commit 92d8566

File tree

5 files changed

+72
-138
lines changed

5 files changed

+72
-138
lines changed

data-browser/src/components/NewInstanceButton/NewFolderButton.tsx

Lines changed: 0 additions & 88 deletions
This file was deleted.

data-browser/src/components/NewInstanceButton/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import { NewBookmarkButton } from './NewBookmarkButton';
44
import { NewInstanceButtonProps } from './NewInstanceButtonProps';
55
import { NewInstanceButtonDefault } from './NewInstanceButtonDefault';
66
import { useSettings } from '../../helpers/AppSettings';
7-
import { NewFolderButton } from './NewFolderButton';
87

98
type InstanceButton = (props: NewInstanceButtonProps) => JSX.Element;
109

1110
/** If your New Instance button requires custom logic, such as a custom dialog */
1211
const classMap = new Map<string, InstanceButton>([
1312
[classes.bookmark, NewBookmarkButton],
14-
[classes.folder, NewFolderButton],
1513
]);
1614

1715
/** A button for creating a new instance of some thing */

data-browser/src/components/NewInstanceButton/useCreateAndNavigate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useNavigate } from 'react-router-dom';
1212
import { constructOpenURL } from '../../helpers/navigation';
1313

1414
/**
15-
* Hook that builds a function that will create a new resoure with the given
15+
* Hook that builds a function that will create a new resource with the given
1616
* properties and then navigate to it.
1717
*
1818
* @param klass The type of resource to create a new instance of.

data-browser/src/components/NewInstanceButton/useDefaultNewInstanceHandler.tsx

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import { useSettings } from '../../helpers/AppSettings';
1111
import { newURL } from '../../helpers/navigation';
1212
import { useCreateAndNavigate } from './useCreateAndNavigate';
1313

14+
/**
15+
* Returns a function that can be used to create a new instance of the given Class.
16+
* This is the place where you can add custom behavior for certain classes.
17+
* By default, we're redirected to an empty Form for the new instance.
18+
* For some Classes, though, we'd rather have some values are pre-filled (e.g. a new ChatRoom with a `new chatroom` title).
19+
* For others, we want to render a custom form, perhaps with a different layout.
20+
*/
1421
export function useDefaultNewInstanceHandler(klass: string, parent?: string) {
1522
const store = useStore();
1623
const { setDrive } = useSettings();
@@ -22,61 +29,77 @@ export function useDefaultNewInstanceHandler(klass: string, parent?: string) {
2229
const createResourceAndNavigate = useCreateAndNavigate(klass, parent);
2330

2431
const onClick = useCallback(async () => {
25-
switch (klass) {
26-
case classes.chatRoom: {
27-
createResourceAndNavigate('chatRoom', {
28-
[properties.name]: 'New ChatRoom',
29-
[properties.isA]: [classes.chatRoom],
30-
});
31-
break;
32-
}
33-
34-
case classes.document: {
35-
createResourceAndNavigate('documents', {
36-
[properties.isA]: [classes.document],
37-
[properties.name]: 'Untitled Document',
38-
});
39-
break;
40-
}
32+
try {
33+
switch (klass) {
34+
case classes.chatRoom: {
35+
createResourceAndNavigate('chatRoom', {
36+
[properties.name]: 'Untitled ChatRoom',
37+
[properties.isA]: [classes.chatRoom],
38+
});
39+
break;
40+
}
4141

42-
case classes.importer: {
43-
createResourceAndNavigate('importer', {
44-
[properties.isA]: [classes.importer],
45-
});
46-
break;
47-
}
42+
case classes.document: {
43+
createResourceAndNavigate('document', {
44+
[properties.isA]: [classes.document],
45+
[properties.name]: 'Untitled Document',
46+
});
47+
break;
48+
}
4849

49-
case classes.drive: {
50-
const agent = store.getAgent();
50+
case classes.folder: {
51+
createResourceAndNavigate('folder', {
52+
[properties.isA]: [classes.folder],
53+
[properties.name]: 'Untitled Folder',
54+
[properties.displayStyle]: classes.displayStyles.list,
55+
});
56+
break;
57+
}
5158

52-
if (!agent || agent.subject === undefined) {
53-
throw new Error(
54-
'No agent set in the Store, required when creating a Drive',
55-
);
59+
case classes.importer: {
60+
createResourceAndNavigate('importer', {
61+
[properties.isA]: [classes.importer],
62+
});
63+
break;
5664
}
5765

58-
const newResource = await createResourceAndNavigate(
59-
'drive',
60-
{
61-
[properties.isA]: [classes.drive],
62-
[properties.write]: [agent.subject],
63-
[properties.read]: [agent.subject],
64-
},
65-
undefined,
66-
true,
67-
);
66+
case classes.drive: {
67+
const agent = store.getAgent();
6868

69-
const agentResource = await store.getResourceAsync(agent.subject);
70-
agentResource.pushPropVal(properties.drives, newResource.getSubject());
71-
agentResource.save(store);
72-
setDrive(newResource.getSubject());
73-
break;
74-
}
69+
if (!agent || agent.subject === undefined) {
70+
throw new Error(
71+
'No agent set in the Store, required when creating a Drive',
72+
);
73+
}
74+
75+
const newResource = await createResourceAndNavigate(
76+
'drive',
77+
{
78+
[properties.isA]: [classes.drive],
79+
[properties.write]: [agent.subject],
80+
[properties.read]: [agent.subject],
81+
},
82+
undefined,
83+
true,
84+
);
7585

76-
default: {
77-
// Opens an `Edit` form with the class and a decent subject name
78-
navigate(newURL(klass, parent, store.createSubject(shortname)));
86+
const agentResource = await store.getResourceAsync(agent.subject);
87+
agentResource.pushPropVal(
88+
properties.drives,
89+
newResource.getSubject(),
90+
);
91+
agentResource.save(store);
92+
setDrive(newResource.getSubject());
93+
break;
94+
}
95+
96+
default: {
97+
// Opens an `Edit` form with the class and a decent subject name
98+
navigate(newURL(klass, parent, store.createSubject(shortname)));
99+
}
79100
}
101+
} catch (e) {
102+
store.handleError(e);
80103
}
81104
}, [klass, store, parent, createResourceAndNavigate]);
82105

lib/src/urls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export const datatypes = {
134134

135135
export const instances = {
136136
publicAgent: 'https://atomicdata.dev/agents/publicAgent',
137+
displayStyleGrid: 'https://atomicdata.dev/agents/publicAgent',
137138
};
138139

139140
export const urls = {

0 commit comments

Comments
 (0)