Skip to content

Commit 13b362d

Browse files
add noauth handling
1 parent 2dd4242 commit 13b362d

3 files changed

Lines changed: 78 additions & 32 deletions

File tree

editor/hooks/use-design.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ export function useDesign({
174174

175175
type TUseDesignFile =
176176
| TFetchFileForApp
177-
| { __type: "error"; reason: "no-file" | "no-auth" | "unauthorized" }
177+
| {
178+
__type: "error";
179+
reason: "no-auth" | "unauthorized";
180+
cached?: TFetchFileForApp;
181+
}
182+
| { __type: "error"; reason: "no-file" }
178183
| { __type: "loading" };
179184

180185
export function useDesignFile({ file }: { file: string }) {
@@ -203,10 +208,21 @@ export function useDesignFile({ file }: { file: string }) {
203208
__type: "loading",
204209
});
205210
} else {
206-
setDesignFile({
207-
__type: "error",
208-
reason: "no-auth",
209-
});
211+
// if no auth provided, try to used cached file if possible.
212+
FigmaDesignRepository.fetchCachedFile(file)
213+
.then((r) => {
214+
setDesignFile({
215+
__type: "error",
216+
reason: "no-auth",
217+
cached: r,
218+
});
219+
})
220+
.catch((e) => {
221+
setDesignFile({
222+
__type: "error",
223+
reason: "no-auth",
224+
});
225+
});
210226
}
211227
}
212228
} else {

editor/pages/files/[key]/index.tsx

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { SigninToContinueBannerPrmoptProvider } from "components/prompt-banner-s
44
import { Editor, EditorDefaultProviders } from "scaffolds/editor";
55
import { EditorSnapshot, StateProvider } from "core/states";
66
import { WorkspaceAction } from "core/actions";
7-
import { useDesign, useDesignFile } from "hooks";
7+
import { useDesignFile } from "hooks";
88

99
import { warmup } from "scaffolds/editor";
10+
import { FileResponse } from "@design-sdk/figma-remote-types";
1011

1112
export default function FileEntryEditor() {
1213
const router = useRouter();
@@ -30,32 +31,7 @@ export default function FileEntryEditor() {
3031
const prevstate =
3132
initialState.type == "success" && initialState.value.history.present;
3233

33-
useEffect(() => {
34-
if (file.__type === "loading") {
35-
return;
36-
}
37-
38-
if (file.__type === "error") {
39-
// handle error by reason
40-
switch (file.reason) {
41-
case "unauthorized":
42-
case "no-auth": {
43-
router.push("/preferences/access-tokens");
44-
break;
45-
}
46-
case "no-file": {
47-
// ignore. might still be fetching file from query param.
48-
break;
49-
}
50-
}
51-
return;
52-
}
53-
54-
if (!file.__initial) {
55-
// when full file is loaded, allow editor with user interaction.
56-
setLoading(false);
57-
}
58-
34+
const initWith = (file: FileResponse) => {
5935
let val: EditorSnapshot;
6036

6137
// TODO: seed this as well
@@ -96,6 +72,49 @@ export default function FileEntryEditor() {
9672
type: "set",
9773
value: val,
9874
});
75+
};
76+
77+
useEffect(() => {
78+
if (!loading) {
79+
return;
80+
}
81+
82+
if (file.__type === "loading") {
83+
return;
84+
}
85+
86+
if (file.__type === "error") {
87+
// handle error by reason
88+
switch (file.reason) {
89+
case "unauthorized":
90+
case "no-auth": {
91+
if (file.cached) {
92+
initWith(file.cached);
93+
setLoading(false);
94+
alert(
95+
"You will now see the cached version of this file. To view the latest version, setup your personall access token."
96+
);
97+
// TODO: show signin prompt
98+
window.open("/preferences/access-tokens", "_blank");
99+
} else {
100+
router.push("/preferences/access-tokens");
101+
}
102+
break;
103+
}
104+
case "no-file": {
105+
// ignore. might still be fetching file from query param.
106+
break;
107+
}
108+
}
109+
return;
110+
}
111+
112+
if (!file.__initial) {
113+
// when full file is loaded, allow editor with user interaction.
114+
setLoading(false);
115+
}
116+
117+
initWith(file);
99118
}, [
100119
filekey,
101120
file,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export class FigmaDesignRepository {
2121
constructor(
2222
readonly auth: { accessToken: string; personalAccessToken: string }
2323
) {}
24+
25+
static async fetchCachedFile(fileId: string) {
26+
const store = new FigmaFileStore(fileId);
27+
const existing = await store.get();
28+
if (existing) {
29+
return { ...existing, __initial: false } as TFetchFileForApp;
30+
} else {
31+
throw new Error("file not found");
32+
}
33+
}
34+
2435
async *fetchFile(fileId: string) {
2536
const store = new FigmaFileStore(fileId);
2637
const existing = await store.get();

0 commit comments

Comments
 (0)