Skip to content

Commit c549a8a

Browse files
CompuIvesSaraVieira
authored andcommitted
Make sure to load the last screenshot in the dashboard (codesandbox#2597)
Normally we've loaded the cached screenshot of a sandbox in the dashboard, this change ensures that we try to download a new version of a screenshot if the sandboxcard has been in the screen long enough and we know that the screenshot is outdated.
1 parent 17c9b7f commit c549a8a

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

packages/app/src/app/pages/Dashboard/Content/SandboxCard/index.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Props = {
4242
template: TemplateType;
4343
customTemplate: { color: string } | null;
4444
screenshotUrl: string | undefined;
45+
screenshotOutdated: boolean;
4546
setSandboxesSelected: (
4647
ids: string[],
4748
options?: { additive?: boolean; range?: boolean }
@@ -86,13 +87,18 @@ class SandboxItemComponent extends React.PureComponent<Props, State> {
8687

8788
state: State = {
8889
renamingSandbox: false,
89-
screenshotUrl: this.props.screenshotUrl,
90+
screenshotUrl: null,
9091
};
9192

92-
requestScreenshot = () => {
93-
this.setState({
94-
screenshotUrl: `/api/v1/sandboxes/${this.props.id}/screenshot.png`,
95-
});
93+
requestScreenshot = async () => {
94+
const url = `/api/v1/sandboxes/${this.props.id}/screenshot.png`;
95+
try {
96+
await fetch(url);
97+
} finally {
98+
this.setState({
99+
screenshotUrl: url,
100+
});
101+
}
96102
};
97103

98104
getPrivacyIcon = () => {
@@ -115,7 +121,7 @@ class SandboxItemComponent extends React.PureComponent<Props, State> {
115121
};
116122

117123
checkScreenshot() {
118-
if (!this.state.screenshotUrl && this.hasScreenshot()) {
124+
if (this.props.screenshotOutdated && this.hasScreenshot()) {
119125
// We only request the screenshot if the sandbox card is in view for > 1 second
120126
this.screenshotTimeout = window.setTimeout(() => {
121127
this.requestScreenshot();
@@ -125,7 +131,7 @@ class SandboxItemComponent extends React.PureComponent<Props, State> {
125131

126132
UNSAFE_componentWillReceiveProps(nextProps) {
127133
if (nextProps.id !== this.props.id) {
128-
this.setState({ screenshotUrl: nextProps.screenshotUrl }, () => {
134+
window.requestAnimationFrame(() => {
129135
this.checkScreenshot();
130136
});
131137
}
@@ -467,8 +473,6 @@ class SandboxItemComponent extends React.PureComponent<Props, State> {
467473
selected,
468474
} = this.props;
469475

470-
const { screenshotUrl } = this.state;
471-
472476
const templateInfo = getTemplate(template);
473477

474478
return (
@@ -517,7 +521,8 @@ class SandboxItemComponent extends React.PureComponent<Props, State> {
517521
{this.hasScreenshot() && (
518522
<SandboxImage
519523
style={{
520-
backgroundImage: `url(${screenshotUrl})`,
524+
backgroundImage: `url(${this.state.screenshotUrl ||
525+
this.props.screenshotUrl})`,
521526
}}
522527
/>
523528
)}

packages/app/src/app/pages/Dashboard/Content/SandboxGrid/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class SandboxGridComponent extends React.Component<*, State> {
333333
privacy={item.privacy}
334334
isPatron={this.props.store.isPatron}
335335
screenshotUrl={item.screenshotUrl}
336+
screenshotOutdated={item.screenshotOutdated}
336337
/>
337338
);
338339
};

packages/app/src/app/pages/Dashboard/queries.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const SANDBOX_FRAGMENT = gql`
2222
updatedAt
2323
privacy
2424
screenshotUrl
25+
screenshotOutdated
2526
2627
source {
2728
template

0 commit comments

Comments
 (0)