Skip to content

Commit 69ceaaf

Browse files
update file data store strategy
1 parent 2d8bca9 commit 69ceaaf

5 files changed

Lines changed: 77 additions & 21 deletions

File tree

editor/components/editor/editor-layer-hierarchy/editor-layer-hierarchy-tree.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export function EditorLayerHierarchy() {
1717
? state.design.pages.find((p) => p.id == state.selectedPage).children
1818
: [state.design?.input?.entry];
1919

20-
const layers: FlattenedNode[][] = root
21-
? root.filter((l) => !!l).map((layer) => flatten(layer))
22-
: [];
20+
const layers: FlattenedNode[][] = useMemo(() => {
21+
return root ? root.filter((l) => !!l).map((layer) => flatten(layer)) : [];
22+
}, [root]);
2323

2424
const renderItem = useCallback(
2525
({ id, name, depth, type, origin }) => {
2626
const selected = state?.selectedNodes?.includes(id);
27+
// const _haschildren = useMemo(() => haschildren(id), [id, depth]);
28+
// const _haschildren = haschildren(id);
2729

2830
return (
2931
<LayerRow
@@ -35,7 +37,7 @@ export function EditorLayerHierarchy() {
3537
name={name}
3638
depth={depth}
3739
id={id}
38-
expanded={haschildren(id) == true ? true : undefined}
40+
// expanded={_haschildren == true ? true : undefined}
3941
key={id}
4042
selected={selected}
4143
onAddClick={() => {}}
@@ -49,7 +51,7 @@ export function EditorLayerHierarchy() {
4951
/>
5052
);
5153
},
52-
[state?.selectedNodes]
54+
[dispatch, state?.selectedNodes, layers]
5355
);
5456

5557
const haschildren = useCallback(

editor/layouts/default-editor-workspace-layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const PanelLeftSideWrap = styled.div`
5757
min-height: 100%;
5858
max-height: 100%;
5959
max-width: 400px;
60-
overflow: auto;
6160
`;
6261

6362
const PanelRightSideWrap = styled.div`

editor/repository/figma-design-repository/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class FigmaDesignRepository {
2020
case "pages":
2121
if (!existing) {
2222
yield next.value;
23+
store.upsert(next.value);
2324
}
2425
break;
2526
case "roots":

editor/store/fimga-file-store/figma-file-store.ts

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ export type FileResponseRecord = FileResponse & {
99
[__pk]: string;
1010
};
1111

12-
const db: Promise<IDBPDatabase<FileResponseRecord>> = new Promise((resolve) => {
13-
openDB<FileResponseRecord>(__db_pref.name, __db_pref.version, {
14-
upgrade(db) {
15-
db.createObjectStore(__table, {
16-
keyPath: __pk,
17-
});
18-
},
19-
}).then((_db) => {
20-
resolve(_db);
21-
});
22-
});
12+
const db: Promise<IDBPDatabase<StorableFileResponse>> = new Promise(
13+
(resolve) => {
14+
openDB<StorableFileResponse>(__db_pref.name, __db_pref.version, {
15+
upgrade(db) {
16+
db.createObjectStore(__table, {
17+
keyPath: __pk,
18+
});
19+
},
20+
}).then((_db) => {
21+
resolve(_db);
22+
});
23+
}
24+
);
2325
// #endregion
2426

2527
export class FigmaFilesStore {
@@ -44,16 +46,68 @@ export class FigmaFileStore {
4446
try {
4547
await (
4648
await db
47-
).put(__table, <FileResponseRecord>{ ...file, [__pk]: this.filekey });
49+
).put(__table, <StorableFileResponse>{
50+
...minimizeFileResponse(file),
51+
[__pk]: this.filekey,
52+
});
4853
} catch (e) {
4954
if (process.env.NODE_ENV === "development") {
5055
console.error(e);
56+
throw e;
5157
}
5258
}
5359
}
5460

55-
async get() {
61+
async get(options?: { nounwrap?: boolean }): Promise<FileResponseRecord> {
5662
const rec = await (await db).get(__table, this.filekey);
57-
return rec;
63+
if (options?.nounwrap) {
64+
return rec;
65+
}
66+
return unwrapStorableFileResponse(rec);
5867
}
5968
}
69+
70+
type StorableFileResponse = {
71+
readonly components: JSONString;
72+
readonly styles: JSONString;
73+
readonly document: JSONString;
74+
readonly lastModified: string;
75+
readonly name: string;
76+
readonly role: string;
77+
readonly schemaVersion: number;
78+
readonly thumbnailUrl: string;
79+
readonly version: string;
80+
};
81+
82+
type JSONString = string;
83+
function minimizeFileResponse(file: FileResponse): StorableFileResponse {
84+
return {
85+
components: JSON.stringify(file.components),
86+
styles: JSON.stringify(file.styles),
87+
document: JSON.stringify(file.document),
88+
lastModified: file.lastModified,
89+
name: file.name,
90+
role: file.role,
91+
schemaVersion: file.schemaVersion,
92+
thumbnailUrl: file.thumbnailUrl,
93+
version: file.version,
94+
};
95+
}
96+
97+
function unwrapStorableFileResponse(
98+
stored: StorableFileResponse | undefined | null
99+
): FileResponseRecord {
100+
if (!stored) return;
101+
return {
102+
key: stored[__pk],
103+
components: JSON.parse(stored.components),
104+
styles: JSON.parse(stored.styles),
105+
document: JSON.parse(stored.document),
106+
lastModified: stored.lastModified,
107+
name: stored.name,
108+
role: stored.role as any,
109+
schemaVersion: stored.schemaVersion,
110+
thumbnailUrl: stored.thumbnailUrl,
111+
version: stored.version,
112+
};
113+
}

externals/design-sdk

0 commit comments

Comments
 (0)