-
Notifications
You must be signed in to change notification settings - Fork 953
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose suggestNewPlugin as a shared util
Reviewed By: antonk52, ivanmisuno Differential Revision: D46798839 fbshipit-source-id: f2b942a138a998f167b60c04c32c634545bbfe96
- Loading branch information
1 parent
7fcaf52
commit 1c53209
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
import {Button, notification} from 'antd'; | ||
import {getFlipperLib, PluginClient} from 'flipper-plugin-core'; | ||
import * as React from 'react'; | ||
|
||
export type SuggestNewPluginProps = { | ||
newPluginGK?: string; | ||
newPluginId: string; | ||
newPluginName: string; | ||
legacyPluginId: string; | ||
legacyPluginName: string; | ||
client: PluginClient; | ||
}; | ||
|
||
export const suggestNewPlugin = ({ | ||
newPluginGK, | ||
newPluginId, | ||
newPluginName, | ||
legacyPluginId, | ||
legacyPluginName, | ||
client, | ||
}: SuggestNewPluginProps) => { | ||
if (newPluginGK && !getFlipperLib().GK(newPluginGK)) { | ||
return; | ||
} | ||
|
||
const lastShownTimestampKey = `${legacyPluginId}-${newPluginId}-BannerLastShownTimestamp`; | ||
let lastShownTimestampFromStorage = undefined; | ||
try { | ||
lastShownTimestampFromStorage = window.localStorage.getItem( | ||
lastShownTimestampKey, | ||
); | ||
} catch (e) {} | ||
|
||
if (lastShownTimestampFromStorage) { | ||
const WithinOneDay = (timestamp: number) => { | ||
const Day = 1 * 24 * 60 * 60 * 1000; | ||
const DayAgo = Date.now() - Day; | ||
|
||
return timestamp > DayAgo; | ||
}; | ||
const lastShownTimestamp = Number(lastShownTimestampFromStorage); | ||
if (WithinOneDay(lastShownTimestamp)) { | ||
// The banner was shown less than 24-hours ago, don't show it again. | ||
return; | ||
} | ||
} | ||
|
||
const lastShownTimestamp = Date.now(); | ||
try { | ||
window.localStorage.setItem( | ||
lastShownTimestampKey, | ||
String(lastShownTimestamp), | ||
); | ||
} catch (e) {} | ||
|
||
const key = `open-${newPluginId}-${lastShownTimestamp}`; | ||
const btn = ( | ||
<Button | ||
type="primary" | ||
size="small" | ||
onClick={() => { | ||
notification.close(key); | ||
client.selectPlugin(newPluginId, undefined); | ||
}}> | ||
Try it now! | ||
</Button> | ||
); | ||
|
||
notification.open({ | ||
message: `${legacyPluginName} plugin is being deprecated.`, | ||
description: `The new replacement plugin, ${newPluginName}, is available for use now. | ||
Find it on the left panel`, | ||
duration: 30, | ||
type: 'warning', | ||
btn, | ||
key, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters