Skip to content

Commit f190695

Browse files
update progressive file fetching prosess with localdb repo integration
1 parent bf82bba commit f190695

13 files changed

Lines changed: 530 additions & 503 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export function EditorLayerHierarchy() {
2222
: [];
2323

2424
const renderItem = useCallback(
25-
({ id, name, depth, type }) => {
25+
({ id, name, depth, type, origin }) => {
2626
const selected = state?.selectedNodes?.includes(id);
2727

2828
return (
2929
<LayerRow
3030
icon={
3131
<IconContainer>
32-
<LayerIcon type={type} selected={selected} />
32+
<LayerIcon type={origin} selected={selected} />
3333
</IconContainer>
3434
}
3535
name={name}

editor/components/home/cards/card-variant-file.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import { BaseHomeSceneCard } from "./base-home-scene-card";
44

55
export function FileCard({
66
label,
7-
thumbnail,
87
data,
98
}: {
109
label: string;
11-
thumbnail: string;
1210
data: {
13-
file: string;
11+
key: string;
12+
thumbnailUrl: string;
1413
};
1514
}) {
1615
const router = useRouter();
1716

1817
return (
1918
<BaseHomeSceneCard
2019
onClick={() => {
21-
router.push(`/files/[file]`, `/files/${data.file}`);
20+
router.push(`/files/[key]`, `/files/${data.key}`);
2221
}}
2322
label={label}
24-
thumbnail={thumbnail}
23+
thumbnail={data.thumbnailUrl}
2524
/>
2625
);
2726
}

editor/components/home/cards/recent-design-card.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ export function RecentDesignCard(props: {
2626
);
2727
}
2828

29-
const _defaultpreview =
30-
"https://s3.amazonaws.com/uifaces/faces/twitter/golovey/128.jpg";
31-
function _safe_previewurl(previewUrl: string): string {
32-
if (!previewUrl) {
33-
return _defaultpreview;
34-
}
35-
return previewUrl;
36-
}
37-
3829
function _str_lastUpdatedAt(lastUpdatedAt: Date) {
3930
if (!lastUpdatedAt) return;
4031
return moment(lastUpdatedAt).format("MM/dd/yyyy");

editor/hooks/use-design.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { convert } from "@design-sdk/figma-node-conversion";
1515
import { mapFigmaRemoteToFigma } from "@design-sdk/figma-remote/lib/mapper";
1616
import { useFigmaAccessToken } from ".";
1717
import { FileResponse } from "@design-sdk/figma-remote-types";
18+
import { FigmaDesignRepository } from "repository/figma-design-repository";
1819

1920
// globally configure auth credentials for interacting with `@design-sdk/figma-remote`
2021
configure_auth_credentials({
@@ -171,10 +172,8 @@ export function useDesignFile({ file }: { file: string }) {
171172
useEffect(() => {
172173
if (file && (fat.accessToken || fat.personalAccessToken)) {
173174
async function handle() {
174-
const iterator = fetch.fetchFile({
175-
file,
176-
auth: fat,
177-
});
175+
const repo = new FigmaDesignRepository(fat);
176+
const iterator = repo.fetchFile(file);
178177
let next: IteratorResult<FileResponse>;
179178
while ((next = await iterator.next()).done === false) {
180179
setDesignFile(next.value);

editor/pages/files/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
HomeSidebar,
88
} from "components/home";
99
import { WorkspaceRepository } from "repository";
10+
import { FileResponseRecord } from "store/fimga-file-store/figma-file-store";
1011

1112
export default function FilesPage() {
1213
const repository = new WorkspaceRepository();
13-
const [files, setFiles] = useState([]);
14+
const [files, setFiles] = useState<FileResponseRecord[]>([]);
1415
useEffect(() => {
1516
repository.getFiles().then(setFiles);
1617
}, []);
@@ -25,7 +26,7 @@ export default function FilesPage() {
2526
<HomeHeading>Files</HomeHeading>
2627
<HomeCardGroup
2728
cards={files.map((d) => (
28-
<Cards.File key={d.id} data={d} label={d.name} thumbnail={null} />
29+
<Cards.File key={d.key} data={d} label={d.name} />
2930
))}
3031
/>
3132
</div>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { fetch } from "@design-sdk/figma-remote";
2+
import { FileResponse } from "@design-sdk/figma-remote-types";
3+
import { FigmaFileStore } from "store/fimga-file-store/figma-file-store";
4+
5+
export class FigmaDesignRepository {
6+
constructor(
7+
readonly auth: { accessToken: string; personalAccessToken: string }
8+
) {}
9+
async *fetchFile(fileId: string) {
10+
const store = new FigmaFileStore(fileId);
11+
const existing = await store.get();
12+
if (existing) {
13+
yield existing;
14+
}
15+
16+
const _iter = fetch.fetchFile({ file: fileId, auth: this.auth });
17+
let next: IteratorResult<fetch.FetchFileGeneratorReturnType>;
18+
while ((next = await _iter.next()).done === false) {
19+
switch (next.value.__response_type) {
20+
case "pages":
21+
if (!existing) {
22+
yield next.value;
23+
}
24+
break;
25+
case "roots":
26+
if (!existing) {
27+
yield next.value;
28+
store.upsert(next.value);
29+
}
30+
break;
31+
case "whole":
32+
yield next.value;
33+
store.upsert(next.value);
34+
break;
35+
}
36+
}
37+
}
38+
}

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

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

editor/repository/workspace-repository/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { FigmaFilesStore } from "store/fimga-file-store/figma-file-store";
2+
13
export class WorkspaceRepository {
24
constructor() {}
35

@@ -13,6 +15,7 @@ export class WorkspaceRepository {
1315
}
1416

1517
async getFiles(): Promise<any[]> {
18+
return FigmaFilesStore.all();
1619
return [
1720
"JRD2EdsbBlWgib8NzTXIGo",
1821
"HSozKEVWhh8saZa2vr1Nxd",

editor/scaffolds/home-dashboard/home-dashboard.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ function FilesSection({ files }: { files }) {
7171
anchor={"#files"}
7272
label={"Files"}
7373
cards={files.map((file) => (
74-
<Cards.File
75-
key={file.id}
76-
data={file}
77-
label={file.name}
78-
thumbnail={file.thumbnail}
79-
/>
74+
<Cards.File key={file.id} data={file} label={file.name} />
8075
))}
8176
/>
8277
);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { openDB, IDBPDatabase } from "idb";
2+
import { FileResponse } from "@design-sdk/figma-remote-api";
3+
4+
// #region global db initialization
5+
const __db_pref = { name: "fimga-file-store", version: 1 };
6+
const __pk = "key";
7+
const __table = "files";
8+
export type FileResponseRecord = FileResponse & {
9+
[__pk]: string;
10+
};
11+
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+
});
23+
// #endregion
24+
25+
export class FigmaFilesStore {
26+
static of = (key: string): FigmaFileStore => {
27+
return new FigmaFileStore(key);
28+
};
29+
30+
static async all() {
31+
const files = await (await db).getAll(__table);
32+
return files.map((file) => file as FileResponseRecord);
33+
}
34+
35+
static async add(key: string, file: FileResponse) {
36+
return new FigmaFileStore(key).upsert(file);
37+
}
38+
}
39+
40+
export class FigmaFileStore {
41+
constructor(readonly filekey: string) {}
42+
43+
async upsert(file: FileResponse) {
44+
try {
45+
await (
46+
await db
47+
).put(__table, <FileResponseRecord>{ ...file, [__pk]: this.filekey });
48+
} catch (e) {
49+
if (process.env.NODE_ENV === "development") {
50+
console.error(e);
51+
}
52+
}
53+
}
54+
55+
async get() {
56+
const rec = await (await db).get(__table, this.filekey);
57+
return rec;
58+
}
59+
}

0 commit comments

Comments
 (0)